Skip to content

Commit

Permalink
fix(tab): 使用原生属性替换超长滚动的offset
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingOnStar committed Aug 19, 2022
1 parent e304f20 commit 0aa89aa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
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(
// 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(
// 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

0 comments on commit 0aa89aa

Please sign in to comment.