原生js實(shí)現(xiàn)自由拖拽彈窗代碼demo
本文為大家分享了原生彈窗拖拽代碼demo,供大家參考,具體內(nèi)容如下
效果圖:

實(shí)現(xiàn)代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>彈窗拖拽</title>
<style>
*{margin:0;padding:0;}
.box{position: absolute;width: 400px;height: 300px;top:100px;left:100px;border:1px solid #001c67;background: #}
.move{position: absolute;width: 100px;height: 100px;top:100px;left:150px;border:1px solid #000;}
.move:hover{cursor: move;}
.close{position: absolute;width: 30px;height: 30px;top:0px;right:0px;background:red;text-align: center;line-height: 30px;}
</style>
<script>
window.onload=function(){
var oMove=document.getElementById('move');
// 拖曳
oMove.onmousedown=fnDown;
// 關(guān)閉
var oClose=document.getElementById('close');
oClose.onclick=function(){
document.getElementById('box').style.display='none';
}
}
function fnDown(event){
event = event || window.event;
var oDrag=document.getElementById('box'),
// 光標(biāo)按下時光標(biāo)和面板之間的距離
disX=event.clientX-oDrag.offsetLeft,
disY=event.clientY-oDrag.offsetTop;
// 移動
document.onmousemove=function(event){
event = event || window.event;
var l=event.clientX-disX,
t=event.clientY-disY,
// 最大left,top值
leftMax=(document.documentElement.clientWidth || document.body.clientWidth)-oDrag.offsetWidth,
topMax=(document.documentElement.clientHeight || document.body.clientHeight)-oDrag.offsetHeight;
if(l<0) l=0;
if(l>leftMax) l=leftMax;
if(t<0) t=0;
if(t>topMax) t=topMax;
oDrag.style.left=l+'px';
oDrag.style.top=t+'px';
}
// 釋放鼠標(biāo)
document.onmouseup=function(){
document.onmousemove=null;
document.onmouseup=null;
}
}
</script>
</head>
<body>
<div class="box" id="box">
<div class="move" id="move">拖拽區(qū)域</div>
<div class="close" id="close">X</div>
</div>
</body>
</html>
主要注意幾點(diǎn):
1.event,IE兼容問題
2.點(diǎn)擊鼠標(biāo)時要先判斷鼠標(biāo)與面板之間的距離
3.要判斷彈窗與瀏覽器整個區(qū)域的距離,不能讓彈窗跑出瀏覽器外的區(qū)域
4.松開鼠標(biāo)要解除事件綁定,不然會有bug
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript通過代碼調(diào)用Flash顯示的方法
這篇文章主要介紹了JavaScript通過代碼調(diào)用Flash顯示的方法,實(shí)例分析了JavaScript通過flash插件swfobject.js調(diào)用flash顯示的具體操作技巧,需要的朋友可以參考下2016-02-02
JavaScrpt判斷一個數(shù)是否是質(zhì)數(shù)的實(shí)例代碼
本文通過實(shí)例代碼給大家分享了JavaScrpt判斷一個數(shù)是否是質(zhì)數(shù),需要的朋友參考下吧2017-06-06
tangram.js庫實(shí)現(xiàn)js類的方式實(shí)例分析
這篇文章主要介紹了tangram.js庫實(shí)現(xiàn)js類的方式,結(jié)合實(shí)例形式分析了tangram.js庫實(shí)現(xiàn)類的創(chuàng)建、繼承等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
jQuery 實(shí)現(xiàn)倒計(jì)時天,時,分,秒功能
本文通過html代碼和js代碼給大家介紹了實(shí)現(xiàn)倒計(jì)時天,時,分,秒功能,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07
localStorage的黑科技-js和css緩存機(jī)制
本文主要介紹了localStorage的黑科技-js和css緩存機(jī)制的相關(guān)知識,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
Javascript Bootstrap的網(wǎng)格系統(tǒng),導(dǎo)航欄和輪播詳解
這篇文章主要為大家介紹了Javascript Bootstrap的網(wǎng)格系統(tǒng),導(dǎo)航欄和輪播,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11
echarts多條折線圖動態(tài)分層的實(shí)現(xiàn)方法
這篇文章主要介紹了echarts多條折線圖動態(tài)分層的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

