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(tab): 修复滑块定位错误的问题 #781

Merged
merged 8 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 23 additions & 22 deletions src/tabs/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export default class Tabs extends SuperComponent {
isScrollY: false,
direction: 'X',
animate: { duration: 0 },
offset: 0,
// offset: 0,
itemId: 'item0',
};

created() {
Expand Down Expand Up @@ -140,19 +141,19 @@ export default class Tabs extends SuperComponent {
}
}

calcScrollOffset(
containerWidth: number,
totalWidth: number,
targetLeft: number,
targetWidth: number,
offset: number,
) {
if (offset + targetLeft > containerWidth / 2) {
const maxOffset = totalWidth - containerWidth;
return Math.min(Math.abs(containerWidth / 2 - targetLeft - offset - targetWidth / 2), maxOffset);
}
return 0;
}
// calcScrollOffset(
LeeJim marked this conversation as resolved.
Show resolved Hide resolved
// containerWidth: number,
// totalWidth: number,
// targetLeft: number,
// targetWidth: number,
// offset: number,
// ) {
// if (offset + targetLeft > containerWidth / 2) {
// const maxOffset = totalWidth - containerWidth;
// return Math.min(Math.abs(containerWidth / 2 - targetLeft - offset - targetWidth / 2), maxOffset);
// }
// return 0;
// }

setTrack() {
if (!this.properties.showBottomLine) return;
Expand All @@ -175,15 +176,15 @@ export default class Tabs extends SuperComponent {
}

if (this.containerWidth) {
const offset = this.calcScrollOffset(
this.containerWidth,
rect.width * res.length,
rect.left,
rect.width,
this.data.offset,
);
// const offset = this.calcScrollOffset(
LeeJim marked this conversation as resolved.
Show resolved Hide resolved
// this.containerWidth,
// rect.width * res.length,
// rect.left,
// rect.width,
// this.data.offset,
// );
this.setData({
offset,
itemId: `item${rect.dataset.index}`,
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/tabs/tabs.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
scroll-x="{{isScrollX}}"
scroll-y="{{isScrollY}}"
scroll-with-animation
scroll-into-view="{{itemId}}"
>
<view class="{{classPrefix}}__nav">
<view
wx:for="{{tabs}}"
wx:key="index"
data-index="{{index}}"
id="{{'item'+index}}"
class="{{classPrefix}}__item {{prefix}}-class-item {{item.disabled ? (prefix + '-is-disabled') : '' }} {{currentIndex === index ? prefix + '-is-active ' + prefix + '-class-active' : ''}}"
bind:tap="onTabTap"
>
Expand Down