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;
    }
相关推荐
m0_616188491 小时前
Vue3 中 Computed 用法
前端·javascript·vue.js
六个点1 小时前
图片懒加载与预加载的实现
前端·javascript·面试
Patrick_Wilson2 小时前
🔥【全网首篇】30分钟带你从0到1搭建基于Lynx的跨端开发环境
前端·react.js·前端框架
逍遥客.2 小时前
uniapp对接打印机和电子秤
javascript·vue.js·uni-app
Moment2 小时前
前端 社招 面筋分享:前端两年都问些啥 ❓️❓️❓️
前端·javascript·面试
Moment2 小时前
一坤时学习 TS 中的装饰器,让你写 NestJS 不再手软 😏😏😏
前端·javascript·面试
子洋2 小时前
AnythingLLM + SearXNG 实现私有搜索引擎代理
前端·人工智能·后端
小满zs2 小时前
React第二十九章(css in js)
前端·react.js
古柳_Deserts_X2 小时前
Manus官方发布视频的1小时后就开始陆续有人注册了相关网站域名!原因就在于「新词新站」这4个字
前端·程序员·创业
YUELEI1182 小时前
vue3 使用sass变量
前端·css·sass