提交 80da005e 编写于 作者: H hdx

long-list: 当 swiper item 首次可见时初始化数据

上级 216b6240
...@@ -66,9 +66,13 @@ ...@@ -66,9 +66,13 @@
family: 'UtsIconsFontFamily', family: 'UtsIconsFontFamily',
source: '/static/fonts/icon-star.ttf' source: '/static/fonts/icon-star.ttf'
}) })
this.loadData()
}, },
methods: { methods: {
init() {
if (this.dataList.length == 0) {
this.loadData()
}
},
loadData() { loadData() {
if (this.loading || this.isEnded) { if (this.loading || this.isEnded) {
return return
...@@ -94,7 +98,7 @@ ...@@ -94,7 +98,7 @@
this.dataList.push(item) this.dataList.push(item)
}) })
if (responseData.data.length <= 0) { if (responseData.data.length == 0) {
this.isEnded = true this.isEnded = true
} else { } else {
this.$currentPage++ this.$currentPage++
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
<swiper class="swiper-view" ref="swiper" :current="swiperIndex" @change="onSwiperChange" <swiper class="swiper-view" ref="swiper" :current="swiperIndex" @change="onSwiperChange"
@transition="onSwiperTransition" @animationfinish="onSwiperAnimationfinish"> @transition="onSwiperTransition" @animationfinish="onSwiperAnimationfinish">
<swiper-item class="swiper-item" v-for="(item, index) in swiperList" :key="index"> <swiper-item class="swiper-item" v-for="(item, index) in swiperList" :key="index">
<long-page :type="item.type"></long-page> <long-page ref="longPage" :type="item.type"></long-page>
</swiper-item> </swiper-item>
</swiper> </swiper>
</view> </view>
...@@ -39,7 +39,8 @@ ...@@ -39,7 +39,8 @@
type SwiperViewItem = { type SwiperViewItem = {
type : string, type : string,
name : string name : string,
init : Boolean,
} }
// 测试数据 // 测试数据
...@@ -98,19 +99,22 @@ ...@@ -98,19 +99,22 @@
this.setSwiperIndex(e.detail.current, false) this.setSwiperIndex(e.detail.current, false)
}, },
onSwiperTransition(e : SwiperTransitionEvent) { onSwiperTransition(e : SwiperTransitionEvent) {
const offsetX = e.detail.dx; const offsetX = e.detail.dx
let moveToIndex = offsetX > 0 ? this.$lastSwiperIndex + 1 : this.$lastSwiperIndex - 1 let moveToIndex = offsetX > 0 ? this.$lastSwiperIndex + 1 : this.$lastSwiperIndex - 1
if (moveToIndex < 0) { moveToIndex = 0 } if (moveToIndex < 0) { moveToIndex = 0 }
if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 } if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 }
const percentage = Math.abs(offsetX) / this.$swiperWidth; const percentage = Math.abs(offsetX) / this.$swiperWidth
const currentSize = this.$swiperTabsRect[this.$lastSwiperIndex]; const currentSize = this.$swiperTabsRect[this.$lastSwiperIndex]
const moveToSize = this.$swiperTabsRect[moveToIndex]; const moveToSize = this.$swiperTabsRect[moveToIndex]
const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage; const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage
const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage; const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage
this.updateTabIndicator(indicatorlineL, indicatorlineW)
this.initSwiperItemPage(moveToIndex)
this.updateTabIndicator(indicatorlineL, indicatorlineW);
//console.log(this.$lastSwiperIndex, moveToIndex, offsetX, this.$swiperWidth, percentage); //console.log(this.$lastSwiperIndex, moveToIndex, offsetX, this.$swiperWidth, percentage);
}, },
onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) { onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
...@@ -134,10 +138,18 @@ ...@@ -134,10 +138,18 @@
} }
this.swiperIndex = index this.swiperIndex = index
this.initSwiperItemPage(index)
if (updateIndicator) { if (updateIndicator) {
this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width) this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width)
} }
}, },
initSwiperItemPage(index : Number) {
if (!this.swiperList[index].init) {
this.swiperList[index].init = true;
(this.$refs["longPage"] as ComponentPublicInstance[])[index].$callMethod('init')
}
},
updateTabIndicator(left : Number, width : Number) { updateTabIndicator(left : Number, width : Number) {
this.swiperIndicatorLineLeft = left this.swiperIndicatorLineLeft = left
this.swiperIndicatorLineWidth = width this.swiperIndicatorLineWidth = width
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册