mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-08-09 22:49:45 +08:00
parent
ab7d27c53a
commit
45994120a9
@ -24,10 +24,29 @@ Component({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
data: {
|
||||||
|
scrollLeft: 0
|
||||||
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
|
_handleScroll(selectedId) {
|
||||||
|
const query = wx.createSelectorQuery().in(this);
|
||||||
|
query.select(`#item-${selectedId}`).boundingClientRect();
|
||||||
|
query.select('#scroll-view').boundingClientRect();
|
||||||
|
query.select('#scroll-view').scrollOffset();
|
||||||
|
query.exec(res => {
|
||||||
|
this.setData({
|
||||||
|
scrollLeft: res[2].scrollLeft + res[0].left + res[0].width / 2 - res[1].width / 2
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
_handleZanTabChange(e) {
|
_handleZanTabChange(e) {
|
||||||
const selectedId = e.currentTarget.dataset.itemId;
|
const selectedId = e.currentTarget.dataset.itemId;
|
||||||
|
|
||||||
|
if (this.data.scroll) {
|
||||||
|
this._handleScroll(selectedId);
|
||||||
|
}
|
||||||
|
|
||||||
this.setData({
|
this.setData({
|
||||||
selectedId
|
selectedId
|
||||||
});
|
});
|
||||||
|
@ -4,8 +4,11 @@
|
|||||||
>
|
>
|
||||||
<block wx:if="{{ scroll }}">
|
<block wx:if="{{ scroll }}">
|
||||||
<scroll-view
|
<scroll-view
|
||||||
|
id="scroll-view"
|
||||||
class="zan-tab__bd zan-tab__bd--scroll {{ fixed ? 'zan-tab__bd--fixed' : '' }}"
|
class="zan-tab__bd zan-tab__bd--scroll {{ fixed ? 'zan-tab__bd--fixed' : '' }}"
|
||||||
scroll-x="true"
|
scroll-x
|
||||||
|
scroll-with-animation
|
||||||
|
scroll-left="{{ scrollLeft }}"
|
||||||
style="height: {{ height ? height + 'px' : 'auto' }}"
|
style="height: {{ height ? height + 'px' : 'auto' }}"
|
||||||
>
|
>
|
||||||
<template
|
<template
|
||||||
@ -34,6 +37,7 @@
|
|||||||
wx:for="{{ list }}"
|
wx:for="{{ list }}"
|
||||||
wx:key="id"
|
wx:key="id"
|
||||||
class="zan-tab__item {{ selectedId == item.id ? 'zan-tab__item--selected' : '' }}"
|
class="zan-tab__item {{ selectedId == item.id ? 'zan-tab__item--selected' : '' }}"
|
||||||
|
id="item-{{ item.id }}"
|
||||||
data-item-id="{{ item.id }}"
|
data-item-id="{{ item.id }}"
|
||||||
bindtap="_handleZanTabChange"
|
bindtap="_handleZanTabChange"
|
||||||
>
|
>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user