swiper是这么调用的
<!-- 月历 -->
<swiper ref="swiper" :class="[ type === 'month' ? 'cm-calendar-swiper' : 'cm-calendar-swiper-week' ]" circular
:current="swiperIndex"
:duration="aniDuration"
@change="swipeHandler">
<swiper-item v-for="(count, index) in 3" :key="index">
<view class="cm-calendar-main">
<view class="cm-calendar-item" v-for="(day, dayIndex) in dateSet[index]"
:key="dayIndex"
:class="[ day.isHoliday ? 'holiday' : '',
date.rulue === day.rulue && day.calendarType === 'standard' ? 'selected' : '',
day.calendarType === 'prev' || day.calendarType === 'next' ? 'left' : '' ]"
@click="dayClickHandler(day)">
<view class="cm-calendar-item-up" style="height: 26px">
<view class="cm-calendar-day-text cm-text-16">{{ day.day }}</view>
<view class="cm-calendar-ganzhi cm-text-12">{{ day.ganzhi }}</view>
</view>
<view class="cm-text-12 cm-margin-top-3">{{ day.event }}</view>
</view>
</view>
</swiper-item>
</swiper>
以下是cm-calendar组件内部,滑动swiper回调处理日历数据的函数()
不要太在意业务逻辑,主要是几个console.time的位置,重点是“组件动画延时”这个定时器,这个定时器反应了setTimeout的延时时间问题很大。
// 手势滑动触发事件
swipeHandler (e) {
if (this.swiperLock) {
// 当手操滑动对象时,不允许触发自动事件
return
}
console.log(e)
const index = e.detail.current
let sign = 'prev'
if (index === this.nextIndex) {
sign = 'next'
}
// 索引跟进
this.curIndex = index
// swiper控制器跟进
this.swiperIndex = index
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!重点从这里开始!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
console.time('组件动画延时')
console.log('组件动画开始', this.aniDuration)
clearTimeout(this.swiperHandlerTimer)
this.swiperHandlerTimer = setTimeout(() => {
// 动画完成后,再跟进日期和数据
if (this.swiperLock) {
// 双保险,当手操滑动对象时,不允许更新数据
return
}
console.timeEnd('组件动画延时')
console.time('日历数据更新')
// 日期跟进
if (this.type === 'month') {
// 月日期跟进
const tempDate = this.dateSet[this.curIndex][CM.Date.config.DAY_IN_WEEK]
let options = {
year: tempDate.year,
month: tempDate.month,
day: this.date.day,
hour: this.date.hour,
min: this.date.min
}
let newDate = new CM.Date(options)
while (!newDate.enabled && options.day > 0) {
// 遇上无效的,不存在的日期,要向前追溯
options.day--
newDate = new CM.Date(options)
}
this.date = newDate
}
else {
// 周日期跟进
const weekInDay = CM.Date.config.DAY_IN_WEEK
const startRulue = this.dateSet[this.curIndex][0].rulue
const desRulue = startRulue + this.date.weekDay
let options = {
rulue: desRulue,
hour: this.date.hour,
min: this.date.min
}
this.date = new CM.Date(options)
}
// 数据跟进
if (sign === 'next') {
const data = this.type === 'month' ?
CM.Date.getCalendarMonth(this.nextMonth.year, this.nextMonth.month) :
CM.Date.getCalendarWeek(this.date.year, this.date.month, this.date.day, 1)
this.$set(this.dateSet, this.nextIndex, data)
}
else {
const data = this.type === 'month' ?
CM.Date.getCalendarMonth(this.prevMonth.year, this.prevMonth.month) :
CM.Date.getCalendarWeek(this.date.year, this.date.month, this.date.day, -1)
this.$set(this.dateSet, this.prevIndex, data)
}
console.timeEnd('日历数据更新')
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!重点j结束!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
// 触发日期变动事件
this.onDateChange()
}, this.aniDuration)
},