vue实现滑动切换效果(仅在手机模式下可用)
2018-02-26 23:37
801 查看
实现滑动时红黄色块左右滑动相应距离,效果如下图
实现过程主要在于实时跟踪手指滑动位置与原位置之间的偏移量,再相应移动红黄块。
红黄块布局如下
back中包含back-l,back-r左右两块,正常情况下为了隐藏其中一块,子模块需要设置display: inline-block,并且宽度都需要设置width: 100%。父模块中设置white-space: nowrap用于处理两个子模块之间的空白。
滑动事件分为三种:touchstart,touchmove,touchEnd,加上prevent避免页面相应滑动。
在touchstart中记录滑动开始点:touchStart(e) {
const touch = e.touches[0]
this.touch.startX = touch.pageX
this.touch.startY = touch.pageY
}touchmove中为滑动过程,手指未离开页面,离开页面时触发touchend。滑动过程中,当横向偏离位置大于纵向偏离位置时认为滑动有效,记录手指偏离位置,相应移动红黄块。touchMove(e) {
console.log("move");
const touch = e.touches[0]
//横向和纵向偏离位置
const deltaX = touch.pageX - this.touch.startX
const deltaY = touch.pageY - this.touch.startY
if (Math.abs(deltaY) > Math.abs(deltaX)) {
return
}
const left = this.currentPlay == 'red' ? 0 : -window.innerWidth
var offsetWidth = Math.min(0, Math.max(-window.innerWidth,left+deltaX))
//记录滑动的距离占屏幕宽度的百分比,如果滑动太少则不切换
this.percent = Math.abs(offsetWidth/window.innerWidth)
//移动红黄块
this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)`
//设置动画时间
this.$refs.back.style["transitionDuration"] = 10
}
计算偏移量时首先需要知道当前偏移位置,如果当前在红块,初始偏移量为0,否则初始偏移量为负的屏幕宽度。初始偏移量加上横向偏移量首先和-window.innerWidth取最大值,-window.innerWidth为最左偏移量。再和0相比较取最小值,偏移量为0或者大于零则不再(向右移动)移动,小于零则可以向左移动。
touchend中处理最终效果,如果滑动距离不大于某一值则恢复原位,否则切换。touchEnd() {
console.log("end");
console.log(this.percent);
let offsetWidth
let percent
//当前为红色,滑动占比大于0.1则切换,否则回到原位置
if(this.currentPlay === 'red'){
if(this.percent > 0.1) {
this.currentPlay = 'yellow'
offsetWidth = -window.innerWidth
} else {
offsetWidth = 0
}
} else {
//当前为黄色,滑动占比大于0.9则切换,否则回到原位置
if(this.percent < 0.9) {
this.currentPlay = 'red'
offsetWidth = 0
} else {
offsetWidth = -window.innerWidth
}
}
//这里的transform是针对最开始的位置而言,而不是移动过程中的位置
this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)`
this.$refs.back.style["transitionDuration"] = 10
}完整代码
实现过程主要在于实时跟踪手指滑动位置与原位置之间的偏移量,再相应移动红黄块。
红黄块布局如下
back中包含back-l,back-r左右两块,正常情况下为了隐藏其中一块,子模块需要设置display: inline-block,并且宽度都需要设置width: 100%。父模块中设置white-space: nowrap用于处理两个子模块之间的空白。
<template lang="html"> <div class="back" @touchstart.prevent="touchStart" @touchmove.prevent="touchMove" @touchend="touchEnd" ref="back"> <div class="back-l" ref="left"></div> <div class="back-r" ref="right"></div> </div> </template> <style scoped lang="stylus" rel="stylesheet/stylus"> .back position: fixed width: 100% height: 100px white-space: nowrap .back-l position: relative vertical-align: top display: inline-block width: 100% height: 100% background-color: red .back-r display: inline-block vertical-align: top position: relative width: 100% height: 100% background-color: yellow </style>父模块监听滑动事件
滑动事件分为三种:touchstart,touchmove,touchEnd,加上prevent避免页面相应滑动。
在touchstart中记录滑动开始点:touchStart(e) {
const touch = e.touches[0]
this.touch.startX = touch.pageX
this.touch.startY = touch.pageY
}touchmove中为滑动过程,手指未离开页面,离开页面时触发touchend。滑动过程中,当横向偏离位置大于纵向偏离位置时认为滑动有效,记录手指偏离位置,相应移动红黄块。touchMove(e) {
console.log("move");
const touch = e.touches[0]
//横向和纵向偏离位置
const deltaX = touch.pageX - this.touch.startX
const deltaY = touch.pageY - this.touch.startY
if (Math.abs(deltaY) > Math.abs(deltaX)) {
return
}
const left = this.currentPlay == 'red' ? 0 : -window.innerWidth
var offsetWidth = Math.min(0, Math.max(-window.innerWidth,left+deltaX))
//记录滑动的距离占屏幕宽度的百分比,如果滑动太少则不切换
this.percent = Math.abs(offsetWidth/window.innerWidth)
//移动红黄块
this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)`
//设置动画时间
this.$refs.back.style["transitionDuration"] = 10
}
计算偏移量时首先需要知道当前偏移位置,如果当前在红块,初始偏移量为0,否则初始偏移量为负的屏幕宽度。初始偏移量加上横向偏移量首先和-window.innerWidth取最大值,-window.innerWidth为最左偏移量。再和0相比较取最小值,偏移量为0或者大于零则不再(向右移动)移动,小于零则可以向左移动。
touchend中处理最终效果,如果滑动距离不大于某一值则恢复原位,否则切换。touchEnd() {
console.log("end");
console.log(this.percent);
let offsetWidth
let percent
//当前为红色,滑动占比大于0.1则切换,否则回到原位置
if(this.currentPlay === 'red'){
if(this.percent > 0.1) {
this.currentPlay = 'yellow'
offsetWidth = -window.innerWidth
} else {
offsetWidth = 0
}
} else {
//当前为黄色,滑动占比大于0.9则切换,否则回到原位置
if(this.percent < 0.9) {
this.currentPlay = 'red'
offsetWidth = 0
} else {
offsetWidth = -window.innerWidth
}
}
//这里的transform是针对最开始的位置而言,而不是移动过程中的位置
this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)`
this.$refs.back.style["transitionDuration"] = 10
}完整代码
<template lang="html"> <div class="back" @touchstart.prevent="touchStart" @touchmove.prevent="touchMove" @touchend="touchEnd" ref="back"> <div class="back-l" ref="left"></div> <div class="back-r" ref="right"></div> </div> </template> <script> export default { data() { return { currentPlay: 'red', percent: 0 } }, created() { this.touch = {} }, methods: { touchStart(e) { const touch = e.touches[0] this.touch.startX = touch.pageX this.touch.startY = touch.pageY }, touchMove(e) { console.log("move"); const touch = e.touches[0] const deltaX = touch.pageX - this.touch.startX const deltaY = touch.pageY - this.touch.startY if (Math.abs(deltaY) > Math.abs(deltaX)) { return } const left = this.currentPlay == 'red' ? 0 : -window.innerWidth var offsetWidth = Math.min(0, Math.max(-window.innerWidth,left+deltaX)) this.percent = Math.abs(offsetWidth/window.innerWidth) this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)` this.$refs.back.style["transitionDuration"] = 10 }, touchEnd() { console.log("end"); console.log(this.percent); let offsetWidth let percent if(this.currentPlay === 'red'){ if(this.percent > 0.1) { this.currentPlay = 'yellow' offsetWidth = -window.innerWidth } else { offsetWidth = 0 } } else { if(this.percent < 0.9) { this.currentPlay = 'red' offsetWidth = 0 } else { offsetWidth = -window.innerWidth } } this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)` this.$refs.back.style["transitionDuration"] = 10 } } } </script> <style scoped lang="stylus" rel="stylesheet/stylus"> .back position: fixed width: 100% height: 100px white-space: nowrap .back-l position: relative vertical-align: top display: inline-block width: 100% height: 100% background-color: red .back-r display: inline-block vertical-align: top position: relative width: 100% height: 100% background-color: yellow </style>
相关文章推荐
- h5用swiper结合angularjs实现手机浏览器左右滑动切换图片的效果
- js实现支持手机滑动切换的轮播图片效果实例
- 【请教问题】kanzi如何实现类似安卓手机桌面滑动切换界面效果?
- js实现支持手机滑动切换的轮播图片效果实例
- 原生JS实现移动端模块的左右滑动切换效果,基于vue、stylus
- 基于Vue实现页面切换左右滑动效果
- 淘宝首页 图片滑动切换效果 基于CSS3的transition方法实现
- 手机页面内容超出屏幕宽度时实现可拖拉滑动效果
- CAGradientLayer(颜色渐变) -- 实现iphone手机屏幕“滑动来解锁”动画效果
- 通过widget来实现手机响铃模式的切换
- android 开发零起步学习笔记(十三):Android 实现多页界面左右滑动切换效果
- jQuery实现的Tab滑动选项卡及图片切换(多种效果)小结
- ViewPager实现多页面滑动切换和动画效果
- vue过渡效果的几种实现方法、各种滑动
- jq实现左侧显示图片右侧文字滑动切换效果
- Android App中使用ViewPager+Fragment实现滑动切换效果
- jQuery实现的点击标题文字切换字体效果示例【测试可用】
- Vue.js实现tab切换效果
- jQuery插件Slider Revolution实现响应动画滑动图片切换效果
- vue-cli 路由 实现类似tab切换效果(vue 2.0)