<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

        自己封裝的一個(gè)原生JS拖動(dòng)方法(推薦)

        來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 20:29:10
        文檔

        自己封裝的一個(gè)原生JS拖動(dòng)方法(推薦)

        自己封裝的一個(gè)原生JS拖動(dòng)方法(推薦):代碼:function drag(t,p){ var point = p null, target = t null, resultX = 0, resultY = 0; (!point) point = target : ''; //如果沒有拖動(dòng)點(diǎn),則拖動(dòng)點(diǎn)默認(rèn)為整個(gè)別拖動(dòng)元素 function getPos(t){ va
        推薦度:
        導(dǎo)讀自己封裝的一個(gè)原生JS拖動(dòng)方法(推薦):代碼:function drag(t,p){ var point = p null, target = t null, resultX = 0, resultY = 0; (!point) point = target : ''; //如果沒有拖動(dòng)點(diǎn),則拖動(dòng)點(diǎn)默認(rèn)為整個(gè)別拖動(dòng)元素 function getPos(t){ va

        代碼:

        function drag(t,p){
         
         var point = p || null,
         target = t || null,
         resultX = 0,
         resultY = 0;
         
         (!point)? point = target : ''; //如果沒有拖動(dòng)點(diǎn),則拖動(dòng)點(diǎn)默認(rèn)為整個(gè)別拖動(dòng)元素
         
         function getPos(t){
         var offsetLeft = 0,
         offsetTop = 0,
         offsetParent = t;
         
         while(offsetParent){
         offsetLeft+=offsetParent.offsetLeft;
         offsetTop+=offsetParent.offsetTop;
         offsetParent = offsetParent.offsetParent;
         }
         
         return {'top':offsetTop,'left':offsetLeft};
         }
         
         function core(){
         
         var width = document.body.clientWidth || document.documentElement.clientWidth,
         height = document.body.clientHeight || document.documentElement.clientHeight; 
         maxWidth = width - target.offsetWidth,
         maxHeight = height - target.offsetHeight;
         
         (resultX >= maxWidth)? target.style.left = maxWidth+'px' : (resultX > 0)?target.style.left = resultX +'px': ''; //重置默認(rèn)位置。
         (resultY >= maxHeight)? target.style.top = maxHeight +'px' : (resultY > 0)?target.style.top = resultY +'px':''; //重置默認(rèn)位置。
         
         point.onmousedown=function(e){ 
         var e = e || window.event,
         coordX = e.clientX,
         coordY = e.clientY,
         posX = getPos(target).left,
         posY = getPos(target).top;
         
         point.setCapture && point.setCapture(); //將Mouse事件鎖定到指定元素上。
         document.onmousemove=function(e){
         
         var ev = e || window.event,
         moveX = ev.clientX,
         moveY = ev.clientY;
         
         resultX = moveX - (coordX - posX); //結(jié)果值是坐標(biāo)點(diǎn)減去被拖動(dòng)元素距離瀏覽器左側(cè)的邊距
         resultY = moveY - (coordY - posY);
         
         (resultX > 0 )?((resultX < maxWidth)?target.style.left = resultX+'px' : target.style.left = maxWidth+'px') : target.style.left = '0px'; 
         (resultY > 0 )?((resultY < maxHeight)?target.style.top = resultY+'px' : target.style.top = maxHeight+'px') : target.style.top = '0px'; 
         
         ev.stopPropagation && ev.stopPropagation(); 
         ev.preventDefault;
         ev.returnValue = false;
         ev.cancelBubble = true;
         };
         };
         document.onmouseup=function(){ // 解決拖動(dòng)時(shí),當(dāng)鼠標(biāo)指向的DOM對(duì)象非拖動(dòng)點(diǎn)元素時(shí),無法觸發(fā)拖動(dòng)點(diǎn)的onmousedown的BUG。
         document.onmousemove = null; 
         point.releaseCapture && point.releaseCapture(); // 將Mouse事件從指定元素上移除。
         };
         point.onmouseup=function(e){
         var e = e || window.event;
         document.onmousemove = null;
         point.releaseCapture && point.releaseCapture();
         };
         }
         core();
         window.onresize = core; 
        }

        使用方式:

        drag(t,p)
        /* 
         * 說明 
         * t 表示被拖動(dòng)的元素
         * p 表示拖動(dòng)點(diǎn)
        */
         
        // 注意:如果省略拖動(dòng)點(diǎn),默認(rèn)可拖動(dòng)的區(qū)域是整個(gè)被拖動(dòng)元素

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        自己封裝的一個(gè)原生JS拖動(dòng)方法(推薦)

        自己封裝的一個(gè)原生JS拖動(dòng)方法(推薦):代碼:function drag(t,p){ var point = p null, target = t null, resultX = 0, resultY = 0; (!point) point = target : ''; //如果沒有拖動(dòng)點(diǎn),則拖動(dòng)點(diǎn)默認(rèn)為整個(gè)別拖動(dòng)元素 function getPos(t){ va
        推薦度:
        標(biāo)簽: 方法 js 封裝
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产男女猛烈无遮档免费视频网站| 国产亚洲精品不卡在线| 亚洲最大免费视频网| 99久久这里只精品国产免费| 精品亚洲视频在线| 亚洲成A∨人片在线观看不卡| 97碰公开在线观看免费视频| 精品在线免费视频| 久久噜噜噜久久亚洲va久| 久久这里只有精品国产免费10| 一级毛片a免费播放王色电影| 久久精品国产亚洲AV嫖农村妇女| 国产免费久久精品| 一级毛片免费不卡在线| 羞羞视频网站免费入口| 亚洲精品无码不卡| 亚洲AV中文无码乱人伦| 97热久久免费频精品99| 91免费福利视频| 亚洲AV永久无码天堂影院| 亚洲a一级免费视频| mm1313亚洲精品无码又大又粗 | 亚洲伊人久久大香线焦| 亚洲人成网站18禁止一区| 四虎在线最新永久免费| 久久国产美女免费观看精品| 亚洲中文字幕无码中文字| 亚洲男人的天堂在线播放| 亚洲黄片手机免费观看| 日韩中文字幕免费| 免费专区丝袜脚调教视频| a级毛片在线免费| 日韩大片在线永久免费观看网站| 亚洲伊人久久精品| 久久精品国产亚洲AV无码娇色| 久久亚洲AV无码西西人体| 国产精品99久久免费| 大学生一级毛片免费看| 91精品国产免费| 国产一级片免费看| 两个人看的www免费高清|