实现功能---如何在vue项目中对页面元素进行放大缩小操作?

前面解决了ios中点击input输入框光标变大,页面放大问题但是后面发现有个页面内容需要放大和缩小!!!

是时候展示新的“技术”了。

一、明确需要完成的功能

文字内容可以放大、缩小

二、解决方法

功能及数据

data() {
    return: {
 // 定义缩放Icon集合常量
            ZoomIcons: [
                { key: 'zoomOut', label: '缩小', value: '缩小' },
                { key: 'zoomIn', label: '放大', value: '放大' },
                { key: 'zoomReload', label: '还原', value: '还原' }
            ],
            zoomScale: 0.9, // 默认的scale设为0.9
            zoomIcon: '', // 点击的图标

   }
}



methods: {
        // 操作页面放大缩小
        handleZoom(key) {
            this.zoomIcon = key;
            if (key === 'zoomOut' && this.zoomScale <= 0.1) {
                this.zoomScale = 0.1;
                return;
            }
            if (key === 'zoomOut' && this.zoomScale > 0.1) {
                console.log('suoxiao1');

                this.zoomScale = (this.zoomScale - 0.1);
                return;
            }
            if (key === 'zoomIn') {
                console.log('fangda');

                this.zoomScale = this.zoomScale + 0.1;
                return;
            }
            this.zoomScale = 0.9

        },
        // 点击显示
        pdfToTop() {
            this.$refs.zoomBox.style.display = 'block';
        }
}

样式类

// 需要放大缩小内容的位置
.bit-right-container {
    /* display: flex;
    justify-content: center;
    align-items: center;
    transform-origin: top; */

    position: absolute;
    width: 100%;
    padding: 10px;
}
// 三个小图标的位置
.bit-wrap-options {
    position: fixed;
    top: 50px;
    right: 16px;
}

.bit-wrap-options .bit-zoom-box {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 32px;
    height: 32px;
    margin-top: 16px;
    background: #409EFF;
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.16);
    border-radius: 50%;
    color: #313C42;
}

.bit-wrap-options .bit-zoom-box:hover {
    background: linear-gradient(180deg, #f78989 0%, #f78989 100%);
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.16);
}

.bit-wrap-options .active {
    background: linear-gradient(180deg, #f78989 0%, #f78989 100%);
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.16);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在单个Vue 3页面使用Hammer.js实现对图片的放大缩小和移动,你可以按照以下步骤进行操作: 1. 安装Hammer.js库: ```bash npm install hammerjs ``` 2. 在你的Vue组件导入Hammer.js和所需的样式: ```vue <script> import Hammer from 'hammerjs'; import 'hammerjs/hammer.css'; export default { mounted() { const mc = new Hammer.Manager(this.$refs.image); mc.add(new Hammer.Pan({ threshold: 0, pointers: 0 })); mc.add(new Hammer.Swipe()).recognizeWith(mc.get('pan')); mc.add(new Hammer.Rotate({ threshold: 0 })).recognizeWith(mc.get('pan')); mc.add(new Hammer.Pinch({ threshold: 0 })).recognizeWith([mc.get('pan'), mc.get('rotate')]); let posX = 0, posY = 0, scale = 1, lastPosX = 0, lastPosY = 0, lastScale = 1; const image = this.$refs.image; const transform = { x: posX, y: posY, scale: scale }; function applyTransform() { const transformString = `translate(${posX}px, ${posY}px) scale(${scale})`; image.style.transform = transformString; image.style.webkitTransform = transformString; } mc.on('panstart panmove', (e) => { posX = lastPosX + e.deltaX; posY = lastPosY + e.deltaY; applyTransform(); }); mc.on('pinchstart pinchmove', (e) => { scale = lastScale * e.scale; applyTransform(); }); mc.on('rotatestart rotatemove', (e) => { const rotation = lastRotation + e.rotation; image.style.transform = `translate(${posX}px, ${posY}px) scale(${scale}) rotate(${rotation}deg)`; }); mc.on('panend', () => { lastPosX = posX; lastPosY = posY; }); mc.on('pinchend', () => { lastScale = scale; }); } } </script> <template> <div> <img ref="image" src="your-image.jpg" alt="Image"> </div> </template> ``` 在上面的代码,我们创建了一个Hammer.js的管理器实例`mc`,并添加了Pan(平移)、Pinch(缩放)和Rotate(旋转)手势。然后,我们监听手势事件,并根据事件的变化来改变图片的位置、缩放和旋转。 最后,在组件的模板,我们使用`ref`指令给图片元素添加一个引用,以便在JavaScript代码使用。 这样,你就可以在Vue 3的单个页面使用Hammer.js实现对图片的放大缩小和移动了。记得根据你的实际需求进行调整和样式的修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值