Skip to content

Commit 7c047b4

Browse files
committed
fix: trackStyle改为wxs
1 parent f475522 commit 7c047b4

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

src/tabs/tabs.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class Tabs extends SuperComponent {
6868
tabs: [],
6969
currentLabels: [],
7070
currentIndex: -1,
71-
trackStyle: [],
71+
trackOption: { lineWidth: 0, distance: 0, isInit: true },
7272
offset: 0,
7373
scrollLeft: 0,
7474
tabID: '',
@@ -193,7 +193,6 @@ export default class Tabs extends SuperComponent {
193193
},
194194

195195
async setTrack() {
196-
// if (!this.properties.showBottomLine) return;
197196
const { children } = this;
198197
if (!children) return;
199198
const { currentIndex } = this.data;
@@ -226,26 +225,16 @@ export default class Tabs extends SuperComponent {
226225
getObserver(this, `.${name}`).then(() => this.setTrack());
227226
}
228227

229-
const trackLineWidth = await this.getTrackSize();
228+
const lineWidth = await this.getTrackSize();
230229
if (this.data.theme === 'line') {
231-
distance += (rect.width - trackLineWidth) / 2;
230+
distance += (rect.width - lineWidth) / 2;
232231
}
233-
const trackStyle = [
234-
`-webkit-transform: translateX(${distance}px)`,
235-
`transform: translateX(${distance}px)`,
236-
`width:${trackLineWidth}px`,
237-
'opacity: 1',
238-
];
239-
// 初始快速定位track位置
240-
if (this.data.trackStyle.length === 0) {
232+
233+
const isInit = this.previousIndex === undefined;
234+
if (isInit || this.previousIndex !== currentIndex) {
241235
this.previousIndex = currentIndex;
242-
trackStyle.push('transition-duration: 0');
243-
this.setData({ trackStyle });
244-
} else if (this.previousIndex !== currentIndex) {
245-
this.previousIndex = currentIndex;
246-
trackStyle.push('transition-duration: 0.3s');
247-
this.setData({ trackStyle });
248-
}
236+
this.setData({ trackOption: { lineWidth, distance, isInit } });
237+
}
249238
} catch (err) {
250239
this.triggerEvent('error', err);
251240
}

src/tabs/tabs.wxml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<import src="../common/template/badge.wxml" />
22
<import src="../common/template/icon.wxml" />
3-
<wxs src="./tabs.wxs" module="filters" />
3+
<wxs src="./tabs.wxs" module="_tabs" />
44
<wxs src="../common/utils.wxs" module="_" />
55

66
<view style="{{_._style([style, customStyle])}}" class="{{_.cls(classPrefix, [placement])}} class {{prefix}}-class">
@@ -58,7 +58,7 @@
5858
<view
5959
wx:if="{{theme == 'line' && showBottomLine}}"
6060
class="{{_.cls(classPrefix + '__track', [placement])}} {{prefix}}-class-track"
61-
style="{{_._style(trackStyle)}}"
61+
style="{{_tabs.trackStyle(trackOption)}}"
6262
/>
6363
</view>
6464
</scroll-view>
@@ -74,7 +74,7 @@
7474
>
7575
<view
7676
class="{{classPrefix}}__content-inner {{prefix}}-class-content"
77-
style="{{ filters.animate({duration: animation.duration, currentIndex:currentIndex}) }}"
77+
style="{{ _tabs.animate({duration: animation.duration, currentIndex:currentIndex}) }}"
7878
>
7979
<slot />
8080
</view>

src/tabs/tabs.wxs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1-
function animate(options) {
2-
var result = [];
1+
var utils = require('../common/utils.wxs');
32

3+
function animate(options) {
44
if (options.duration) {
5-
result.push('transition-duration: ' + options.duration + 's');
6-
result.push('transform: translate3d( ' + -100 * options.currentIndex + '%,0, 0)');
5+
return utils._style({
6+
'transition-duration': options.duration + 's',
7+
transform: 'translate3d( ' + -100 * options.currentIndex + '%,0, 0)',
8+
});
79
}
10+
return '';
11+
}
812

9-
return result.join(';');
13+
function trackStyle(options) {
14+
if (options.distance) {
15+
return utils._style({
16+
'-webkit-transform': 'translateX(' + options.distance + 'px)',
17+
transform: 'translateX(' + options.distance + 'px)',
18+
'transition-duration': options.isInit ? '0' : '0.3s',
19+
width: options.lineWidth,
20+
opacity: 1,
21+
});
22+
}
23+
return '';
1024
}
1125

1226
module.exports = {
1327
animate: animate,
28+
trackStyle: trackStyle,
1429
};

0 commit comments

Comments
 (0)