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

js實(shí)現(xiàn)簡(jiǎn)單的拖拽效果

 更新時(shí)間:2021年09月23日 09:30:12   作者:奔跑的肉夾饃_  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡(jiǎn)單的拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

1.拖拽的基本效果

思路:

鼠標(biāo)在盒子上按下時(shí),準(zhǔn)備移動(dòng) (事件加給物體)

鼠標(biāo)移動(dòng)時(shí),盒子跟隨鼠標(biāo)移動(dòng) (事件添加給頁(yè)面)

鼠標(biāo)抬起時(shí),盒子停止移動(dòng) (事件加給頁(yè)面)

var o = document.querySelector('div');
 
        //鼠標(biāo)按下
        o.onmousedown = function (e) {
            //鼠標(biāo)相對(duì)于盒子的位置
            var offsetX = e.clientX - o.offsetLeft;
            var offsetY = e.clientY - o.offsetTop;
            //鼠標(biāo)移動(dòng)
            document.onmousemove = function (e) {
                o.style.left = e.clientX - offsetX + "px";
                o.style.top = e.clientY - offsetY + "px";
            }
            //鼠標(biāo)抬起
            document.onmouseup = function () {
                document.onmousemove = null;
                document.onmouseup = null;
            }
        }

2.拖拽的問(wèn)題

若盒子中出現(xiàn)了文字,或盒子自身為圖片,由于瀏覽器的默認(rèn)行為(文字和圖片本身就可以拖拽),我們可以設(shè)置return false來(lái)阻止它的默認(rèn)行為,但這種攔截默認(rèn)行為在IE低版本中,不適用,可以使用全局捕獲來(lái)解決IE的問(wèn)題。

全局捕獲

全局捕獲僅適用于IE低版本瀏覽器。

<button>btn1</button>
    <button>btn2</button>
    <script>
        var bts = document.querySelectorAll('button')
 
        bts[0].onclick = function () {
            console.log(1);
        }
        bts[1].onclick = function () {
            console.log(2);
        }
 
        // bts[0].setCapture()  //添加全局捕獲
        // bts[0].releaseCapture() ;//釋放全局捕獲
</script>

一旦為指定節(jié)點(diǎn)添加全局捕獲,則頁(yè)面中其它元素就不會(huì)觸發(fā)同類型事件。

3.完整版的拖拽

var o = document.querySelector('div');
 
        //鼠標(biāo)按下
        o.onmousedown = function (e) {
            if (o.setCapture) {   //IE低版本
                o.setCapture()
            }
            e = e || window.event
            //鼠標(biāo)相對(duì)于盒子的位置
            var offsetX = e.clientX - o.offsetLeft;
            var offsetY = e.clientY - o.offsetTop;
            //鼠標(biāo)移動(dòng)
            document.onmousemove = function (e) {
                e = e || window.event
                o.style.left = e.clientX - offsetX + "px";
                o.style.top = e.clientY - offsetY + "px";
            }
            //鼠標(biāo)抬起
            document.onmouseup = function () {
                document.onmousemove = null;
                document.onmouseup = null;
                if (o.releaseCapture) {
                    o.releaseCapture();//釋放全局捕獲   
                }
            }
            return false;//標(biāo)準(zhǔn)瀏覽器的默認(rèn)行為
        }

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

相關(guān)文章

最新評(píng)論

玉门市| 连江县| 当涂县| 花莲市| 永昌县| 阜城县| 蒲江县| 蕉岭县| 岱山县| 龙泉市| 嘉黎县| 大庆市| 舟曲县| 邯郸县| 吉首市| 沙坪坝区| 浦东新区| 台江县| 建德市| 鄂州市| 阳西县| 绩溪县| 临江市| 大英县| 泰宁县| 巴楚县| 梨树县| 新平| 镇远县| 内丘县| 日照市| 墨竹工卡县| 边坝县| 温宿县| 化州市| 大邑县| 上蔡县| 德安县| 武穴市| 道真| 奇台县|