基于vue2.0以及better-scroll实现scroll滑动组件及所实现组件的应用例子
2017-12-20 05:21
726 查看
直接上源码:
组件:scroll.vue,需要先npm install better-scroll
<template>
<div ref="wrapper">
<slot></slot>
</div>
</template>
<script type="text/ecmascript-6">
import BScroll from 'better-scroll'
const DIRECTION_H = 'horizontal'
const DIRECTION_V = 'vertical'
export default {
props: {
probeType: {
type: Number,
default: 1
},
click: {
type: Boolean,
default: false
},
listenScroll: {
type: Boolean,
default: false
},
data: {
type: Array,
default: null
},
pullup: {
type: Boolean,
default: false
},
beforeScroll: {
type: Boolean,
default: false
},
refreshDelay: {
type: Number,
default: 20
},
direction: {
type: String,
default: DIRECTION_V
}
},
mounted() {
setTimeout(() => {
this._initScroll()
}, 20)
},
methods: {
_initScroll() {
if (!this.$refs.wrapper) {
return
}
this.scroll = new BScroll(this.$refs.wrapper, {
probeType: this.probeType,
click: this.click,
eventPassthrough: this.direction === DIRECTION_V ? DIRECTION_H : DIRECTION_V
})
if (this.listenScroll) {
this.scroll.on('scroll', (pos) => {
this.$emit('scroll', pos)
})
}
if (this.pullup) {
this.scroll.on('scrollEnd', () => {
if (this.scroll.y <= (this.scroll.maxScrollY + 50)) {
this.$emit('scrollToEnd')
}
})
}
if (this.beforeScroll) {
this.scroll.on('beforeScrollStart', () => {
this.$emit('beforeScroll')
})
}
},
disable() {
this.scroll && this.scroll.disable()
},
enable() {
this.scroll && this.scroll.enable()
},
refresh() {
this.scroll && this.scroll.refresh()
},
scrollTo() {
this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
},
scrollToElement() {
this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
}
},
watch: {
data() {
setTimeout(() => {
this.refresh()
}, this.refreshDelay)
}
}
}
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
</style>
应用方法:
引入并注册组件:
import Scroll from 'base/scroll/scroll'
export default {
components: {
Scroll
}
}
template结构以及style:
效果:图中轮播图+列表为滚动区域(recommend-content),即需要滚动的内容;对应的class:slider为轮播图,recommend-list为列表。由于轮播图数据以及list数据为动态获取,实际使用请填充你的数据,以上只给出关键结构。
已实现滚动:
组件:scroll.vue,需要先npm install better-scroll
<template>
<div ref="wrapper">
<slot></slot>
</div>
</template>
<script type="text/ecmascript-6">
import BScroll from 'better-scroll'
const DIRECTION_H = 'horizontal'
const DIRECTION_V = 'vertical'
export default {
props: {
probeType: {
type: Number,
default: 1
},
click: {
type: Boolean,
default: false
},
listenScroll: {
type: Boolean,
default: false
},
data: {
type: Array,
default: null
},
pullup: {
type: Boolean,
default: false
},
beforeScroll: {
type: Boolean,
default: false
},
refreshDelay: {
type: Number,
default: 20
},
direction: {
type: String,
default: DIRECTION_V
}
},
mounted() {
setTimeout(() => {
this._initScroll()
}, 20)
},
methods: {
_initScroll() {
if (!this.$refs.wrapper) {
return
}
this.scroll = new BScroll(this.$refs.wrapper, {
probeType: this.probeType,
click: this.click,
eventPassthrough: this.direction === DIRECTION_V ? DIRECTION_H : DIRECTION_V
})
if (this.listenScroll) {
this.scroll.on('scroll', (pos) => {
this.$emit('scroll', pos)
})
}
if (this.pullup) {
this.scroll.on('scrollEnd', () => {
if (this.scroll.y <= (this.scroll.maxScrollY + 50)) {
this.$emit('scrollToEnd')
}
})
}
if (this.beforeScroll) {
this.scroll.on('beforeScrollStart', () => {
this.$emit('beforeScroll')
})
}
},
disable() {
this.scroll && this.scroll.disable()
},
enable() {
this.scroll && this.scroll.enable()
},
refresh() {
this.scroll && this.scroll.refresh()
},
scrollTo() {
this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
},
scrollToElement() {
this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
}
},
watch: {
data() {
setTimeout(() => {
this.refresh()
}, this.refreshDelay)
}
}
}
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
</style>
应用方法:
引入并注册组件:
import Scroll from 'base/scroll/scroll'
export default {
components: {
Scroll
}
}
template结构以及style:
效果:图中轮播图+列表为滚动区域(recommend-content),即需要滚动的内容;对应的class:slider为轮播图,recommend-list为列表。由于轮播图数据以及list数据为动态获取,实际使用请填充你的数据,以上只给出关键结构。
已实现滚动:
相关文章推荐
- vue2.0 better-scroll 实现移动端滑动的示例代码
- 本应用是基于淘宝开放平台(TOP)API以及Google Map JavaScript API的基础上实现的一个自用型淘宝买家应用。
- ionic通讯录点击滑动效果的实现以及$ionicscrolldelegate使用分析
- 详解基于Vue2.0实现的移动端弹窗(Alert, Confirm, Toast)组件
- better-scroll上拉加载 和下拉刷新 的使用(基于vue写成组件的形式)
- 【C++ STL应用与实现】26: 如何使用std::for_each以及基于范围的for循环 (since C++11)
- 使用better-scroll实现slider组件
- 微信小程序例子——如何使用scroll-view组件实现视图垂直滚动
- JFreeChart组件在基于JSP的Web统计图表中的应用与实现
- 基于Ogre的DeferredShading(延迟渲染)的实现以及应用
- vue2.0实现父子通信(基于之前我写的复选框组件)
- unity3D学习【功能实现】之十:关卡制作scroll rect应用以及一些代码
- 基于vue2.0实现音乐/视频播放进度条组件的思路及具体实现方法+代码解释
- 基于socket,实现用户注册,用户登录,以及文件上传的例子
- 歌词数据解析、歌词滚动、歌词进度控制功能的实现(基于js-base64、lyric-parser、better-scroll)
- 基于Ogre的DeferredShading(延迟渲染)的实现以及应用
- 基于session的事务管理组件实现与应用
- ionic实现上拉加载更多(组件 ion-infinite-scroll使用,以及多次加载的问题)