vue实现盒子内拖动方块移动

vue实现盒子内拖动方块移动

本文采用vue2的options api。

1. 创建盒子与方块

最简单的父子嵌套盒子,子盒子绝对定位。

html 复制代码
<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获取

html 复制代码
<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.监听鼠标点击事件

html 复制代码
<template>
    <div ref="drag" class="Drag">
        <div class="scroll_thumb" @mousedown="mousedown"></div>
    </div>
</template>

<script>
export default {
    methods: {
        // 点击鼠标按键触发
        mousedown() {
            // 判断是否是鼠标左键
            if (event.button !== 0) return;
            // 监听鼠标移动与弹起事件
            document.addEventListener("mousemove", this.mousemove);
            document.addEventListener("mouseup", this.mouseup);
        },
        // 鼠标移动触发
        mousemove(event) {
            // 此为关键代码,下面进行分析
        },
        // 鼠标按键弹起时移除监听的时间
        mouseup() {
            this.removeMouseEvent()
        },
        removeMouseEvent() {
            document.removeEventListener("mousemove", this.mousemove);
            document.removeEventListener("mouseup", this.mouseup);
        },
    },
};
</script>

4.计算方块跟随鼠标移动

html 复制代码
<template>
    <div ref="drag" class="Drag">
        <!-- 动态计算方块的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.将方块限制在盒子内移动

html 复制代码
<template>
    <div ref="drag" class="Drag">
        <!-- 动态计算方块的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.完整代码

html 复制代码
<template>
    <div ref="drag" class="Drag">
        <!-- 动态计算方块的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.效果

8.应用

笔者做这个案例是为了实现虚拟列表的滚动条做的技术储备,还有其他应用或想法欢迎评论区留言。

相关推荐
程序员爱钓鱼9 分钟前
Go语言泛型-泛型约束与实践
前端·后端·go
前端小巷子11 分钟前
web从输入网址到页面加载完成
前端·面试·浏览器
江城开朗的豌豆11 分钟前
Vue路由动态生成秘籍:让你的链接'活'起来!
前端·javascript·vue.js
晓得迷路了12 分钟前
栗子前端技术周刊第 88 期 - Apache ECharts 6.0 beta、Deno 2.4、Astro 5.11...
前端·javascript·echarts
江城开朗的豌豆17 分钟前
在写vue公用组件的时候,怎么提高可配置性
前端·javascript·vue.js
江城开朗的豌豆18 分钟前
Vue路由跳转的N种姿势,总有一种适合你!
前端·javascript·vue.js
江城开朗的豌豆18 分钟前
Vue路由玩法大揭秘:三种路由模式你Pick谁?
前端·javascript·vue.js
江城开朗的豌豆19 分钟前
Vue路由守卫全攻略:给页面访问装上'安检门'
前端·javascript·vue.js
小磊哥er26 分钟前
【前端工程化】前端组件模版构建那些事
前端
前端 贾公子27 分钟前
monorepo + Turborepo --- 开发应用程序
java·前端·javascript