佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > html5 拖拽及用 js 實現(xiàn)拖拽功能的示例代碼

html5 拖拽及用 js 實現(xiàn)拖拽功能的示例代碼

熱門標(biāo)簽:使用U盤裝系統(tǒng) 智能手機 檢查注冊表項 網(wǎng)站建設(shè) 硅谷的囚徒呼叫中心 美圖手機 百度競價點擊價格的計算公式 阿里云

1. HTML5 拖拽

1.1 相關(guān)知識

拖拽元素:可以為元素添加 draggable="true"來讓元素能夠被拖拽。

拖拽元素的事件監(jiān)聽:(應(yīng)用于拖拽元素)

  • ondragstart當(dāng)拖拽開始時調(diào)用
  • ondragleave 當(dāng)鼠標(biāo)離開拖拽元素時調(diào)用
  • ondragend 當(dāng)拖拽結(jié)束時調(diào)用
  • ondrag 整個拖拽過程都會調(diào)用

目標(biāo)元素:把元素A拖拽到元素B里,那么元素B就是目標(biāo)元素。頁面中任何一個元素都可以成為目標(biāo)元素。

目標(biāo)元素的事件監(jiān)聽:(應(yīng)用于目標(biāo)元素)

  • ondragenter 當(dāng)拖拽元素進(jìn)入時調(diào)用
  • ondragover 當(dāng)拖拽元素停留在目標(biāo)元素上時,就會連續(xù)一直觸發(fā)(不管拖拽元素此時是移動還是不動的狀態(tài))
  • ondrop 當(dāng)在目標(biāo)元素上松開鼠標(biāo)時調(diào)用
  • ondragleave 當(dāng)鼠標(biāo)離開目標(biāo)元素時調(diào)用

如果想讓拖拽元素在目標(biāo)元素里做點事情,就必須要在 ondragover() 里加event.preventDefault()這一行代碼。

1.2 拖拽基礎(chǔ)

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            .box {
                width: 200px;
                height: 200px;
                background: green;
            }
            .box2 {
                position: relative;
                left: 300px;
                top: 50px;
                width: 300px;
                height: 300px;
                background: red;
            }
        </style>
    </head>
    <body>
        <div class="box" draggable="true"></div>
        <div class="box2"></div>
        <script>
            // HTML5 拖拽
            // 應(yīng)用于拖拽元素
            var box = document.querySelector('.box')
            box.ondragstart = function () {
                console.log('拖拽開始')
            }
            box.ondragleave = function () {
                console.log('鼠標(biāo)離開元素')
            }
            box.ondragend = function () {
                console.log('拖拽結(jié)束')
            }
            // box.ondrag = function () {
            //     console.log('在拖拽');
            // }

            // 應(yīng)用于目標(biāo)元素(想把 box 拖拽進(jìn)去的地方)
            var box2 = document.querySelector('.box2')
            box2.ondragenter = function () {
                console.log('進(jìn)來了')
            }
            box2.ondragleave = function () {
                console.log('離開了')
            }

            // 當(dāng)拖拽元素在 目標(biāo)元素上時,連續(xù)觸發(fā)
            box2.ondragover = function (e) {
                // 如果想讓拖拽元素在目標(biāo)元素里做點事情,就必須要在 ondragover() 里加event.preventDefault()這一行代碼。
                e.preventDefault()
                console.log('over')
            }
            box2.ondrop = function () {
                console.log('松開鼠標(biāo)了')
            }
        </script>
    </body>
</html>

1.3 將 A 在 B、C 之間拖拽

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            .box-b {
                width: 250px;
                height: 250px;
                background: green;
            }
            .cell-a {
                float: left;
                width: 50px;
                height: 50px;
                margin: 5px;
                text-align: center;
                line-height: 50px;
                border-radius: 50%;
                background: red;
            }
            .box-c {
                width: 200px;
                height: 200px;
                margin-top: 10px;
                background: skyblue;
            }
        </style>
    </head>
    <body>
        <p>boxB</p>
        <div class="box-b">
            <div class="cell-a" draggable="true">1</div>
            <div class="cell-a" draggable="true">2</div>
            <div class="cell-a" draggable="true">3</div>
            <div class="cell-a" draggable="true">4</div>
            <div class="cell-a" draggable="true">5</div>
        </div>
        <p>boxC</p>
        <div class="box-c"></div>
        <script>
            var cellA = document.querySelectorAll('.cell-a')
            var boxB = document.querySelector('.box-b')
            var boxC = document.querySelector('.box-c')
            var temp = null

            cellA.forEach((cell, index) => {
                // 從 boxB 拖拽到 boxC
                cell.ondragstart = function () {
                    // 保持當(dāng)前拖拽的元素
                    temp = this
                }
                cell.ondragend = function () {
                    temp = null
                }
                boxC.ondragover = function (e) {
                    e.preventDefault()
                }
                boxC.ondragenter = function () {
                    this.appendChild(temp)
                }

                // 從 boxC 拖拽到 boxB
                boxB.ondragover = function (e) {
                    e.preventDefault()
                }
                boxB.ondragenter = function () {
                    this.appendChild(temp)
                }
            })
        </script>
    </body>
</html>

效果展示

2. 用 js 實現(xiàn)拖拽

2.1 js 簡單拖拽

按下鼠標(biāo)進(jìn)行簡單的拖拽。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            #box {
                position: absolute;
                width: 200px;
                height: 200px;
                background: green;
            }
        </style>
        <script>
            window.onload = function () {
                var box = document.getElementById('box')
                var disX = 0
                var disY = 0

                box.onmousedown = function (e) {
                    var e = e || window.event
                    disX = e.clientX - this.offsetLeft
                    disY = e.clientY - this.offsetTop
                    box.onmousemove = function (e) {
                        var e = e || window.event
                        box.style.left = e.clientX - disX + 'px'
                        box.style.top = e.clientY - disY + 'px'
                    }
                    box.onmouseup = function (e) {
                        console.log('end')
                        this.onmousemove = null
                    }
                    return false
                }
            }
        </script>
    </head>
    <body>
        <div id="box"></div>
    </body>
</html>

效果展示

2.2 帶效果的拖拽

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            .box {
                position: absolute;
                width: 200px;
                height: 200px;
                background: skyblue;
            }
            .box1 {
                position: absolute;
                border: 1px dashed black;
                opacity: 0.5;
            }
            .way-box {
                position: absolute;
                bottom: 30px;
                right: 30px;
                /* 無法選中 */
                -moz-user-select: none; /* 火狐 */
                -webkit-user-select: none; /* 谷歌 */
                -ms-user-select: none; /* IE */
                user-select: none;
            }
        </style>
        <script>
            window.onload = function () {
                ;(function () {
                    var box = document.querySelector('.box')
                    var disX, disY, temp
                    var body = document.querySelector('body')
                    var way1 = document.querySelector('#way1')
                    var way2 = document.querySelector('#way2')

                    box.onmousedown = function (e) {
                        var e = e || window.event // 兼容性寫法
                        disX = e.clientX - this.offsetLeft
                        disY = e.clientY - this.offsetTop

                        temp = document.createElement('div')
                        body.appendChild(temp)
                        temp.classList.add('box')
                        temp.classList.add('box1')
                        // 移動后位置會變,temp 的位置應(yīng)該與 box 位置重合
                        temp.style.left = e.clientX - disX + 'px' // 記得加單位!
                        temp.style.top = e.clientY - disY + 'px'

                        temp.onmousemove = function (e) {
                            var e = e || window.event
                            temp.style.left = e.clientX - disX + 'px' // 記得加單位!
                            temp.style.top = e.clientY - disY + 'px'
                        }
                        temp.onmouseup = function (e) {
                            console.log('end')
                            this.onmousemove = null
                            // 1 則默認(rèn)不發(fā)生實際移動
                            if (way2.checked) {
                                box.style.left = e.clientX - disX + 'px'
                                box.style.top = e.clientY - disY + 'px'
                            }
                            temp.style.display = 'none'
                            this.onmouseup = null
                        }
                    }
                })()
            }
        </script>
    </head>
    <body>
        <div class="box"></div>
        <div class="way-box">
            <p>請選擇拖拽的方式</p>
            <input type="radio" id="way1" name="way" checked />
            <label for="way1">1</label>
            <input type="radio" id="way2" name="way" />
            <label for="way2">2</label>
        </div>
    </body>
</html>

效果展示

有時會卡頓,未解決…

到此這篇關(guān)于html5 拖拽及用 js 實現(xiàn)拖拽的文章就介紹到這了,更多相關(guān)html5 拖拽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

標(biāo)簽:湖北 煙臺 黃山 懷化 湘潭 山南 通遼 賀州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html5 拖拽及用 js 實現(xiàn)拖拽功能的示例代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    荆州市| 乌兰县| 炎陵县| 汽车| 永吉县| 闽侯县| 合阳县| 开阳县| 黄平县| 贡觉县| 永修县| 增城市| 大港区| 瓮安县| 武夷山市| 洛川县| 溧阳市| 宿迁市| 焦作市| 安宁市| 左云县| 常熟市| 玉山县| 东阿县| 桂林市| 保康县| 鲁山县| 烟台市| 苍南县| 墨竹工卡县| 清徐县| 安宁市| 年辖:市辖区| 子洲县| 常德市| 龙岩市| 武冈市| 佛坪县| 亚东县| 高台县| 和政县|