Browse Source

优化slide

pull/19/head
zyronon 4 years ago
parent
commit
8793b328c8
  1. 60
      src/pages/test/slide.js

60
src/pages/test/slide.js

@ -17,6 +17,8 @@ export default class Slide {
this.pageSize = 10 this.pageSize = 10
this.pageNo = 0 this.pageNo = 0
this.list = [] this.list = []
this.listMap = new Map()
this.loading = false
this.judgeValue = 0 this.judgeValue = 0
this.index = 0 this.index = 0
@ -34,7 +36,7 @@ export default class Slide {
this.isDrawDown = true this.isDrawDown = true
this.config = config this.config = config
this.appInsMap = new Map() this.appInsMap = new Map()
this.getData() this.getData(this.pageNo)
} }
create(template) { create(template) {
@ -47,7 +49,7 @@ export default class Slide {
el.style[args[0]] = args[1] el.style[args[0]] = args[1]
} }
async getData() { async getData(pageNo, init = true) {
// if (this.config.request) { // if (this.config.request) {
// let res = await this.config.request({pageNo: this.pageNo, pageSize: this.pageSize}) // let res = await this.config.request({pageNo: this.pageNo, pageSize: this.pageSize})
// if (res.code === 200) { // if (res.code === 200) {
@ -61,31 +63,42 @@ export default class Slide {
// }) // })
// } // }
// } // }
this.getRecommend().then( this.getRecommend(pageNo).then(
r => { r => {
this.init() init && this.init()
} }
) )
} }
async getRecommend() { async getRecommend(pageNo) {
if (this.config.request) { if (this.config.request) {
let res = await this.config.request({pageNo: this.pageNo, pageSize: this.pageSize})
if (this.listMap.has(pageNo)) return
this.loading = true
let res = await this.config.request({pageNo, pageSize: this.pageSize})
this.loading = true
if (res.code === 200) { if (res.code === 200) {
this.total = res.data.total this.total = res.data.total
this.list = this.list.concat(res.data.list) this.pageNo = pageNo
console.log('请求数据', res.data.list)
this.listMap.set(pageNo, res.data.list)
// this.list = this.list.concat(res.data.list)
} }
} }
} }
init() { init() {
this.list.slice(this.index, this.index + this.virtualTotal).map((v, i) => { this.getList().slice(this.index, this.index + this.virtualTotal).map((v, i) => {
let el = this.getInsEl(v, i, false) let el = this.getInsEl(v, i, false)
this.slideList.appendChild(el) this.slideList.appendChild(el)
}) })
this.setActive() this.setActive()
} }
getList() {
return Array.from(this.listMap.values()).flat()
}
getInsEl(item, index, play = false) { getInsEl(item, index, play = false) {
// console.log('index',index,play) // console.log('index',index,play)
let slideVNode = this.config.render(item, index, play) let slideVNode = this.config.render(item, index, play)
@ -148,16 +161,16 @@ export default class Slide {
if (canSlide) { if (canSlide) {
if (this.isDrawDown) { if (this.isDrawDown) {
if (this.index !== this.total) { if (this.index < this.total - 3) {
// if (this.list.length - this.index <= 3 && this.index < this.total - 3) { if (this.index + 5 > this.getList().length) {
// this.getData() this.getData(this.pageNo + 1, false)
// } }
let addIndex = this.index + 3 let addIndex = this.index + 3
let removeIndex = this.index - 2 let removeIndex = this.index - 2
let res = this.slideList.querySelector(`.slide-item-${addIndex}`) let res = this.slideList.querySelector(`.slide-item-${addIndex}`)
if (!res) { if (!res) {
this.slideList.appendChild(this.getInsEl(this.list[addIndex], addIndex)) this.slideList.appendChild(this.getInsEl(this.getList()[addIndex], addIndex))
} }
let res2 = this.slideList.querySelector(`.slide-item-${removeIndex}`) let res2 = this.slideList.querySelector(`.slide-item-${removeIndex}`)
if (res2) { if (res2) {
@ -174,8 +187,27 @@ export default class Slide {
this.setActive() this.setActive()
} }
} else { } else {
if (this.index !== 0) { if (this.index > 2) {
let addIndex = this.index - 3
let removeIndex = this.index + 2
let res = this.slideList.querySelector(`.slide-item-${addIndex}`)
if (!res) {
this.slideList.insertBefore(this.getInsEl(this.getList()[addIndex], addIndex), this.slideList.firstChild)
}
let res2 = this.slideList.querySelector(`.slide-item-${removeIndex}`)
if (res2) {
this.slideList.removeChild(res2)
}
this.slideList.childNodes.forEach(v => {
this.css(v, 'top', (this.index - 3) * this.height + 'px')
})
}
if (this.index > 0) {
this.index -= 1 this.index -= 1
this.setActive()
} }
} }
} }

Loading…
Cancel
Save