vue实现在页面拖拽放大缩小div并显示鼠标在div的坐标

1、功能要求:

实现在一个指定区域拖拽div,并可以放大缩小,同时显示鼠标在该div里的坐标,如图可示

缩小并拖动

2、实现

bash 复制代码
     <div class="div_content" ref="div_content">
                <div class="div_image" id="pic"
                    :style="{'zoom':zoom,'transform':'translate('+moveX+'px,'+moveY+'px)',}"
                    @mousedown.stop="mousedown($event)" @mousemove="handleMouseMove" @mouseleave="mouseout"
                   >


                </div>

                <div class="div_image_tool">
                    <img src="..." class="div_add " @click="add_img">
                    <div class="heng"></div>
                    <img src="..." class="div_add div_decrease" @click="dec_img">
                </div>
            </div>
            <div class="div_axirs">X:{{ux}} Y:{{uy}}</div>

js代码

bash 复制代码
data(){return{
                ux: 0,
                uy: 0,
                moveX: 0,//X轴移动的距离

                moveY: 0,

                startx: '',//鼠标的初始位置
                starty: '',
                endx: 0,
                endy: 0,
                zoom: 1,//放大的倍数
}
},
methods:{
    add_img() {

                this.zoom <= 10 ? this.zoom = this.zoom + 0.5 : ''
            },
            dec_img() {

                this.zoom > 0.5 ? this.zoom = this.zoom - 0.5 : ''
            },


         mousedown(e) {
                // 绑定mousemove
                this.startx = this.formatXY(e.pageX / this.zoom); this.starty = this.formatXY(e.pageY / this.zoom)
              

                
                document.onmousemove = (e) => {
                    this.moveX = this.formatXY(e.pageX / this.zoom) - this.startx + this.endx

                    this.moveY = this.formatXY(e.pageY / this.zoom) - this.starty + this.endy
                    e.preventDefault()
                };
                document.onmouseup = (e) => {
                    // 解除绑定mousemove

                    document.onmousemove = null;

                    this.endx = this.moveX

                    this.endy = this.moveY;
                }


            },
                   mouseout() {
                var that = this;
                that.ux = 0;
                that.uy = 0;
            },
  
            handleMouseMove(e) {
                this.getAxis(e)
            },
            getAxis(e) {
 
                this.ux = this.formatXY(e.offsetX / this.zoom);
                this.uy = this.formatXY(e.offsetY / this.zoom);
},
   formatXY(num) {
                return num.toFixed(0)
            },
            }

css

bash 复制代码
  .div_content {
          position: relative;
        width: 600px;
        height: 580px !important;
        margin: 0 20px;
        overflow: hidden;
        background: rgb(230, 229, 229);
    }

    .div_image {
        height: 400px;
        width: 400px;
        background: white;
        margin: 100px auto auto 100px !important;
    }
相关推荐
洛小豆16 小时前
她问我::is-logged 是啥?我说:前面加冒号,就是 Vue 在发暗号
前端·vue.js·面试
我有一棵树17 小时前
前端开发中 SCSS 变量与 CSS 变量的区别与实践选择,—— 两种变量别混为一谈
前端·css·scss
麦麦大数据17 小时前
F024 CNN+vue+flask电影推荐系统vue+python+mysql+CNN实现
vue.js·python·cnn·flask·推荐算法
white-persist17 小时前
JWT 漏洞全解析:从原理到实战
前端·网络·python·安全·web安全·网络安全·系统安全
IT_陈寒17 小时前
React 性能优化:5个实战技巧让首屏加载提升50%,开发者亲测有效!
前端·人工智能·后端
rising start18 小时前
前端基础一、HTML5
前端·html·html5
鬼谷中妖18 小时前
JavaScript 循环与对象:深入理解 for、for...in、for...of、不可枚举属性与可迭代对象
前端
大厂码农老A18 小时前
你打的日志,正在拖垮你的系统:从P4小白到P7专家都是怎么打日志的?
java·前端·后端
im_AMBER18 小时前
CSS 01【基础语法学习】
前端·css·笔记·学习
DokiDoki之父18 小时前
前端速通—CSS篇
前端·css