html部分

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模态框</title>
  6. <!-- IMPORT CSS -->
  7. <link rel="stylesheet" href="css/bootstrap.min.css">
  8. <style>
  9. .modal-backdrop {
  10. display: none;
  11. background: rgba(0, 0, 0, .5);
  12. }
  13. .modal {
  14. width: 500px;
  15. height: 264px;
  16. transition: opacity .3s linear !important;
  17. }
  18. .modal .modal-dialog {
  19. margin: 0;
  20. }
  21. .modal .modal-header {
  22. cursor: move;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <!-- Button-->
  28. <button type="button" class="btn btn-primary" id="loginBtn">
  29. 百度登录
  30. </button>
  31. <!-- Modal -->
  32. <div class="modal" id="loginModal" style="opacity:0">
  33. <div class="modal-dialog">
  34. <div class="modal-content">
  35. <div class="modal-header">
  36. <h5 class="modal-title">百度登录</h5>
  37. <button type="button" class="close" id="loginCloseBtn">
  38. <span>&times;</span>
  39. </button>
  40. </div>
  41. <div class="modal-body">
  42. 夫君子之行,静以修身,俭以养德。非淡泊无以明志,非宁静无以致远。夫学须静也,才须学也,非学无以广才,非志无以成学。淫慢则不能励精,险躁则不能治性。年与时驰,意与日去,遂成枯落,多不接世,悲守穷庐,将复何及!
  43. </div>
  44. <div class="modal-footer">
  45. <button type="button" class="btn btn-primary">提交</button>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. <div class="modal-backdrop" id="loginModalBack"></div>
  51. <!-- IMPORT JS -->
  52. <script src="js/jquery.min.js"></script>
  53. <script src="js/drag-plugin.js"></script>
  54. <script src="js/dialog.js"></script>
  55. </body>
  56. </html>

js部分

  1. let $loginBtn = $('#loginBtn'),
  2. $loginModal = $('#loginModal'),
  3. $loginCloseBtn = $('#loginCloseBtn'),
  4. $loginModalBack = $('#loginModalBack'),
  5. $window = $(window);
  6. $loginBtn.click(function () {
  7. $loginModal.css("display", "block");
  8. $loginModalBack.css('display', 'block');
  9. $loginModal.css("opacity", 1);
  10. // 实现居中
  11. $loginModal.css({
  12. left: ($window.outerWidth() - $loginModal.outerWidth()) / 2,
  13. top: ($window.outerHeight() - $loginModal.outerHeight()) / 2
  14. });
  15. });
  16. $loginCloseBtn.click(function () {
  17. $loginModal.css('opacity', 0).one('transitionend', function () {
  18. $loginModal.css('display', 'none');
  19. $loginModalBack.css('display', 'none');
  20. });
  21. });
  22. let $modalHeade = $loginModal.find('.modal-header'),
  23. $document = $(document),
  24. $window = $(window);
  25. // 这样处理,当鼠标按下的时候,DOWN 方法中的 THIS 是 MODAL-HEAD,但是我们后期要操作整个盒子的样式,我们最好让 THIS 变为整个盒子(原生 JS 对象)
  26. // $modalHeade.on('mousedown', down);
  27. $modalHeade.on('mousedown', down.bind($loginModal.get(0)));
  28. function down(ev) {
  29. let $this = $(this);
  30. this.startX = ev.pageX;
  31. this.startY = ev.pageY;
  32. this.startL = parseFloat($this.css('left'));
  33. this.startT = parseFloat($this.css('top'));
  34. this._move = move.bind(this);
  35. this._up = up.bind(this);
  36. $document.on('mousemove', this._move);
  37. $document.on('mouseup', this._up);
  38. }
  39. function move(ev) {
  40. let $this = $(this),
  41. curL = ev.pageX - this.startX + this.startL,
  42. curT = ev.pageY - this.startY + this.startT;
  43. let minL = 0,
  44. minT = 0,
  45. maxL = $window.outerWidth() - $this.outerWidth(),
  46. maxT = $window.outerHeight() - $this.outerHeight();
  47. curL = curL < minL ? minL : (curL > maxL ? maxL : curL);
  48. curT = curT < minT ? minT : (curT > maxT ? maxT : curT);
  49. $this.css({
  50. top: curT,
  51. left: curL
  52. });
  53. }
  54. function up(ev) {
  55. $document.off('mousemove', this._move);
  56. $document.off('mouseup', this._up);
  57. }
  58. /*
  59. * 给元素设置自定义属性
  60. * 1. 给内存空间中设置一个属性
  61. * box.myIndex=1;
  62. * $box.myIndex=1;
  63. *
  64. * 2. 把自定义属性设置在元素的行内属性上(设置的属性值最后都要变为字符串)
  65. * 我们在案例中,数据绑定阶段,把一些值存储到元素的行内上,以后要获取的时候只能基于 attr / getAttribute 获取
  66. * box.setAttribute('myIndex',1);
  67. * $box.attr('myIndex',1);
  68. */