Jacob
4 天以前 f564a64d3c99b4d300e54e038446227a440bba48
pages/zone/index.vue
....@@ -20,8 +20,9 @@
2020 <view v-else>
2121 <view class="expireTime" v-if="expireTime && expireTime.length">
2222 <text>专区将在</text>
23
- <text v-for="(val, index) in expireTime" :key="index">
24
- <text class="num">{{ val }}</text>{{ expireTimeUnit[index] }}
23
+ <text v-for="(item, index) in expireTime" :key="index">
24
+ <text class="num"><text v-for="(char, ci) in item.digits" :key="ci"
25
+ class="digit">{{ char }}</text></text>{{ item.unit }}
2526 </text>
2627 <text>{{ startOrEnd }}</text>
2728 </view>
....@@ -88,9 +89,8 @@
8889 </view>
8990 </view>
9091 </view>
91
- <view @click="goLottery" class="lottery_banner" v-if="info && info.isLottery">
92
- <image class="lottery_bannerImg"
93
- src="https://ossacmall.bluedeer.com.cn/app/static/images/zone_lotteryBanner.gif" mode="widthFix">
92
+ <view @click="goLottery" class="lottery_banner" v-if="info && info.isLottery && info.lotteryBanner">
93
+ <image class="lottery_bannerImg" :src="info.lotteryBanner" mode="widthFix">
9494 </image>
9595 </view>
9696 <view class="tag-img-container" v-if="info.zoneType == 7">
....@@ -607,7 +607,7 @@
607607 showMenu: false,
608608 selectedMenuArr: [],
609609 expireTime: [],
610
- expireTimeUnit: [],
610
+ countdownToken: 0,
611611 isPageShow: false,
612612 loginBackFn: null,
613613 errText: '',
....@@ -1327,6 +1327,9 @@
13271327 })
13281328 },
13291329 validAlert(data) {
1330
+ // 每次重新计算前作废旧的倒计时链,避免多条定时器同时刷新导致数字跳动
1331
+ this.countdownToken++
1332
+ const token = this.countdownToken
13301333 const {
13311334 startTime,
13321335 expireTime
....@@ -1345,17 +1348,17 @@
13451348 let minutes = startTimetamp.getMinutes()
13461349 minutes = minutes < 10 ? '0' + minutes : minutes;
13471350
1348
- this.countdownTimer(startTime.split('-').join('/'), 'start')
1351
+ this.countdownTimer(startTime.split('-').join('/'), 'start', token)
13491352 this.startOrEnd = "后开启"
13501353 return
1351
- }
1352
- }
1353
- if (expireTime) {
1354
- const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1355
- if (expireTimetamp < nowTimestamp) {
1354
+ }
1355
+ }
1356
+ if (expireTime) {
1357
+ const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1358
+ if (expireTimetamp < nowTimestamp) {
13561359 return
1357
- }
1358
- this.countdownTimer(expireTime.split('-').join('/'), 'end')
1360
+ }
1361
+ this.countdownTimer(expireTime.split('-').join('/'), 'end', token)
13591362 this.startOrEnd = "后结束"
13601363 }
13611364 },
....@@ -1494,7 +1497,7 @@
14941497 that.homeTop = that.getHomeTop(0.4)
14951498 }
14961499 that.expireTime = [];
1497
- that.expireTimeUnit = [];
1500
+ that.countdownToken++;
14981501 that.where.page = 1;
14991502 that.getGoodList()
15001503 that.validAlert(res.data)
....@@ -1520,8 +1523,9 @@
15201523 }
15211524 });
15221525 },
1523
- countdownTimer(expireTime, type) {
1526
+ countdownTimer(expireTime, type, token) {
15241527 if (!expireTime) return
1528
+ if (token !== undefined && token !== this.countdownToken) return
15251529 let that = this;
15261530 // 结束时间的时间戳(需传入一个有效的日期对象或时间戳)
15271531 const endTimestamp = new Date(expireTime).getTime();
....@@ -1533,56 +1537,56 @@
15331537 if (type == 'end') {
15341538 if (diff <= 0) {
15351539 this.expireTime = []
1536
- this.expireTimeUnit = []
15371540 return
15381541 } else { }
15391542 } else if (type == 'start') {
15401543 if (diff <= 0) {
15411544 this.expireTime = []
1542
- this.expireTimeUnit = []
15431545 const expireTime2 = that.info.expireTime
15441546 if (expireTime2) {
15451547 const expireTimetamp = new Date(expireTime2.split('-').join('/')).getTime();
15461548 if (expireTimetamp < nowTimestamp) {
15471549 return
15481550 }
1549
- this.countdownTimer(expireTime2.split('-').join('/'), 'end')
1551
+ this.countdownTimer(expireTime2.split('-').join('/'), 'end', token)
15501552 this.startOrEnd = "后结束"
15511553 }
15521554 return
15531555 }
15541556 }
1557
+ const pad = num => (num < 10 ? '0' + num : String(num))
15551558 const res = []
1556
- const unit = []
15571559 const days = Math.floor(diff / (1000 * 60 * 60 * 24))
15581560 diff %= (1000 * 60 * 60 * 24)
1559
- if (days > 0) {
1560
- res.push(days);
1561
- unit.push('日')
1562
- }
15631561 // 转换为小时、分钟、秒
15641562 const hours = Math.floor(diff / (1000 * 60 * 60));
15651563 diff %= (1000 * 60 * 60); // 剩余毫秒数
1566
- if (hours > 0) {
1567
- res.push(hours < 10 ? '0' + hours : hours);
1568
- unit.push('时')
1569
- }
15701564 const minutes = Math.floor(diff / (1000 * 60));
1571
- if (minutes > 0) {
1572
- res.push(minutes < 10 ? '0' + minutes : minutes);
1573
- unit.push('分')
1574
- }
15751565 diff %= (1000 * 60); // 剩余毫秒数
15761566 const seconds = Math.floor(diff / 1000);
1577
- if (seconds > 0) {
1578
- res.push(seconds < 10 ? '0' + seconds : seconds);
1579
- unit.push('秒')
1567
+ // 天数为 0 时不展示,时/分/秒始终展示(补 0),保证倒计时长度稳定
1568
+ if (days > 0) {
1569
+ res.push({
1570
+ digits: pad(days).split(''),
1571
+ unit: '日'
1572
+ })
15801573 }
1574
+ res.push({
1575
+ digits: pad(hours).split(''),
1576
+ unit: '时'
1577
+ })
1578
+ res.push({
1579
+ digits: pad(minutes).split(''),
1580
+ unit: '分'
1581
+ })
1582
+ res.push({
1583
+ digits: pad(seconds).split(''),
1584
+ unit: '秒'
1585
+ })
15811586 this.expireTime = res
1582
- this.expireTimeUnit = unit
15831587 if (this.isPageShow) {
15841588 setTimeout(() => {
1585
- that.countdownTimer(expireTime, type)
1589
+ that.countdownTimer(expireTime, type, token)
15861590 }, 1000)
15871591 }
15881592 },
....@@ -1669,13 +1673,11 @@
16691673 .lottery_banner {
16701674 margin: 15rpx auto;
16711675 width: 690rpx;
1672
- height: 120rpx;
16731676 text-align: center;
16741677 }
16751678
16761679 .lottery_bannerImg {
16771680 width: 690rpx;
1678
- height: 120rpx;
16791681 }
16801682
16811683 .tag-wrap-container {
....@@ -2159,11 +2161,22 @@
21592161 padding: 15rpx;
21602162 padding-left: 30rpx;
21612163 color: #979797;
2164
+ font-size: 26rpx;
21622165
21632166 .num {
2167
+ display: inline-block;
21642168 color: #000000;
21652169 background: #FFE20E;
2166
- padding: 0 10rpx;
2170
+ padding: 0 6rpx;
2171
+ margin: 0 4rpx;
2172
+ vertical-align: middle;
2173
+ }
2174
+
2175
+ /* 每个数字固定宽度,避免倒计时跳动时宽度忽长忽短 */
2176
+ .digit {
2177
+ display: inline-block;
2178
+ width: 19rpx;
2179
+ text-align: center;
21672180 }
21682181 }
21692182 </style>