<view style="margin-top: 40rpx;" >
<scroll-view class="home-icon" :scroll-left="scrollLeft" scroll-x="true" ref="scroll" style="white-space: nowrap" @scroll="onScroll">
<view id="move">
<block v-for="(item,index) in priceList.price" :key="index">
<view :id="'item-' + index" class="min-icon">
<text>¥{{ item.money }}</text>
<image :src="priceList.img_url" alt="" class="icon-image" />
</view>
</block>
</view>
</scroll-view>
</view>
onScroll(e) {
this.initialScrollLeft = e.target.scrollLeft;
},
//点击滚动
handlerMoney(item,index) {
if(item.cardNum == 0){
item.cardNum++
//总卡数
this.totalCard = this.priceList.price.reduce((total, val) => total + val.cardNum, 0);
// 计算总金额
this.totalMoney = this.priceList.price.reduce((total, val) => total + val.cardNum * val.money, 0);
//去除小数点 this.totalMoney = this.priceList.price.reduce((total, val) => total + Math.floor(val.cardNum * val.money), 0);
}
this.selected.push(index);
this.selected= [...new Set([...this.selected, index])]
// const selectedIndex = this.selected.indexOf(index);
// if (selectedIndex > -1) {
// this.selected.splice(selectedIndex, 1);
// } else {
// this.selected.push(index);
// }
this.currentId = index; //用于来定位元素
var that = this;
if (!this.animation) {
this.animation = true;
uni.createSelectorQuery().select(`#item-${index}`).boundingClientRect(rect => {
const windowWidth = uni.getSystemInfoSync().windowWidth;
const scrollLeft = rect.left - windowWidth / 2 + rect.width / 2;
let targetScrollLeft = this.initialScrollLeft + scrollLeft;
let interval = setInterval(() => {
if (Math.abs(targetScrollLeft - that.scrollLeft) <= 1) {
that.scrollLeft = targetScrollLeft;
clearInterval(interval);
interval = null;
this.animation = false; // 动画结束,修改 animation状态
} else {
that.scrollLeft += (targetScrollLeft - that.scrollLeft) * 0.1;
}
}, 10);
}).exec();
}
},
这一块是最主要的
.home-icon {
height: 100%;
width: 100%;
overflow-x: scroll;
white-space: nowrap;
}
.min-icon {
display: inline-block;
height: 370rpx;
margin-left: 12rpx;
margin-right: 12rpx;
position: relative;
}
.icon-image {
width: 640rpx;
height: 400rpx;
}
.min-icon text:nth-child(1){
color: #fff;
font-size: 60rpx;
position: absolute;
top: 40rpx;
right: 50rpx;
font-weight: 600;
}