getSOPImages(this.id).then(res => { console.log(res); let files = res.data; if (files.length > 1) { this.setTimer(); } for (let i = 0; i < files.length; i++) { let file = files[i]; file.url = 'http://' + process.env.VUE_APP_MAPCS_SERVER_IP + ':8080' + file.url; this.list.push(file); } })此代码获取图片数据;<div class="content"> <template> <div class="home"> <div class="box" @mouseout="out" @mouseover="over"> <div style="width: 100%;height: 100%;"> <img v-for="(item,index) in list" :ref="item.id" v-show="listIndex === index" :key="index" :src="item.url" alt="" /> </div> <p class="left1" @click="changePage(prevIndex)"> < </p> <ul> <li :class="{color:index == listIndex}" v-for="(item,index) in list" @click="changePage(index)" :key="index" /> </ul> <p class="right1" @click="changePage(nextIndex)"> > </p> </div> </div> </template> </div>此代码是对图片进行展示,请根据以上代码写出图片双指缩放的功能,要求使用hammer.js+vue2,每行添加注释,还要指明在哪调用这个双指缩放的方法
时间: 2023-10-07 16:06:53 浏览: 103
首先,需要在vue2项目中安装hammer.js库:`npm install --save hammerjs`
然后在需要使用双指缩放功能的组件中引入hammer.js和vue2-hammer库:
```javascript
import Hammer from 'hammerjs'
import VueHammer from 'vue2-hammer'
```
在组件中注册VueHammer:
```javascript
export default {
name: 'ImageDisplay',
directives: { VueHammer },
...
}
```
接下来,在mounted钩子函数中初始化hammer.js,并添加双指缩放功能:
```javascript
mounted() {
// 初始化hammer.js
const mc = new Hammer.Manager(this.$el)
// 添加pan和pinch事件
mc.add(new Hammer.Pan({ threshold: 0, pointers: 0 }))
mc.add(new Hammer.Pinch({ threshold: 0 })).recognizeWith([mc.get('pan')])
// 定义初始状态
let posX = 0
let posY = 0
let scale = 1
let lastScale = 1
let lastPosX = 0
let lastPosY = 0
let maxPosX = 0
let maxPosY = 0
let transform = ''
// 监听pan事件
mc.on('pan', (e) => {
posX = e.deltaX + lastPosX
posY = e.deltaY + lastPosY
maxPosX = Math.ceil((scale - 1) * this.$el.clientWidth / 2)
maxPosY = Math.ceil((scale - 1) * this.$el.clientHeight / 2)
if (posX > maxPosX) {
posX = maxPosX
}
if (posX < -maxPosX) {
posX = -maxPosX
}
if (posY > maxPosY) {
posY = maxPosY
}
if (posY < -maxPosY) {
posY = -maxPosY
}
transform = `translate3d(${posX}px, ${posY}px, 0) scale3d(${scale}, ${scale}, 1)`
this.$refs[this.list[this.listIndex].id].style.transform = transform
})
// 监听pinch事件
mc.on('pinch', (e) => {
scale = Math.max(.5, Math.min(lastScale * (e.scale), 5))
transform = `translate3d(${posX}px, ${posY}px, 0) scale3d(${scale}, ${scale}, 1)`
this.$refs[this.list[this.listIndex].id].style.transform = transform
})
// 监听pan结束事件
mc.on('panend', () => {
lastPosX = posX < maxPosX ? posX : maxPosX
lastPosY = posY < maxPosY ? posY : maxPosY
})
// 监听pinch结束事件
mc.on('pinchend', () => {
lastScale = scale
})
}
```
最后,在需要使用双指缩放功能的模板中添加VueHammer指令:
```html
<template>
<div class="home">
<div class="box" @mouseout="out" @mouseover="over" v-vue-hammer>
<div style="width: 100%;height: 100%;">
<img v-for="(item,index) in list" :ref="item.id" v-show="listIndex === index" :key="index" :src="item.url" alt="" />
</div>
<p class="left1" @click="changePage(prevIndex)"> < </p>
<ul>
<li :class="{color:index == listIndex}" v-for="(item,index) in list" @click="changePage(index)" :key="index" />
</ul>
<p class="right1" @click="changePage(nextIndex)"> > </p>
</div>
</div>
</template>
```
这样,双指缩放功能就被添加到了图片展示组件中。需要注意的是,在mounted钩子函数中添加双指缩放功能时,需要根据实际情况修改transform变量的值,以实现正确的图片缩放和拖动效果。
阅读全文