预览
代码
<template>
<div :style="style" style="position:relative;">
<div style="position: relative;width:90%;height:30%;" v-for="(item,index) in list" :key="'_'+index">
<img class="imgs" v-for="(url,img_i) in item" :key="img_i" :src="url" @touchstart="touch_start($event,img_i)" @touchmove="touch_move($event,img_i)" @touchend="touch_end($event,img_i)">
</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data() {
return {
style: {},
style01:{},
potition: 0,
imgs: [],
width: 0,
height: 0,
list: [
['https://img0.baidu.com/it/u=925998594,1358415170&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-2-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653277858&t=51e1c2c45186f2ab47a4a33b65346dfd',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp09%2F21031FKU44S6-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653277858&t=d6873329d7be390040bb920e46628709'
],
['https://img0.baidu.com/it/u=925998594,1358415170&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-2-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653277858&t=51e1c2c45186f2ab47a4a33b65346dfd',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp09%2F21031FKU44S6-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653277858&t=d6873329d7be390040bb920e46628709'
],
['https://img0.baidu.com/it/u=925998594,1358415170&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-2-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653277858&t=51e1c2c45186f2ab47a4a33b65346dfd',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp09%2F21031FKU44S6-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653277858&t=d6873329d7be390040bb920e46628709'
],
['https://img0.baidu.com/it/u=925998594,1358415170&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-2-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653277858&t=51e1c2c45186f2ab47a4a33b65346dfd',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp09%2F21031FKU44S6-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653277858&t=d6873329d7be390040bb920e46628709'
],
]
};
},
created() {
let width = window.innerWidth;
let height = window.innerHeight;
this.width = width;
this.height = height;
this.style = { 'width': width+'px', 'height': height+'px'};
document.addEventListener('touchstart', () => {
}, true);
document.addEventListener('touchmove', () => {
}, true);
document.addEventListener('touchend', () => {
}, true);
},
methods: {
touch_start(e) {
this.imgs = e.path[1].querySelectorAll('.imgs');
this.position = parseInt(e.changedTouches[0].clientX);
},
touch_move(e, index) {
let x = parseInt(e.changedTouches[0].clientX);
if (x < this.position) {
this.imgs[index].style.transform = 'translateX(' + parseInt(x - this.position) + 'px)';
}
if (x > this.position) {
this.imgs[index].style.transform = 'translateX(' + parseInt(x - this.position) + 'px)';
}
},
touch_end(e, index) {
let distance = parseInt(e.changedTouches[0].clientX) - this.position;
let step = parseInt(-(this.width / 5));
this.imgs[index].style.transform = 'translateX(0)';
console.log(distance);
if (distance<0 && Math.abs(distance) > step) {
for (let i = 0; i < this.imgs.length; i++) {
if (index == i) {
this.imgs[i].style.zIndex = '-2';
}
if (index != i) {
this.imgs[i].style.zIndex = '0';
}
if (index - 1 == i) {
this.imgs[i].style.zIndex = '1';
}
}
}
if (distance>0 && Math.abs(distance) > step) {
for (let i = 0; i < this.imgs.length; i++) {
if (index == i) {
this.imgs[i].style.zIndex = '-2';
}
if (index != i) {
this.imgs[i].style.zIndex = '1';
}
if (index + 1 == i) {
this.imgs[i].style.zIndex = '0';
}
}
}
},
},
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
.imgs {
height: 50%;
width: 50%;
margin-left: 25%;
margin-top: 30%;
position: absolute;
}
</style>
|