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>
....@@ -70,9 +71,8 @@
7071 </view>
7172 </view>
7273 </view>
73
- <view @click="goLottery" class="lottery_banner" v-if="info && info.isLottery">
74
- <image class="lottery_bannerImg"
75
- src="https://ossacmall.bluedeer.com.cn/app/static/images/zone_lotteryBanner.gif" mode="widthFix">
74
+ <view @click="goLottery" class="lottery_banner" v-if="info && info.isLottery && info.lotteryBanner">
75
+ <image class="lottery_bannerImg" :src="info.lotteryBanner" mode="widthFix">
7676 </image>
7777 </view>
7878 <view class="tag-img-container" v-if="info.zoneType == 7">
....@@ -589,7 +589,7 @@
589589 showMenu: false,
590590 selectedMenuArr: [],
591591 expireTime: [],
592
- expireTimeUnit: [],
592
+ countdownToken: 0,
593593 isPageShow: false,
594594 loginBackFn: null,
595595 errText: '',
....@@ -1296,6 +1296,9 @@
12961296 })
12971297 },
12981298 validAlert(data) {
1299
+ // 每次重新计算前作废旧的倒计时链,避免多条定时器同时刷新导致数字跳动
1300
+ this.countdownToken++
1301
+ const token = this.countdownToken
12991302 const {
13001303 startTime,
13011304 expireTime
....@@ -1314,17 +1317,17 @@
13141317 let minutes = startTimetamp.getMinutes()
13151318 minutes = minutes < 10 ? '0' + minutes : minutes;
13161319
1317
- this.countdownTimer(startTime.split('-').join('/'), 'start')
1320
+ this.countdownTimer(startTime.split('-').join('/'), 'start', token)
13181321 this.startOrEnd = "后开启"
13191322 return
1320
- }
1321
- }
1322
- if (expireTime) {
1323
- const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1324
- if (expireTimetamp < nowTimestamp) {
1323
+ }
1324
+ }
1325
+ if (expireTime) {
1326
+ const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1327
+ if (expireTimetamp < nowTimestamp) {
13251328 return
1326
- }
1327
- this.countdownTimer(expireTime.split('-').join('/'), 'end')
1329
+ }
1330
+ this.countdownTimer(expireTime.split('-').join('/'), 'end', token)
13281331 this.startOrEnd = "后结束"
13291332 }
13301333 },
....@@ -1462,7 +1465,7 @@
14621465 that.homeTop = that.getHomeTop(0.4)
14631466 }
14641467 that.expireTime = [];
1465
- that.expireTimeUnit = [];
1468
+ that.countdownToken++;
14661469 that.where.page = 1;
14671470 that.getGoodList()
14681471 that.validAlert(res.data)
....@@ -1488,8 +1491,9 @@
14881491 }
14891492 });
14901493 },
1491
- countdownTimer(expireTime, type) {
1494
+ countdownTimer(expireTime, type, token) {
14921495 if (!expireTime) return
1496
+ if (token !== undefined && token !== this.countdownToken) return
14931497 let that = this;
14941498 // 结束时间的时间戳(需传入一个有效的日期对象或时间戳)
14951499 const endTimestamp = new Date(expireTime).getTime();
....@@ -1501,56 +1505,56 @@
15011505 if (type == 'end') {
15021506 if (diff <= 0) {
15031507 this.expireTime = []
1504
- this.expireTimeUnit = []
15051508 return
15061509 } else { }
15071510 } else if (type == 'start') {
15081511 if (diff <= 0) {
15091512 this.expireTime = []
1510
- this.expireTimeUnit = []
15111513 const expireTime2 = that.info.expireTime
15121514 if (expireTime2) {
15131515 const expireTimetamp = new Date(expireTime2.split('-').join('/')).getTime();
15141516 if (expireTimetamp < nowTimestamp) {
15151517 return
15161518 }
1517
- this.countdownTimer(expireTime2.split('-').join('/'), 'end')
1519
+ this.countdownTimer(expireTime2.split('-').join('/'), 'end', token)
15181520 this.startOrEnd = "后结束"
15191521 }
15201522 return
15211523 }
15221524 }
1525
+ const pad = num => (num < 10 ? '0' + num : String(num))
15231526 const res = []
1524
- const unit = []
15251527 const days = Math.floor(diff / (1000 * 60 * 60 * 24))
15261528 diff %= (1000 * 60 * 60 * 24)
1527
- if (days > 0) {
1528
- res.push(days);
1529
- unit.push('日')
1530
- }
15311529 // 转换为小时、分钟、秒
15321530 const hours = Math.floor(diff / (1000 * 60 * 60));
15331531 diff %= (1000 * 60 * 60); // 剩余毫秒数
1534
- if (hours > 0) {
1535
- res.push(hours < 10 ? '0' + hours : hours);
1536
- unit.push('时')
1537
- }
15381532 const minutes = Math.floor(diff / (1000 * 60));
1539
- if (minutes > 0) {
1540
- res.push(minutes < 10 ? '0' + minutes : minutes);
1541
- unit.push('分')
1542
- }
15431533 diff %= (1000 * 60); // 剩余毫秒数
15441534 const seconds = Math.floor(diff / 1000);
1545
- if (seconds > 0) {
1546
- res.push(seconds < 10 ? '0' + seconds : seconds);
1547
- unit.push('秒')
1535
+ // 天数为 0 时不展示,时/分/秒始终展示(补 0),保证倒计时长度稳定
1536
+ if (days > 0) {
1537
+ res.push({
1538
+ digits: pad(days).split(''),
1539
+ unit: '日'
1540
+ })
15481541 }
1542
+ res.push({
1543
+ digits: pad(hours).split(''),
1544
+ unit: '时'
1545
+ })
1546
+ res.push({
1547
+ digits: pad(minutes).split(''),
1548
+ unit: '分'
1549
+ })
1550
+ res.push({
1551
+ digits: pad(seconds).split(''),
1552
+ unit: '秒'
1553
+ })
15491554 this.expireTime = res
1550
- this.expireTimeUnit = unit
15511555 if (this.isPageShow) {
15521556 setTimeout(() => {
1553
- that.countdownTimer(expireTime, type)
1557
+ that.countdownTimer(expireTime, type, token)
15541558 }, 1000)
15551559 }
15561560 },
....@@ -1637,13 +1641,11 @@
16371641 .lottery_banner {
16381642 margin: 15rpx auto;
16391643 width: 690rpx;
1640
- height: 120rpx;
16411644 text-align: center;
16421645 }
16431646
16441647 .lottery_bannerImg {
16451648 width: 690rpx;
1646
- height: 120rpx;
16471649 }
16481650
16491651 .tag-wrap-container {
....@@ -2085,11 +2087,22 @@
20852087 padding: 15rpx;
20862088 padding-left: 30rpx;
20872089 color: #979797;
2090
+ font-size: 26rpx;
20882091
20892092 .num {
2093
+ display: inline-block;
20902094 color: #000000;
20912095 background: #FFE20E;
2092
- padding: 0 10rpx;
2096
+ padding: 0 6rpx;
2097
+ margin: 0 4rpx;
2098
+ vertical-align: middle;
2099
+ }
2100
+
2101
+ /* 每个数字固定宽度,避免倒计时跳动时宽度忽长忽短 */
2102
+ .digit {
2103
+ display: inline-block;
2104
+ width: 19rpx;
2105
+ text-align: center;
20932106 }
20942107 }
20952108 </style>