需求vue项目移动端,用户需要对某个盒子双指缩放可以滑动查看内容

方法一:纯手写双指事件

1.HTML

		<div class="box">
      <div class="demo"  style="width: 700px;height: 400px;">
        <img class="img" src="https://img2.baidu.com/it/u=862159221,1723036925&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500">
      </div>
    </div>

2.js

缩放可以css中的transformscalezoom

最后选择zoom,用transform盒子缩放后,滚动条问题滑动不到顶部去,有部分内容查看不了,滑动不了

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data() {
    return {
			zoom:1
    }
  },
  methods:{

  },
  mounted(){
        const content = document.querySelector('.demo'); //获取需要放大缩小盒子
        let firstDistance = 0; // 存放手指开始放上的时,两根手指之间的距离
        // 缩放事件处理
        const getDistance =  (start, stop) => { //计算两根手指之间的距离
            return Math.sqrt(Math.pow(Math.abs(start.x - stop.x), 2) + Math.pow(Math.abs(start.y - stop.y), 2));
        };
        content.addEventListener('touchstart', function (event) { // 获取一次触摸的点
            const touches = event.touches;
            if(touches.length > 1){ //判断是否是两指
                const events1 = touches[0];
                const events2 = touches[1];
                const one = {
                    x:events1.pageX, //第一根手指的横坐标
                    y:events1.pageY, //第一根手指的横坐标
                }; //第一根手指的横坐标
                const two = {
                    x:events2.pageX, //第二根手指的横坐标
                    y:events2.pageY, //第二根手指的横坐标
                }; 
                firstDistance = getDistance(one,two);
            }
            // event.preventDefault();
        });
        document.addEventListener('touchmove', (event) => {
            // event.preventDefault();
            const touches = event.touches;
            if(touches.length>1){
                const events1 = touches[0];
                const events2 = touches[1];
                const one = {
                    x:events1.pageX, //第一根手指的横坐标
                    y:events1.pageY, //第一根手指的横坐标
                }; //第一根手指的横坐标
                const two = {
                    x:events2.pageX, //第二根手指的横坐标
                    y:events2.pageY, //第二根手指的横坐标
                }; 
                const distance = getDistance(one,two);
                let zoom = distance / firstDistance
                // content.style.transform = 'scale('+ zoom +')';
                content.style.zoom = this.zoom  * zoom 
				//设置最大最小zomm
            }
        });
				document.addEventListener('touchend', (event)  => {
					this.zoom = content.style.zoom
				})
  }
}
</script>

方法二:插件Easyscroller

git地址https://github.com/ulesta/easyscroller

npm地址https://www.npmjs.com/package/easyscroller

使用方法

安装npm i easyscroller —save

import { EasyScroller } from 'easyscroller'
mounted() {
 const ele = document.querySelector('#zoomBox'); //缩放盒子

  this.scroller = new EasyScroller(ele, {
    scrollingX: true,
    scrollingY: true,
    zooming: true,
    minZoom: 0.5,    //最小缩放
    maxZoom: 5,    //最大缩放
    zoomLevel: 0.5, //初始值缩放
    bouncing: false,
  });
},
beforeDestroy() {
  this.scroller.destroy(); //销毁
},

原文地址:https://blog.csdn.net/MadSnail00/article/details/131481504

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_29486.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注