溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用JavaScript實(shí)現(xiàn)簡單的拖拽效果

發(fā)布時(shí)間:2021-11-08 08:59:18 來源:億速云 閱讀:97 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“怎么用JavaScript實(shí)現(xiàn)簡單的拖拽效果”,在日常操作中,相信很多人在怎么用JavaScript實(shí)現(xiàn)簡單的拖拽效果問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用JavaScript實(shí)現(xiàn)簡單的拖拽效果”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

1.先搭架子:

* {
            margin: 0;
            padding: 0;
        }
        
        p {
            background: skyblue;
            text-align: center;
        }
        
        html,
        body {
            width: 100%;
            height: 100%;
        }
        
        .mask {
            width: 100%;
            height: 100%;
            position: fixed;
            left: 0;
            top: 0;
            background: rgba(0, 0, 0, .5);
            display: none;
        }
        
        .login {
            width: 400px;
            height: 300px;
            background: purple;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            display: none;
            cursor: move;
        }
        
        .login>span {
            display: inline-block;
            width: 50px;
            height: 50px;
            background: red;
            position: absolute;
            top: 0;
            right: 0;
        }
<p>我是p標(biāo)簽</p>
    <a href="http://www.baidu.com" >官網(wǎng)</a>
    <div class="mask"></div>
    <div class="login">
        <span></span>
</div>

怎么用JavaScript實(shí)現(xiàn)簡單的拖拽效果

2.邏輯部分

//1.拿到需要操作的元素
const oP = document.querySelector("p");
const oMask = document.querySelector(".mask");
const oLogin = document.querySelector(".login");
const oClose = oLogin.querySelector(".login>span");
// console.log(oClose);
 
//2.監(jiān)聽點(diǎn)擊事件
oP.onclick = function() {
    oMask.style.display = "block";
    oLogin.style.display = "block";
        };
        oClose.onclick = function() {
            oMask.style.display = "none";
            oLogin.style.display = "none";
        };
 
//3.監(jiān)聽登錄框的按下和移動(dòng)事件
oLogin.onmousedown = function(e) {
            e = e || e.window;
 
            //1.計(jì)算固定不變的距離
            const x = e.pageX - oLogin.offsetLeft;
            const y = e.pageY - oLogin.offsetTop;
            // console.log(x);
 
            //2.監(jiān)聽移動(dòng)事件
            oLogin.onmousemove = function(e) {
                e = e || e.window;
 
                //3.計(jì)算移動(dòng)之后的偏移位
                let offsetX = e.pageX - x;
                let offsetY = e.pageY - y;
 
                //4.重新設(shè)置登錄框的位置
                oLogin.style.left = offsetX + 'px';
                oLogin.style.top = offsetY + 'px';
            };
        };
 
        oLogin.onmouseup = function() {
            oLogin.onmousemove = null;
        };

怎么用JavaScript實(shí)現(xiàn)簡單的拖拽效果

到此,關(guān)于“怎么用JavaScript實(shí)現(xiàn)簡單的拖拽效果”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI