html部分
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>模态框</title> <!-- IMPORT CSS --> <link rel="stylesheet" href="css/bootstrap.min.css"> <style> .modal-backdrop { display: none; background: rgba(0, 0, 0, .5); } .modal { width: 500px; height: 264px; transition: opacity .3s linear !important; } .modal .modal-dialog { margin: 0; } .modal .modal-header { cursor: move; } </style></head><body> <!-- Button--> <button type="button" class="btn btn-primary" id="loginBtn"> 百度登录 </button> <!-- Modal --> <div class="modal" id="loginModal" style="opacity:0"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">百度登录</h5> <button type="button" class="close" id="loginCloseBtn"> <span>×</span> </button> </div> <div class="modal-body"> 夫君子之行,静以修身,俭以养德。非淡泊无以明志,非宁静无以致远。夫学须静也,才须学也,非学无以广才,非志无以成学。淫慢则不能励精,险躁则不能治性。年与时驰,意与日去,遂成枯落,多不接世,悲守穷庐,将复何及! </div> <div class="modal-footer"> <button type="button" class="btn btn-primary">提交</button> </div> </div> </div> </div> <div class="modal-backdrop" id="loginModalBack"></div> <!-- IMPORT JS --> <script src="js/jquery.min.js"></script> <script src="js/drag-plugin.js"></script> <script src="js/dialog.js"></script></body></html>
js部分
let $loginBtn = $('#loginBtn'), $loginModal = $('#loginModal'), $loginCloseBtn = $('#loginCloseBtn'), $loginModalBack = $('#loginModalBack'), $window = $(window);$loginBtn.click(function () { $loginModal.css("display", "block"); $loginModalBack.css('display', 'block'); $loginModal.css("opacity", 1); // 实现居中 $loginModal.css({ left: ($window.outerWidth() - $loginModal.outerWidth()) / 2, top: ($window.outerHeight() - $loginModal.outerHeight()) / 2 });});$loginCloseBtn.click(function () { $loginModal.css('opacity', 0).one('transitionend', function () { $loginModal.css('display', 'none'); $loginModalBack.css('display', 'none'); });}); let $modalHeade = $loginModal.find('.modal-header'), $document = $(document), $window = $(window);// 这样处理,当鼠标按下的时候,DOWN 方法中的 THIS 是 MODAL-HEAD,但是我们后期要操作整个盒子的样式,我们最好让 THIS 变为整个盒子(原生 JS 对象)// $modalHeade.on('mousedown', down);$modalHeade.on('mousedown', down.bind($loginModal.get(0)));function down(ev) { let $this = $(this); this.startX = ev.pageX; this.startY = ev.pageY; this.startL = parseFloat($this.css('left')); this.startT = parseFloat($this.css('top')); this._move = move.bind(this); this._up = up.bind(this); $document.on('mousemove', this._move); $document.on('mouseup', this._up);}function move(ev) { let $this = $(this), curL = ev.pageX - this.startX + this.startL, curT = ev.pageY - this.startY + this.startT; let minL = 0, minT = 0, maxL = $window.outerWidth() - $this.outerWidth(), maxT = $window.outerHeight() - $this.outerHeight(); curL = curL < minL ? minL : (curL > maxL ? maxL : curL); curT = curT < minT ? minT : (curT > maxT ? maxT : curT); $this.css({ top: curT, left: curL });}function up(ev) { $document.off('mousemove', this._move); $document.off('mouseup', this._up);} /* * 给元素设置自定义属性 * 1. 给内存空间中设置一个属性 * box.myIndex=1; * $box.myIndex=1; * * 2. 把自定义属性设置在元素的行内属性上(设置的属性值最后都要变为字符串) * 我们在案例中,数据绑定阶段,把一些值存储到元素的行内上,以后要获取的时候只能基于 attr / getAttribute 获取 * box.setAttribute('myIndex',1); * $box.attr('myIndex',1); */