栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

控制图片缩放移动

控制图片缩放移动

鼠标滚轮缩放图片,双指移动缩放图片
鼠标移动拖动图片,单指移动拖动图片

Vue.component('imgControler', {
    props:{
      imgsrc: String
    },
    data: () => {
        return {
            isWin: isWin,
            imgscale: 3,
            imgLeft: 0,
            imgTop: 0,

            mOnDown: false,
            mLastPosition: {x:0,y:0},

            touchFirst: true,
            isTwoTouch: false,
            touchTwoFirst: true,
            touchLastDistance: 0,
            tLastPositon: {x:0,y:0},
        }
    },
    template: `
    
        
    
    `,
    methods:{
        Down(e){
            this.monDown = true;
            this.mLastPosition.x = e.pageX;
            this.mLastPosition.y = e.pageY;
        },
        Move(e){
            if (isWin) {
                if (this.mOnDown) {
                    let currX = e.pageX,
                    currY = e.pageY;
                    let deltaX = currX - this.mLastPosition.x, deltaY = currY - this.mLastPosition.y;
                    this.imgLeft += deltaX;
                    this.imgTop += deltaY;
                    this.mLastPosition.x = currX;
                    this.mLastPosition.y = currY;
                }       
            } else {
                let touches = e.changedTouches;
                if (touches.length === 1) {
                    if (this.isTwoTouch) {
                        return;
                    }
                    if (this.touchFirst) {
                        this.tLastPositon.x = touches[0].pageX;
                        this.tLastPositon.y = touches[0].pageY;
                        this.touchFirst = false;
                    } else {
                        let deltaX = touches[0].pageX - this.tLastPositon.x;
                        let deltaY = touches[0].pageY - this.tLastPositon.y;
                        this.imgLeft += deltaY;
                        this.imgTop -= deltaX;
                        this.tLastPositon.x = touches[0].pageX;
                        this.tLastPositon.y = touches[0].pageY;
                    }
                } else if (touches.length === 2) {
                    this.isTwoTouch = true;
                    let t1 = { 
                        x: touches[0].pageX,
                        y: touches[0].pageY
                    }, 
                    t2 = {
                        x: touches[1].pageX,
                        y: touches[1].pageY
                    };
                    if (this.touchTwoFirst) {
                        this.touchLastDistance = this.calDistance(t1, t2);
                        this.touchTwoFirst = false;
                    } else {
                        let currDistance = this.calDistance(t1, t2);
                        let factor = 0.1 * (currDistance - this.touchLastDistance);
                        this.setScale(factor);
                        touchLastDistance = currDistance;
                    }
                }
                
            }
                
        },
        calDistance(a, b) {
            return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
        }, 
        Up(e){
            this.monDown = false;
            this.touchFirst = true;
            this.touchTwoFirst = true;
            this.isTwoTouch = false;
            Object.assign(this.mLastPosition, {x: 0,y: 0});
            this.touchLastDistance = 0;
        },
        setScale(delta){
            let imgscale = this.imgscale;
            imgscale += delta / 10;
            this.imgscale = imgscale < 0.2 ? 0.2 : (imgscale > 6 ? 6 : imgscale);
        }
    },
    created(){
    },
    mounted(){
        let self = this;
        if (window.addEventListener)//FF,火狐浏览器会识别该方法
        window.addEventListener('DOMMouseScroll', wheel, false);
        window.onmousewheel = document.onmousewheel = wheel;//W3C
        //统一处理滚轮滚动事件
        function wheel(event){
            var delta = 0;
            if (!event) event = window.event;
            if (event.wheelDelta) {//IE、chrome浏览器使用的是wheelDelta,并且值为“正负120”
                delta = event.wheelDelta/120; 
                if (window.opera) delta = -delta;//因为IE、chrome等向下滚动是负值,FF是正值,为了处理一致性,在此取反处理
            } else if (event.detail) {//FF浏览器使用的是detail,其值为“正负3”
                delta = -event.detail/3;
            }
            if (delta)
                self.setScale(delta);
        }
    },
    updated(){
        // console.log('updated');
    }
})
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/762287.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号