国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

簡單實現(xiàn)js拖拽效果

2024-05-06 16:38:39
字體:
供稿:網(wǎng)友

本文實例為大家分享了js拖拽效果展示的具體代碼,供大家參考,具體內(nèi)容如下

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Document</title>  <style>    *{      margin:0;      padding:0;    }    html,body{      width:100%;      height:100%;    }    #box{      position:absolute;      top:50%;      left:50%;      width:200px;      height:200px;      background:#ff6600;      margin:-100px 0 0 -100px;      cursor:move;      /*        不知道寬高的情況下的居中      position:absolute;      top:0;      left:0;      right:0;      bottom:0;      margin:auto;      */      }  </style></head><body>  <div id='box'>      </div>  <script>    //JS實現(xiàn)讓當前的元素在屏幕居中的位置    var box = document.getElementById('box');    // box.style.top = ((document.documentElement.clientHeight || document.body.clientHeight)-box.offsetHeight)/2 + "px";    // box.style.left = ((document.documentElement.clientWidth || document.body.clientWidth)-box.offsetWidth)/2 + "px";    //拖拽的原理    /*      當鼠標在盒子上按下的時候,我們開始拖拽(給盒子綁定onmousemove和onmouseup),當鼠標移動的時候,我們計算盒子的最新位置      當鼠標抬起的時候說明拖拽結(jié)束了,我們的move和up就沒用了,我們再把這兩個方法移除    */    box.onmousedown = down;    function down(e){      e = e || window.event;      //記錄開始位置的信息      this["strX"] = e.clientX;      this["strY"] = e.clientY;      this["strL"] = parseFloat(this.style.left);      this["strT"] = parseFloat(this.style.top);      //給元素綁定移動和抬起的事件      if(this.setCapture){        this.setCapture()//把當前的鼠標和this綁定在一起        this.onmousemove = move;        this.onmouseup= up;      }else{        var _this = this;        document.onmousemove = function(e){          // move(e)//這個里面的this是window          move.call(_this,e);        }          ;        document.onmouseup= function(e){          up.call(_this,e);        };      }          }    function move(e){      e = e || window.event;      var curL = (e.clientX-this["strX"])+this["strL"];      var curT = (e.clientY-this["strY"])+this["strT"];      //邊界判斷      var minL = 0,minT = 0,maxL = (document.documentElement.clientWidth || document.body.clientWidth) - this.offsetWidth,maxT = (document.documentElement.clientHeight || document.body.clientHeight) - this.offsetHeight;      curL = curL < minL ? minL :(curL > maxL ? maxL : curL);      curT = curT < minT ? minT :(curT > maxT ? maxT : curT)      this.style.left = curL + "px";      this.style.top = curT + "px";    }    function up(e){      if(this.releaseCapture){        this.releaseCapture();//把當前的鼠標和盒子解除綁定        this.onmousemove = null;        this.onmouseup= null;      }else{        document.onmousemove = null;        document.onmouseup= null;        //這樣綁定的話,move和up綁定的this都是document      }          }    //當鼠標移動過快的時候,我們的鼠標會脫離盒子,導致盒子的mousemove和mouseup事件都移除不到->"鼠標焦點丟失"    //在IE和火狐瀏覽器中,我們用一個方法把盒子和鼠標綁定在一起即可。    //鼠標再快也跑不出去文檔:我們把mousemove和mouseup綁定給document  </script></body></html>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 开化县| 庆云县| 信宜市| 南岸区| 罗江县| 义乌市| 瑞丽市| 肇东市| 富裕县| 永丰县| 仪陇县| 河曲县| 呼和浩特市| 襄樊市| 邢台市| 祁阳县| 三明市| 雅安市| 明星| 临颍县| 济源市| 龙岩市| 阿坝县| 宁南县| 广汉市| 织金县| 曲阜市| 吉水县| 安丘市| 盖州市| 元氏县| 明星| 美姑县| 嘉荫县| 丰台区| 都昌县| 电白县| 陆丰市| 南昌市| 行唐县| 调兵山市|