better-scroll的listview组件开发
listview组件在这里设计为可以滚动的页面,右侧具有滚动提示。
<template lang="html">
<scroll class="listview" :data="data"
ref="listview"
:listenScroll="listenScroll"
@scroll="scroll"
:probeType="probeType">
<ul>
<!-- list-view 的原因是L和V大写 -->
<li v-for="group in data" class="list-group" ref="listGroup">
<h2 class="list-group-title">{{group.title}}</h2>
<ul>
<li v-for="item in group.items" class="list-group-item">
<img class="avatar" v-lazy="item.avatar">
<span class="name">{{item.name}}</span>
</li>
</ul>
</li>
</ul>
<div class="list-shortcut" @touchstart="onShortcutTouchStart" @touchmove.stop.prevent="onShortcutTouchMove">
<ul>
<li v-for="(item,index) in shortcutList" class="item"
:class="{'current':currentIndex==index}"
:data-index="index">
{{item}}
</li>
</ul>
</div>
</scroll>
</template>
<script>
import Scroll from 'base/scroll/scroll'
import {
getData
} from 'common/js/dom'
const ANCHOR_HEIGHT = 18
export default {
created() {
this.touch = {}
this.listenScroll = true
this.listHight = []
this.probeType = 3
},
props: {
data: {
type: Array,
default: []
}
},
data() {
return {
scrollY: -1,
currentIndex: 0
}
},
computed: {
shortcutList() {
return this.data.map((group) => {
return group.title.substr(0, 1)
})
}
},
methods: {
//私有方法放下面,绑定事件的放上面
scroll(pos) {
this.scrollY = pos.y
},
onShortcutTouchStart(e) {
console.log("start");
let anchorIndex = getData(e.target, 'index')
let firstTouch = e.touches[0]
this.touch.y1 = firstTouch.pageY
this.touch.anchorIndex = anchorIndex
this._scrollTo(anchorIndex)
},
onShortcutTouchMove(e) {
console.log("move");
let firstTouch = e.touches[0]
this.touch.y2 = firstTouch.pageY
// | 0 为向下取整
let delta = (this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT | 0
let anchorIndex = parseInt(this.touch.anchorIndex) + delta
this._scrollTo(anchorIndex)
},
_scrollTo(index) {
this.$refs.listview.scrollToElement(this.$refs.listGroup[index], 0)
},
_calculateHeight() {
this.listHight = []
const list = this.$refs.listGroup
let height = 0
this.listHight.push(height)
for (let i = 0; i < list.length; i++) {
let item = list[i]
height += item.clientHeight
this.listHight.push(height)
}
}
},
watch: {
data() {
setTimeout(() => {
this._calculateHeight()
}, 20);
},
scrollY(newY) {
const listHeight = this.listHight
for (let i = 0; i < listHeight.length; i++) {
let heightl = listHeight[i]
let height2 = listHeight[i + 1]
if (!height2 || (-newY > heightl && -newY < height2)) {
this.currentIndex = i
return
}
}
this.currentIndex = 0
}
},
components: {
Scroll
}
}
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
@import "~common/stylus/variable"
.listview
position: relative
width: 100%
height: 100%
overflow: hidden
background: $color-background
.list-group
padding-bottom: 30px
.list-group-title
height: 30px
line-height: 30px
padding-left: 20px
font-size: $font-size-small
color: $color-text-l
background: $color-highlight-background
.list-group-item
display: flex
align-items: center
padding: 20px 0 0 30px
.avatar
width: 50px
height: 50px
border-radius: 50%
.name
margin-left: 20px
color: $color-text-l
font-size: $font-size-medium
.list-shortcut
position: absolute
z-index: 30
right: 0
top: 50%
transform: translateY(-50%)
width: 20px
padding: 20px 0
border-radius: 10px
text-align: center
background: $color-background-d
font-family: Helvetica
.item
padding: 3px
line-height: 1
color: $color-text-l
font-size: $font-size-small
&.current
color: $color-theme
.list-fixed
position: absolute
top: 0
left: 0
width: 100%
.fixed-title
height: 30px
line-height: 30px
padding-left: 20px
font-size: $font-size-small
color: $color-text-l
background: $color-highlight-background
.loading-container
position: absolute
width: 100%
top: 50%
transform: translateY(-50%)
</style>
list-shortcut 为右侧的保持不动的滚动提示信息,设置
top: 50%
transform: translateY(-50%)
使其右侧垂直居中。
需要实现以下两个效果:
1 点击右侧页面相应滚动,在右侧滚动页面也同样滚动
2 页面滚动提示相应变化
为了达到第一个效果绑定了@touchstart=”onShortcutTouchStart”touchstart事件,在onShortcutTouchStart默认会传入事件event,获取其中data-index属性,这个属性的值即为元素的Index。之后触发betterscroll的scrollToElement函数即可。
export function getData(el, name, val) {
const prefix = 'data-'
name = prefix + name
if (val) {
return el.setAttribute(name, val)
} else {
return el.getAttribute(name)
}
}
为了实现在右侧提示的滚动页面相应滚动,需要记住滑动的开始和结束坐标,以及开始坐标所在的列表索引,根据开始索引加上移动的索引数即可得到滑动后的索引数,再次触发scrollToElement函数即可。
移动索引数可以用滑动结束y轴坐标减去开始y轴坐标除以每一个索引高度即可。
为了得到滑动结束y轴坐标,绑定@touchmove.stop.prevent=”onShortcutTouchMove”事件,.stop.prevent防止页面相应滚动。滑动索引数
delta = (this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT | 0
| 0为向下取整。由于从属性里取出的Index为字符串,加上移动索引数前要先parseInt。
如果要实现第二个左右联动的效果,那么必须记住页面滚动的位置,算出滚动到哪个区间,再得到这个区间对应右端哪个索引,使其高亮。要监听页面滚动到哪,需要在scroll组件中添加listenScroll事件。
if (this.listenScroll) {
let me = this
this.scroll.on('scroll', (pos) => {
//监听到后向上派发scroll事件
me.$emit('scroll', pos)
})
}
在listview中添加@scroll接收派发的scroll方法,触发listview组件中的scroll方法
<scroll class="listview" :data="data"
ref="listview"
:listenScroll="listenScroll"
//@scroll为接收派发的scroll方法,
@scroll="scroll"
:probeType="probeType">
//scroll方法
scroll(pos) {
this.scrollY = pos.y
},
本地的scroll方法得到scroll组件中传入的y坐标,监听y坐标的值的变化进行相应操作。
watch: {
data() {
//data变化时延时操作的原因是从数据变化到dom变化有延时,变化时重新计算高度
setTimeout(() => {
this._calculateHeight()
}, 20)
},
scrollY(newY) {
const listHeight = this.listHeight
// console.log("length",listHeight.length);
//当滚动到顶部,newY>0
if (newY > 0) {
this.currentIndex = 0
return
}
//在中间部分滚动
for (let i = 0; i < listHeight.length - 1; i++) {
let heightl = listHeight[i]
let height2 = listHeight[i + 1]
if (-newY >= heightl && -newY < height2) {
this.currentIndex = i
this.diff = height2 - (-newY)
return
}
}
//当滚动到底部,-newY大于最后一个元素
this.currentIndex = listHeight.length - 2
},
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)`
}
},
总结联动方式:
- 对于右侧索引滑动连带正文列表滑动,首先右侧绑定touchstart和touchmove.stop.prevent事件,当第一触碰右侧索引栏时,在touchstart事件中可以获取点击的索引,使用better-scroll自带的scrollTo方法即可使右侧滚动到相应位置,同时记录点击的y轴距离y1和当前索引;
当在索引栏滑动时,获取滑动结束的位置y2,(y2-y1)/索引高 得到滑动的索引数,用初始索引加上滑动索引数即可得到右侧滚动到索引值。
- 对于正文列表滑动,在better-scroll中派发出scroll事件可以获取滚动到的y轴坐标,通过计算得到所在的区间,然后设置currentIndex使右侧相应索引值高亮。