hammerjs

参考:https://www.cnblogs.com/zhuzhenwei918/p/6853258.html
https://www.jianshu.com/p/0b0b9364f967
https://blog.csdn.net/kongjiea/article/details/43016085

使用nz-carousel的时候,报错:

core.js:13611 The "swipeleft" event cannot be bound because Hammer.JS is not loaded and no custom loader has been specified.
core.js:13611 The "swiperight" event cannot be bound because Hammer.JS is not loaded and no custom loader has been specified.
core.js:13611 The "pan" event cannot be bound because Hammer.JS is not loaded and no custom loader has been specified.

所以使用hammerjs,在my-project/src/polyfills.ts 中:

import 'hammerjs';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在单个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实现对图片的放大缩小和移动了。记得根据你的实际需求进行调整和样式的修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值