微信小程序(uniApp):scroll-view横向滚动实现点击元素滚动到页面中间的效果
<scroll-view scroll-x="true" class="scroll-x x" :scroll-left='scrollLeft' :scroll-with-animation="scrollAnimation" :show-scrollbar="false" :scroll-into-view="seqToView">
<view class="week-list">
<view class="week-item" :class="weekIndex === index ? 'week-item-active' : 'week-item-default'" v-for="(item, index) in weekDayList" :key="index" :id="'seq-' + index" @click="weekClick(index)">{{item}}</view>
</view>
</scroll-view>
onLoad() {
// 滚动至选中序号位置
this.seqToView = 'seq-' + this.weekIndex
},
// 将点击元素移动到中间
moveTo(index) {
const query = uni.createSelectorQuery().in(this)
query
.selectAll(`.week-item`)
.boundingClientRect(rect => {
this.scrollLeft = rect[index].left + rect[index].width / 2 - (this.width1 - this.documentWidth) / 2 - rect[0].left
}).exec()
},
weekClick(index) {
this.topBack()
this.moveTo(index)
this.weekIndex= index
},