欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實現盒子內拖動方塊移動的示例代碼

 更新時間:2023年08月16日 08:53:38   作者:russo_zhang  
這篇文章主要給大家介紹了如何通過vue實現盒子內拖動方塊移動,文章通過代碼示例講解的非常詳細,具有一定的參考價值,感興趣的小伙伴可以參考閱讀本文

vue實現盒子內拖動方塊移動

本文采用vue2的options api。

1. 創(chuàng)建盒子與方塊

最簡單的父子嵌套盒子,子盒子絕對定位。

<template>
    <div class="Drag">
        <div class="scroll_thumb"></div>
    </div>
</template>
<script>
export default {
    name: "Drag",
};
</script>
<style scoped>
.Drag {
    position: relative;
    width: 1000px;
    height: 600px;
    border: 1px solid #333;
    margin: 100px auto;
}
.Drag .scroll_thumb {
    position: absolute;
    top: 0;
    left: 0;
    width: 20px;
    height: 20px;
    background-color: gold;
}
</style>

2.獲取父盒子在文檔的位置信息

使用getBoundingClientRect API獲取

jcW7ZeMz9iGW5ZIXHfHQN-GoBuIMk3V0S0ZwCwJS6MoBEqFf-o.png

<template>
    <div ref="drag" class="Drag">
        <div class="scroll_thumb"></div>
    </div>
</template>
<script>
export default {
    name: "Drag",
    data() {
        return {
            boxRect: {}, //父盒子位置信息
        };
    },
    mounted() {
        this.setBoxRect();
        // 窗口變化時重新獲取數據
        window.addEventListener("resize", this.setBoxRect);
    },
    beforeDestroy() {
        window.removeEventListener("resize", this.setBoxRect);
    },
    methods: {
        setBoxRect() {
            //獲取父盒子在文檔的位置信息
            this.boxRect = this.$refs.drag.getBoundingClientRect();
        },
    },
}
</script>

3.監(jiān)聽鼠標點擊事件

<template>
    <div ref="drag" class="Drag">
        <div class="scroll_thumb" @mousedown="mousedown"></div>
    </div>
</template>
<script>
export default {
    methods: {
        // 點擊鼠標按鍵觸發(fā)
        mousedown() {
            // 判斷是否是鼠標左鍵
            if (event.button !== 0) return;
            // 監(jiān)聽鼠標移動與彈起事件
            document.addEventListener("mousemove", this.mousemove);
            document.addEventListener("mouseup", this.mouseup);
        },
        // 鼠標移動觸發(fā)
        mousemove(event) {
            // 此為關鍵代碼,下面進行分析
        },
        // 鼠標按鍵彈起時移除監(jiān)聽的時間
        mouseup() {
            this.removeMouseEvent()
        },
        removeMouseEvent() {
            document.removeEventListener("mousemove", this.mousemove);
            document.removeEventListener("mouseup", this.mouseup);
        },
    },
};
</script>

4.計算方塊跟隨鼠標移動

<template>
    <div ref="drag" class="Drag">
        <!-- 動態(tài)計算方塊的top與left -->
        <div
            class="scroll_thumb"
            :style="{
                width: `${thumbW}px`,
                height: `${thumbH}px`,
                top: `${thumbTop}px`,
                left: `${thumbLeft}px`,
            }"
            @mousedown="mousedown"
        ></div>
    </div>
</template>
<script>
export default {
    name: "Drag",
    data() {
        return {
            // 方塊的位置
            thumbTop: 0,
            thumbLeft: 0,
            // 定義方塊的寬高
            thumbW: 20,
            thumbH: 20,
            boxRect: {},
        };
    },
    methods: {
        mousemove(event) {
            // 阻止瀏覽器默認的行為
            event.preventDefault();
            // 停止冒泡
            event.stopPropagation();
            // 方塊定位的值 = 鼠標位置 - 方塊自身寬高/2 - 父盒子左上角位置的值
            // ps1:方塊自身寬高/2,作用是使鼠標的位置始終在方塊的中心點
            // ps2: 父盒子左上角位置的值,是因為event.clientX/Y是采用文檔定位,而thumbLeft是絕對定位,所以要矯正偏移的值
            this.thumbLeft = event.clientX - this.thumbW / 2 - this.boxRect.left;
            this.thumbTop = event.clientY - this.thumbH / 2 - this.boxRect.top;
        },
    },
};
</script>

到此已經能夠實現鼠標拖動了,但是方塊可以拖動到任意位置,下面我們將進行限制。

5.將方塊限制在盒子內移動

<template>
    <div ref="drag" class="Drag">
        <!-- 動態(tài)計算方塊的top與left -->
        <div
            class="scroll_thumb"
            :style="{
                width: `${thumbW}px`,
                height: `${thumbH}px`,
                top: `${thumbTop}px`,
                left: `${thumbLeft}px`,
            }"
            @mousedown="mousedown"
        ></div>
    </div>
</template>
<script>
export default {
    name: "Drag",
    data() {
        return {
            // 方塊的位置
            thumbTop: 0,
            thumbLeft: 0,
            // 定義方塊的寬高
            thumbW: 20,
            thumbH: 20,
            boxRect: {},
        };
    },
    methods: {
        mousemove(event) {
            event.preventDefault();
            event.stopPropagation();
            this.thumbLeft = event.clientX - this.thumbW / 2 - this.boxRect.left;
            this.thumbTop = event.clientY - this.thumbH / 2 - this.boxRect.top;
            // 調用限制
            this.setLimit();
        },
        setLimit() {
            // 獲取方塊的右側與底部的值
            const thumbRight = this.thumbLeft + this.thumbW;
            const thumbBottom = this.thumbTop + this.thumbH;
            // 當方塊的位置超過頂部或左側,將定位的值設置為0
            if (this.thumbLeft <= 0) {
                this.thumbLeft = 0;
            }
            if (this.thumbTop <= 0) {
                this.thumbTop = 0;
            }
            // 當方塊的位置超過底部或者右側,將定位的值設置為父盒子寬高-方塊本身寬高的值
            if (thumbRight >= this.boxRect.width) {
                this.thumbLeft = this.boxRect.width - this.thumbW;
            }
            if (thumbBottom >= this.boxRect.height) {
                this.thumbTop = this.boxRect.height - this.thumbH;
            }
        },
    },
};
</script>

6.完整代碼

<template>
    <div ref="drag" class="Drag">
        <!-- 動態(tài)計算方塊的top與left -->
        <div
            class="scroll_thumb"
            :style="{
                width: `${thumbW}px`,
                height: `${thumbH}px`,
                top: `${thumbTop}px`,
                left: `${thumbLeft}px`,
            }"
            @mousedown="mousedown"
        ></div>
    </div>
</template>
<script>
export default {
    name: "Drag",
    data() {
        return {
            thumbTop: 0,
            thumbLeft: 0,
            thumbW: 20,
            thumbH: 20,
            boxRect: {},
        };
    },
    mounted() {
        this.setBoxRect();
        window.addEventListener("resize", this.setBoxRect);
    },
    beforeDestroy() {
        window.removeEventListener("resize", this.setBoxRect);
        this.removeMouseEvent();
    },
    methods: {
        setBoxRect() {
            this.boxRect = this.$refs.drag.getBoundingClientRect();
        },
        mousedown(event) {
            // 判斷是否是鼠標左鍵
            if (event.button !== 0) return;
            document.addEventListener("mousemove", this.mousemove);
            document.addEventListener("mouseup", this.mouseup);
        },
        mousemove(event) {
            event.preventDefault();
            event.stopPropagation();
            this.thumbLeft = event.clientX - this.thumbW / 2 - this.boxRect.left;
            this.thumbTop = event.clientY - this.thumbH / 2 - this.boxRect.top;
            this.setLimit();
        },
        setLimit() {
            const thumbRight = this.thumbLeft + this.thumbW;
            const thumbBottom = this.thumbTop + this.thumbH;
            if (this.thumbLeft <= 0) {
                this.thumbLeft = 0;
            }
            if (this.thumbTop <= 0) {
                this.thumbTop = 0;
            }
            if (thumbRight >= this.boxRect.width) {
                this.thumbLeft = this.boxRect.width - this.thumbW;
            }
            if (thumbBottom >= this.boxRect.height) {
                this.thumbTop = this.boxRect.height - this.thumbH;
            }
        },
        mouseup() {
            this.removeMouseEvent();
        },
        removeMouseEvent() {
            document.removeEventListener("mousemove", this.mousemove);
            document.removeEventListener("mouseup", this.mouseup);
        },
    },
};
</script>
<style scoped>
.Drag {
    position: relative;
    width: 1000px;
    height: 600px;
    border: 1px solid #333;
    margin: 100px auto;
}
.Drag .scroll_thumb {
    position: absolute;
    background-color: gold;
}
</style>

7.效果

thumb.gif

8.應用

筆者做這個案例是為了實現虛擬列表的滾動條做的技術儲備,還有其他應用或想法歡迎評論區(qū)留言。

以上就是vue實現盒子內拖動方塊移動的示例代碼的詳細內容,更多關于vue實現盒子內方塊移動的資料請關注腳本之家其它相關文章!

相關文章

  • Vue代理報錯404問題及解決(vue配置proxy)

    Vue代理報錯404問題及解決(vue配置proxy)

    這篇文章主要介紹了Vue代理報錯404問題及解決(vue配置proxy),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Vue2中pinia刷新后數據丟失的問題解決

    Vue2中pinia刷新后數據丟失的問題解決

    Pinia是一個Vue.js狀態(tài)管理庫,如果你在組件中修改了store中的數據并刷新了界面,Pinia會將store中的數據重置為初始值,從而導致數據丟失的問題,本文就來介紹一下問題解決,感興趣的可以了解一下
    2023-12-12
  • Vue3.0利用vue-grid-layout插件實現拖拽布局

    Vue3.0利用vue-grid-layout插件實現拖拽布局

    這篇文章主要介紹了Vue3.0利用vue-grid-layout插件實現拖拽布局,工作中難免遇到需要對頁面布局進行拖拽然后改變布局,保存布局,下面文章就圍繞Vue3.0利用vue-grid-layout插件實現拖拽布局的相關資料展開詳細內容,需要的朋友可以參考一下
    2021-11-11
  • uniapp中設置全局頁面背景色的簡單教程

    uniapp中設置全局頁面背景色的簡單教程

    uni-app如何設置頁面全局背景色,其實非常簡單,一句話即可,但有時候也會踩一些坑,這篇文章主要給大家介紹了關于uniapp中設置全局頁面背景色的相關資料,需要的朋友可以參考下
    2023-03-03
  • Vue3中引入使用vant組件的教程詳解

    Vue3中引入使用vant組件的教程詳解

    Vant是一個強大的移動端組件庫,目前Vant 官方提供了 Vue 2 版本,Vue 3 版本和微信小程序版本,本文主要為大家介紹vue3中的vant組件引入使用的方法,希望對大家有所幫助
    2023-10-10
  • Vue中引入svg圖標的兩種方式

    Vue中引入svg圖標的兩種方式

    這篇文章主要給大家介紹了關于Vue中引入svg圖標的兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • Vue?$event作為參數傳遞使用demo

    Vue?$event作為參數傳遞使用demo

    這篇文章主要介紹了Vue?$event作為參數傳遞使用demo詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • vue中echarts3.0自適應的方法

    vue中echarts3.0自適應的方法

    這篇文章主要介紹了vue中echarts3.0自適應,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 在vue中實現echarts隨窗體變化

    在vue中實現echarts隨窗體變化

    這篇文章主要介紹了在vue中實現echarts隨窗體變化,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue - vue.config.js中devServer配置方式

    vue - vue.config.js中devServer配置方式

    今天小編就為大家分享一篇vue - vue.config.js中devServer配置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10

最新評論