vue实现鼠标选中div拖拽缩放功能

实现功能:

鼠标选中一个div,并且有选中样式,点击八个角实现鼠标拖拽div进行缩放

代码如下:

<template>
    <div id="pageBox">
        <div id="container" @click="clickBox" :style="isselect ? selectStyle : ''">
            <div class="point top" v-show="isselect"></div>
            <div class="point left" v-show="isselect"></div>
            <div class="point right" v-show="isselect"></div>
            <div class="point bottom" v-show="isselect"></div>
            <div class="point top-left" v-show="isselect"></div>
            <div class="point top-right" v-show="isselect"></div>
            <div class="point bottom-left" v-show="isselect"></div>
            <div class="point bottom-right" v-show="isselect"></div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            // 页面对象
            pageObj: null,
            // 缩放元素
            zoomBox: null,
            // 是否允许缩放
            resizeable: false,
            // 鼠标按下时的坐标,并在修改尺寸时保存上一个鼠标的位置
            clientX: 0,
            clientY: 0,
            // div可修改的最小宽高
            minW: 8,
            minH: 8,
            // 鼠标按下时的位置,使用n、s、w、e表示
            direc: '',
            isselect: false,
            selectStyle: {
                border: '1px dashed #19be6b',
                cursor: 'pointer'
            }
        }
    },
    created() {},
    mounted() {
        this.pageObj = document.getElementById('pageBox')
    },
    methods: {
        clickBox() {
            console.log('点击选中')
            console.log('可拖拽', this.resizeable)
            this.isselect = true
            // 需要调整尺寸的div
            this.zoomBox = document.getElementById('container')
            // 鼠标按下事件
            this.zoomBox.onmousedown = this.zoomDown
            // 页面监听移动事件
            this.pageObj.onmousemove = this.zoomMove
            // 鼠标松开事件
            this.pageObj.onmouseup = this.zoomUp
        },
        // 鼠标按下
        zoomDown(e) {
            console.log('鼠标按下', e)
            // 去除鼠标默认事件          
            document.oncontextmenu = function(e) {
                e.preventDefault()
            }
            // 阻止冒泡
            e.stopPropagation()
            let oEvent = e || event
            let d = this.getDirection(oEvent)
            // 当位置为四个边和四个角时才开启尺寸修改
            if (d !== '') {
                this.resizeable = true
                this.direc = d
                this.clientX = e.clientX
                this.clientY = e.clientY
            }
        },
        // 缩放移动
        zoomMove(e) {
            let oEvent = e || event
            let d = this.getDirection(oEvent)
            let cursor = ''
            if (d === '') {
                // 默认移动鼠标样式
                cursor = 'move'
            } else {
                // 缩放鼠标样式
                cursor = d + '-resize'
            }
            // 修改鼠标显示效果
            this.zoomBox.style.cursor = cursor
            // 当开启尺寸修改时,鼠标移动会修改div尺寸
            if (this.resizeable) {
                // 鼠标按下的位置在右边,修改宽度
                if (this.direc.indexOf('e') !== -1) {
                    this.zoomBox.style.width = Math.max(this.minW, this.zoomBox.offsetWidth + (e.clientX - this.clientX)) + 'px'
                    this.clientX = e.clientX
                }
                // 鼠标按下的位置在上部,修改高度
                if (this.direc.indexOf('n') !== -1) {
                    this.zoomBox.style.height = Math.max(this.minH, this.zoomBox.offsetHeight + (this.clientY - e.clientY)) + 'px'
                    this.clientY = e.clientY
                }
                // 鼠标按下的位置在底部,修改高度
                if (this.direc.indexOf('s') !== -1) {
                    this.zoomBox.style.height = Math.max(this.minH, this.zoomBox.offsetHeight + (e.clientY - this.clientY)) + 'px'
                    this.clientY = e.clientY
                }

                // 鼠标按下的位置在左边,修改宽度
                if (this.direc.indexOf('w') !== -1) {
                    this.zoomBox.style.width = Math.max(this.minW, this.zoomBox.offsetWidth + (this.clientX - e.clientX)) + 'px'
                    this.clientX = e.clientX
                }
            }
        },
        // 鼠标抬起
        zoomUp() {
            console.log('鼠标抬起')
            this.resizeable = false
            this.isselect = false
            this.zoomBox.onmousedown = null
            this.pageObj.onmousemove = null
            this.pageObj.onmouseup = null
        },
        // 获取鼠标所在div的位置
        getDirection(ev) {
            let dir = ''
            if (ev.target.className.indexOf('top') >= 0) {
                dir += 'n'
            } else if (ev.target.className.indexOf('bottom') >= 0) {
                dir += 's'
            }
            if (ev.target.className.indexOf('left') >= 0) {
                dir += 'w'
            } else if (ev.target.className.indexOf('right') >= 0) {
                dir += 'e'
            }
            if (ev.target.className.indexOf('item') >= 0) {
                dir = 'move'
            }
            return dir
        }
    }
}
</script>

<style lang="less" scoped>
#container {
    width: 200px;
    height: 200px;
    padding: 15px;
    box-sizing: border-box;
    position: relative;
    margin: 40px;
    cursor: default;
    background: #757575;
}
.point{
    width: 10px;
    height: 10px;
    border-radius: 2px;
    border: 1px solid #19be6b;
    position: absolute;
}
.top{
    top: -5px;
    left: 50%;
}
.bottom{
    bottom: -5px;
    left: 50%;
}
.left{
    left: -5px;
    top: 50%;
}
.right{
    right: -5px;
    top: 50%;
}
.top-left{
    top: -5px;
    left: -5px;
}
.bottom-left{
    bottom: -5px;
    left: -5px;
}
.top-right{
    top: -5px;
    right: -5px;
}
.bottom-right{
    bottom: -5px;
    right: -5px;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的多边形绘制 Vue 组件,具有拖拽缩放功能: ``` <template> <div class="polygon" :style="polygonStyles"> <div class="handle top-left" @mousedown="startDrag($event, 'top-left')"></div> <div class="handle top-right" @mousedown="startDrag($event, 'top-right')"></div> <div class="handle bottom-left" @mousedown="startDrag($event, 'bottom-left')"></div> <div class="handle bottom-right" @mousedown="startDrag($event, 'bottom-right')"></div> <canvas ref="canvas" @mousedown="startDraw" @mousemove="draw" @mouseup="endDraw"></canvas> </div> </template> <script> export default { data() { return { drawing: false, points: [], dragHandle: null, mouseStart: null, polygonStyles: { position: 'absolute', top: '0', left: '0', width: '100%', height: '100%', cursor: 'crosshair' } }; }, mounted() { this.resizeCanvas(); window.addEventListener('resize', this.resizeCanvas); }, beforeDestroy() { window.removeEventListener('resize', this.resizeCanvas); }, methods: { resizeCanvas() { const canvas = this.$refs.canvas; canvas.width = canvas.offsetWidth; canvas.height = canvas.offsetHeight; }, startDraw(event) { this.drawing = true; this.mouseStart = this.getMousePosition(event); this.points = [this.mouseStart]; }, draw(event) { if (this.drawing) { const point = this.getMousePosition(event); const canvas = this.$refs.canvas; const context = canvas.getContext('2d'); context.clearRect(0, 0, canvas.width, canvas.height); context.beginPath(); context.moveTo(this.points[0].x, this.points[0].y); for (let i = 1; i < this.points.length; i++) { context.lineTo(this.points[i].x, this.points[i].y); } context.lineTo(point.x, point.y); context.stroke(); } }, endDraw(event) { this.drawing = false; this.mouseStart = null; }, startDrag(event, handle) { this.dragHandle = handle; this.mouseStart = this.getMousePosition(event); }, drag(event) { if (this.dragHandle) { const mouseCurrent = this.getMousePosition(event); const deltaX = mouseCurrent.x - this.mouseStart.x; const deltaY = mouseCurrent.y - this.mouseStart.y; const polygon = this.$el; const polygonRect = polygon.getBoundingClientRect(); const polygonStyles = window.getComputedStyle(polygon); let newWidth = parseInt(polygonStyles.width) + deltaX; let newHeight = parseInt(polygonStyles.height) + deltaY; if (this.dragHandle.includes('left')) { polygon.style.left = (polygonRect.left + deltaX) + 'px'; newWidth = parseInt(polygonStyles.width) - deltaX; } if (this.dragHandle.includes('top')) { polygon.style.top = (polygonRect.top + deltaY) + 'px'; newHeight = parseInt(polygonStyles.height) - deltaY; } polygon.style.width = newWidth + 'px'; polygon.style.height = newHeight + 'px'; this.mouseStart = mouseCurrent; } }, endDrag() { this.dragHandle = null; this.mouseStart = null; }, getMousePosition(event) { const canvas = this.$refs.canvas; const rect = canvas.getBoundingClientRect(); return { x: event.clientX - rect.left, y: event.clientY - rect.top }; } } }; </script> <style> .polygon { position: relative; display: inline-block; } .handle { position: absolute; width: 10px; height: 10px; background-color: #fff; border: 1px solid #000; cursor: move; } .top-left { top: -5px; left: -5px; } .top-right { top: -5px; right: -5px; } .bottom-left { bottom: -5px; left: -5px; } .bottom-right { bottom: -5px; right: -5px; } </style> ``` 这个组件包含一个 `canvas` 元素,可以用鼠标绘制多边形。同时,组件还包含四个拖拽手柄,可以用来缩放多边形。当用户按下鼠标时,可以通过 `mousedown` 事件开始绘制多边形或拖拽多边形。当用户移动鼠标时,可以通过 `mousemove` 事件更新多边形或手柄的位置。当用户松开鼠标时,可以通过 `mouseup` 事件结束绘制或拖拽操作。 注意,这个组件只是一个简单的示例,可能不适用于所有情况。如果需要更高级的多边形绘制和缩放功能,可以考虑使用第三方库,如 Konva.js 或 Fabric.js。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值