js實(shí)現(xiàn)彈出框的拖拽效果實(shí)例代碼詳解
具體代碼如下所示:
//HTML部分
<div class="wrap"></div>
<div class="popUpBox">
<div class="layer-head"><div class="layer-head-text">彈出框</div><div class="layer-close"></div></div>
<div class="layer-body"></div>
<div class="layer-footer">
<div class="layer-footer-button-group">
<div class="layer-footer-button layer-sure">確定</div>
<div class="layer-footer-button layer-cancel">取消</div>
</div>
</div>
</div>
//CSS部分
.wrap {
position: fixed;
left: 0;
top: 0;
background-color: #000;
z-index: 10000;
opacity: 0.3;
}
.popUpBox {
height: 400px;
width: 700px;
position: absolute;
overflow: hidden;
box-sizing: border-box;
z-index: 10000;
background-color: #fff;
border-radius: 2px;
box-shadow: 1px 1px 50px rgba(0,0,0,.3);
}
.layer-head {
width: 100%;
height: 35px;
border-bottom: 1px solid #eee;
box-sizing: border-box;
background-color: #f8f8f8;
border-radius: 4px 4px 0 0;
cursor: move;
}
.layer-head-text {
padding-left: 20px;
font-size: 14px;
color: #333;
height: 35px;
line-height: 34px;
float: left;
}
.layer-close {
float: right;
width: 16px;
height: 16px;
background-image: url(../images/close_hover.png);
background-repeat:no-repeat;
background-size:100% 100%;
position: absolute;
top: 10px;
right: 12px;
cursor: pointer;
}
.layer-body {
width: 100%;
height: calc(100% - 73px);
}
.layer-footer {
width: 100%;
height: 38px;
border-top: 1px solid #eee;
box-sizing: border-box;
background-color: #f8f8f8;
border-radius: 0 0 4px 4px;
}
.layer-footer-button-group {
padding: 5px 0 5px 576px;
height: 28px;
}
.layer-footer-button {
width: 56px;
height: 28px;
line-height: 28px;
margin-right: 6px;
box-sizing: border-box;
font-size: 12px;
float: left;
text-align: center;
cursor: pointer;
}
.layer-sure {
border: 1px solid #4898d5;
background-color: #2e8ded;
color: #fff;
}
.layer-cancel {
border: 1px solid #dedede;
background-color: #f1f1f1;
color: #333;
}
//點(diǎn)擊某物體時(shí),用drag對(duì)象即可,move和up是全局區(qū)域,也就是整個(gè)文檔通用,應(yīng)該使用document對(duì)象而不是drag對(duì)象(否則,采用drag對(duì)象時(shí)物體只能往右方或下方移動(dòng))
$(document).on('mousedown', '.layer-head', function(e) {
e = e || window.event; //兼容ie瀏覽器
var drag = $(this).parent();
$('body').addClass('select'); //webkit內(nèi)核和火狐禁止文字被選中
document.body.onselectstart = document.body.ondrag = function() { //ie瀏覽器禁止文字選中
return false;
}
if ($(e.target).hasClass('layer-close')) { //點(diǎn)關(guān)閉按鈕不能拖拽模態(tài)框
return;
}
var diffX = e.clientX - drag.offset().left; //鼠標(biāo)點(diǎn)擊物體那一刻相對(duì)于物體左側(cè)邊框的距離=點(diǎn)擊時(shí)的位置相對(duì)于瀏覽器最左邊的距離-物體左邊框相對(duì)于瀏覽器最左邊的距離
var diffY = e.clientY - drag.offset().top;
$(document).on('mousemove', function(e) {
e = e || window.event; //兼容ie瀏覽器
var left = e.clientX - diffX;
var top = e.clientY - diffY;
if (left < 0) {
left = 0;
} else if (left > window.innerWidth - drag.width()) {
left = window.innerWidth - drag.width();
}
if (top < 0) {
top = 0;
} else if (top > window.innerHeight - drag.height()){
top = window.innerHeight - drag.height();
}
//移動(dòng)時(shí)重新得到物體的距離,解決拖動(dòng)時(shí)出現(xiàn)晃動(dòng)的現(xiàn)象
drag.css({
'left': left + 'px',
'top': top + 'px'
});
});
$(document).on('mouseup', function(e) { //當(dāng)鼠標(biāo)彈起來(lái)的時(shí)候不再移動(dòng)
$(document).unbind("mousemove");
$(document).unbind("mouseup");
});
});
總結(jié)
以上所述是小編給大家介紹的js實(shí)現(xiàn)彈出框的拖拽效果實(shí)例代碼詳解 ,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- 原生JS實(shí)現(xiàn)可拖拽登錄框
- Javascript實(shí)現(xiàn)登錄框拖拽效果
- js實(shí)現(xiàn)登錄框鼠標(biāo)拖拽效果
- js實(shí)現(xiàn)百度登錄框鼠標(biāo)拖拽效果
- 百度Popup.js彈出框進(jìn)化版 拖拽小框架發(fā)布 兼容IE6/7/8,Firefox,Chrome
- javascript 網(wǎng)頁(yè)編輯框及拖拽圖片的問(wèn)題
- 使用純JS實(shí)現(xiàn)checkbox的框選效果(鼠標(biāo)拖拽多選)
- JavaScript實(shí)現(xiàn)模態(tài)框拖拽效果
- HTML+CSS+JavaScript實(shí)現(xiàn)可拖拽模態(tài)框
- javascript實(shí)現(xiàn)登錄框拖拽
相關(guān)文章
通過(guò)js動(dòng)態(tài)操作table(新增,刪除相關(guān)列信息)
通過(guò)js動(dòng)態(tài)操作table(新增,刪除相關(guān)列信息)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-05-05
JavaScript仿小米商城官網(wǎng)完整頁(yè)面實(shí)現(xiàn)流程
只能看不能玩的靜態(tài)頁(yè)面早就看夠了吧,今天我們來(lái)做一個(gè)相對(duì)完整的動(dòng)態(tài)網(wǎng)站,用Javascript來(lái)實(shí)現(xiàn)模仿小米的官網(wǎng)商城,感興趣的朋友快來(lái)看看吧2021-11-11
一篇文章帶你學(xué)會(huì)JavaScript計(jì)時(shí)事件
JS可以實(shí)現(xiàn)很多java代碼不易完成的功能,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)一篇文章帶你學(xué)會(huì)JavaScript計(jì)時(shí)事件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
JavaScript設(shè)計(jì)模式之單例模式原理與用法實(shí)例分析
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之單例模式原理與用法,結(jié)合實(shí)例形式分析了單例模式的原理、命名空間的使用、閉包、惰性單例形式以及單例模式的基本應(yīng)用,需要的朋友可以參考下2018-07-07
javascript+css3 實(shí)現(xiàn)動(dòng)態(tài)按鈕菜單特效
這篇文章主要介紹了javascript+css3 實(shí)現(xiàn)動(dòng)態(tài)按鈕菜單特效的相關(guān)資料,需要的朋友可以參考下2016-02-02

