<view class='container_box'>
<view class='page_last'>
<block wx:for='{{imgArr}}' wx:key='{{index}}'>
<view
class="photo_item i_{{index}} {{item.isturn?'left':'right'}}"
style="z-index:{{item.zIndex}}"
data-index='{{index}}'
bindtouchstart='touchStart'
bindtouchend='touchEnd'
bindtransitionend='transitionend'
>
<image src='{{item.src}}' mode='aspectFill'></image>
</view>
</block>
</view>
</view>
let dataSource = [
"https://t7.baidu.com/it/u=954153296,2797898137&fm=193&f=GIF",
"https://img0.baidu.com/it/u=919322343,9900989&fm=253&fmt=auto&app=138&f=JPEG?w=728&h=500",
"https://img1.baidu.com/it/u=3512166882,1513526432&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750",
"https://img1.baidu.com/it/u=1635908731,513902843&fm=253&fmt=auto&app=138&f=JPEG?w=507&h=500",
];
Page({
data: {
imgArr: [],
flag: true,
currentIndex: 0,
startX: 0,
},
onLoad: function (options) {
let arr = [];
dataSource.forEach((src, index) => {
let obj = {};
obj.isturn = false;
obj.src = src;
arr.push(obj);
if (!index) {
obj.zIndex = 30;
} else {
obj.zIndex = 10;
}
});
this.setData({
imgArr: arr,
});
},
touchStart(e) {
console.log("touchStart");
this.setData({
startX: e.touches[0].pageX,
});
},
touchEnd(e) {
console.log("touchEnd");
let index = e.currentTarget.dataset.index;
this.setData({
currentIndex: index,
});
if (this.data.startX - e.changedTouches[0].pageX >= 50 && this.data.flag) {
if (index < this.data.imgArr.length - 1) {
this.change(this.data.currentIndex);
} else {
wx.showToast({
icon: "none",
title: "后面没有了",
});
}
} else if (this.data.startX - e.changedTouches[0].pageX <= -50 && this.data.flag) {
if (index > 0) {
this.change(this.data.currentIndex - 1);
} else {
wx.showToast({
icon: "none",
title: "前面没有了",
});
}
}
},
change(index) {
if (this.data.flag) {
this.data.flag = true;
let imgs = this.data.imgArr;
imgs.map((ele, i) => {
if (index == i) {
imgs[i].isturn = !imgs[i].isturn;
imgs[i].zIndex = 30;
} else {
imgs[i].zIndex = 10;
}
});
if (index - 1 >= 0) {
imgs[index - 1].zIndex = 20;
}
if (index + 1 < imgs.length) {
imgs[index + 1].zIndex = 20;
}
this.setData({
imgArr: imgs,
currentIndex: index,
});
}
},
transitionend() {
this.data.flag = true;
},
});
.container_box {
width: 100%;
}
.page_first,
.page_last {
width: 80%;
height: 480rpx;
}
.page_first {
display: flex;
box-sizing: border-box;
padding: 0 20rpx;
text-align: center;
}
.page_last {
position: relative;
}
.photo_item {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
box-sizing: border-box;
border: 1rpx solid #eee;
background-color: #fff;
}
image {
width: 100%;
height: 100%;
}
.left,
.right {
transform-style: preserve-3d;
transform-origin: 0% center;
perspective: 1000;
transition: all 1s ease-in-out;
}
.left {
transform: perspective(2000rpx) rotateY(-180deg);
}
.right {
transform: perspective(2000rpx) rotateY(0deg);
}
.zindex1 {
z-index: 1;
}
.zindex2 {
z-index: 2;
}
.zindex3 {
z-index: 3;
}