最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實現(xiàn)鼠標(biāo)拖動功能

 更新時間:2017年03月20日 11:58:26   作者:18301695170  
本文主要介紹了js實現(xiàn)鼠標(biāo)拖動功能的實例代碼。具有很好的參考價值。下面跟著小編一起來看下吧

效果圖:

代碼如下:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title></title>
 <style type="text/css">
  #div{
  width: 200px;
  height: 200px;
  background: green;
  position: absolute;
  }
  #div2{
  width: 200px;
  height: 200px;
  background: gold;
  position: absolute;
  top: 200px;
  left: 200px;
  }
 </style>
 </head>
 <body>
 <div id="div">
 </div>
 <div id="div2"></div>
 <script>
  window.onload=function(){
  var div=document.getElementById("div");
  div.onmousedown=function(ev){
   var e=window.event || ev;
   //var Mydiv=document.getElementById("div");
   //獲取到鼠標(biāo)點擊的位置距離div左側(cè)和頂部邊框的距離;
   var oX=e.clientX-div.offsetLeft;
   var oY=e.clientY-div.offsetTop;
   //當(dāng)鼠標(biāo)移動,把鼠標(biāo)的偏移量付給div
   document.onmousemove=function(ev){
   //計算出鼠標(biāo)在XY方向上移動的偏移量,把這個偏移量加給DIV的左邊距和上邊距,div就會跟著移動
   var e=window.event|| ev;
   div.style.left=e.clientX-oX+"px";
   div.style.top=e.clientY-oY+"px";
   }
   //當(dāng)鼠標(biāo)按鍵抬起,清除移動事件
   document.onmouseup=function(){
   document.onmousemove=null;
   document.onmouseup=null;
   }
  }
  var div2=document.getElementById("div2");
  div2.onmousedown=function(ev){
   var e=window.event || ev;
   //var Mydiv=document.getElementById("div");
   //獲取到鼠標(biāo)點擊的位置距離div左側(cè)和頂部邊框的距離;
   var oX=e.clientX-div2.offsetLeft;
   var oY=e.clientY-div2.offsetTop;
   //當(dāng)鼠標(biāo)移動,把鼠標(biāo)的偏移量付給div
   document.onmousemove=function(ev){
   //計算出鼠標(biāo)在XY方向上移動的偏移量,把這個偏移量加給DIV的左邊距和上邊距,div就會跟著移動
   var e=window.event|| ev;
   div2.style.left=e.clientX-oX+"px";
   div2.style.top=e.clientY-oY+"px";
   }
   //當(dāng)鼠標(biāo)按鍵抬起,清除移動事件
   document.onmouseup=function(){
   document.onmousemove=null;
   document.onmouseup=null;
   }
  }
  }
 </script>
 </body>
</html>

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

最新評論

嘉兴市| 县级市| 栖霞市| 延长县| 新闻| 朔州市| 东港市| 浑源县| 武川县| 甘孜| 松潘县| 栖霞市| 乌鲁木齐县| 西宁市| 梁平县| 达拉特旗| 新闻| 湖南省| 太仆寺旗| 河池市| 洪雅县| 江都市| 邵东县| 宜章县| 出国| 乌审旗| 商都县| 铜陵市| 会东县| 永德县| 长白| 靖州| 本溪| 金坛市| 凤山市| 嘉善县| 兴和县| 昭苏县| 清水县| 邢台县| 祥云县|