欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js實(shí)現(xiàn)小窗口拖拽效果

 更新時(shí)間:2016年12月03日 10:27:00   作者:代碼小公主  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)小窗口拖拽效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)窗口拖拽的具體代碼,供大家參考,具體內(nèi)容如下

<!DOCTYPE html>
<html>
 
 <head>
 <meta charset="UTF-8">
 <title></title>
 <style type="text/css">
 #box {
 height: 300px;
 width: 300px;
 background-color: green;
 position: absolute;
 }
 </style>
 </head>
 
 <body>
 <div id="box">
 
 </div>
 </body>
 <script type="text/javascript">
 var box = document.getElementById("box");
 //鼠標(biāo)按下的函數(shù)
 box.onmousedown = function(ev) {
 var oEvent = ev || event;
 //求出鼠標(biāo)和box的位置差值
 var x = oEvent.clientX - box.offsetLeft;
 var y = oEvent.clientY - box.offsetTop;
 //鼠標(biāo)移動(dòng)的函數(shù)
 //把事件加在document上,解決因?yàn)槭髽?biāo)移動(dòng)太快時(shí),
 //鼠標(biāo)超過box后就沒有了拖拽的效果的問題
 document.onmousemove = function(ev) {
  var oEvent = ev || event;
 
  //保證拖拽框一直保持在瀏覽器窗口內(nèi)部,不能被拖出的瀏覽器窗口的范圍
  var l = oEvent.clientX - x;
  var t = oEvent.clientY - y;
  if(l < 0) {
  l = 0;
 
  } else if(l > document.documentElement.clientWidth - box.offsetWidth) {
  l = document.documentElement.clientWidth - box.offsetWidth;
  }
  if(t < 0) {
  t = 0;
  } else if(t > document.documentElement.clientHeight - box.offsetHeight) {
  t = document.documentElement.clientHeight - box.offsetHeight;
  }
  box.style.left = l + "px";
  box.style.top = t + "px";
 }
 //鼠標(biāo)抬起的函數(shù)
 document.onmouseup = function() {
  document.onmousemove = null;
  document.onmouseup = null;
 }
 //火狐瀏覽器在拖拽空div時(shí)會(huì)出現(xiàn)bug
 //return false阻止默認(rèn)事件,解決火狐的bug
 return false;
 
 }
 </script>
 
</html>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論