Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tabs): resolve track position #1958

Merged
merged 1 commit into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/tabs/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ export default class Tabs extends SuperComponent {
tabs: [],
currentIndex: -1,
trackStyle: '',
isScrollX: true,
direction: 'X',
offset: 0,
tabID: '',
placement: 'top',
Expand Down Expand Up @@ -95,6 +93,12 @@ export default class Tabs extends SuperComponent {
}

methods = {
onScroll(e) {
const { scrollLeft } = e.detail;
this.setData({
offset: scrollLeft,
});
},
updateTabs(cb) {
const { children } = this;
const tabs = children.map((child: any) => child.data);
Expand Down Expand Up @@ -166,7 +170,7 @@ export default class Tabs extends SuperComponent {
if (!this.properties.showBottomLine) return;
const { children } = this;
if (!children) return;
const { currentIndex, isScrollX, direction } = this.data;
const { currentIndex } = this.data;
if (currentIndex <= -1) return;

try {
Expand All @@ -179,10 +183,10 @@ export default class Tabs extends SuperComponent {

res.forEach((item) => {
if (count < currentIndex) {
distance += isScrollX ? item.width : item.height;
distance += item.width;
count += 1;
}
totalSize += isScrollX ? item.width : item.height;
totalSize += item.width;
});

if (this.containerWidth) {
Expand All @@ -193,18 +197,14 @@ export default class Tabs extends SuperComponent {
});
}

if (isScrollX && this.data.theme === 'line') {
if (this.data.theme === 'line') {
const trackLineWidth = await this.getTrackSize();
distance += (rect.width - trackLineWidth) / 2;
}
let trackStyle = `-webkit-transform: translate${direction}(${distance}px);
transform: translate${direction}(${distance}px);
`;
if (!isScrollX) {
trackStyle += `height: ${rect.height}px;`;
}
this.setData({
trackStyle,
trackStyle: `-webkit-transform: translateX(${distance}px);
transform: translateX(${distance}px);
`,
});
} catch (err) {
this.triggerEvent('error', err);
Expand Down
6 changes: 3 additions & 3 deletions src/tabs/tabs.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
enhanced
enable-flex
scroll-left="{{offset}}"
scroll-x="{{isScrollX}}"
scroll-y="{{isScrollY}}"
scroll-x="{{true}}"
scroll-with-animation
show-scrollbar="{{false}}"
bind:scroll="onScroll"
>
<view class="{{_.cls(classPrefix + '__nav', [placement])}}" aria-role="tablist">
<view
Expand Down Expand Up @@ -68,7 +68,7 @@
>
<view
class="{{classPrefix}}__content-inner {{prefix}}-class-content"
style="{{ filters.animate({duration: animation.duration, currentIndex:currentIndex, direction}) }}"
style="{{ filters.animate({duration: animation.duration, currentIndex:currentIndex}) }}"
>
<slot />
</view>
Expand Down
6 changes: 1 addition & 5 deletions src/tabs/tabs.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ function animate(options) {

if (options.duration) {
result.push('transition-duration: ' + options.duration + 's');
result.push(
options.direction === 'Y'
? ';transform: translate3d( 0,' + -100 * options.currentIndex + '%, 0)'
: ';transform: translate3d( ' + -100 * options.currentIndex + '%,0, 0)',
);
result.push('transform: translate3d( ' + -100 * options.currentIndex + '%,0, 0)');
}

return result.join(';');
Expand Down