// listview.vue
<template>
<scroll
:data="data"
ref="listview"
:probe-type="probeType"
:listenScroll="listenScroll"
@scroll="scroll">
...
<div ref="fixed" v-show="fixedTitle">
<div>{{fixedTitle}}</div>
</div>
</scroll>
</template>
<script type="text/ecmascript-6">
import Scroll from 'base/scroll/scroll'
const TITLE_HEIGHT = 28
const ANCHOR_HEIGHT = 18
export default {
...
data() {
return {
scrollY: -1,
currentIndex: 0,
diff: -1
}
},
computed: {
...
fixedTitle() {
if (this.scrollY > 0) {
return ''
}
return this.data[this.currentIndex] ? this.data[this.currentIndex].title : ''
}
},
watch: {
...
scrollY(newY) {
...
for (let i = 0; i < listHeight.length - 1; i++) {
...
if (-newY >= height1 && -newY < height2) {
...
this.diff = height2 + newY
return
}
}
...
},
diff(newVal) {
let fixedTop = (newVal > 0 && newVal < TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0
if (this.fixedTop === fixedTop) {
return
}
this.fixedTop = fixedTop
this.$refs.fixed.style.transform = `translate3d(0,${fixedTop}px,0)`
}
}
}
</script>
运行结果