vue实现滑动切换效果(仅在手机模式下可用)


Posted in Javascript onJune 29, 2020

本文实例为大家分享了vue实现滑动时红黄色块左右滑动相应距离,效果如下图

vue实现滑动切换效果(仅在手机模式下可用)

实现过程主要在于实时跟踪手指滑动位置与原位置之间的偏移量,再相应移动红黄块。

红黄块布局如下

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
textContent在Firefox下与innerText等效的属性
May 12 Javascript
JavaScript中window.open用法实例详解
Apr 15 Javascript
checkbox 选中一个另一个checkbox也会选中的实现代码
Jul 09 Javascript
jQuery动态生成Bootstrap表格
Nov 01 Javascript
详解基于angular-cli配置代理解决跨域请求问题
Jul 05 Javascript
微信小程序之电影影评小程序制作代码
Aug 03 Javascript
vuejs使用$emit和$on进行组件之间的传值的示例
Oct 04 Javascript
jQuery发请求传输中文参数乱码问题的解决方案
May 22 jQuery
React降级配置及Ant Design配置详解
Dec 27 Javascript
微信小程序动画组件使用解析,类似vue,且更强大
Aug 01 Javascript
node 解析图片二维码的内容代码实例
Sep 11 Javascript
javascript使用canvas实现饼状图效果
Sep 08 Javascript
微信小程序设置滚动条过程详解
Jul 25 #Javascript
vuejs移动端实现div拖拽移动
Jul 25 #Javascript
vue实现拖拽的简单案例 不超出可视区域
Jul 25 #Javascript
vue实现一拉到底的滑动验证
Jul 25 #Javascript
微信小程序实现图片选择并预览功能
Jul 25 #Javascript
详细教你微信公众号正文页SVG交互开发技巧
Jul 25 #Javascript
微信小程序绘制图片发送朋友圈
Jul 25 #Javascript
You might like
信用卡效验程序
2006/10/09 PHP
如何用php生成扭曲及旋转的验证码图片
2013/06/07 PHP
ThinkPHP控制器间实现相互调用的方法
2014/10/31 PHP
php遍历替换目录下文件指定内容的方法
2016/11/10 PHP
载入jQuery库的最佳方法详细说明及实现代码
2012/12/28 Javascript
jquery入门—访问DOM对象方法
2013/01/07 Javascript
nodejs批量修改文件编码格式
2015/01/22 NodeJs
JQuery页面地址处理插件jqURL详解
2015/05/03 Javascript
jQuery实现为图片添加镜头放大效果的方法
2015/06/25 Javascript
浅谈jquery.fn.extend与jquery.extend区别
2015/07/13 Javascript
极易被忽视的javascript面试题七问七答
2016/02/15 Javascript
非常酷炫的Bootstrap图片轮播动画
2016/05/27 Javascript
Javascript中的数组常用方法解析
2016/06/17 Javascript
JS实现的表头列头固定页面功能示例
2017/01/10 Javascript
详解JS中遍历语法的比较
2017/04/07 Javascript
微信小程序自定义键盘 内部虚拟支付
2018/12/20 Javascript
弱类型语言javascript中 a,b 的运算实例小结
2019/08/07 Javascript
JavaScript 反射和属性赋值实例解析
2019/10/28 Javascript
vue 中的 render 函数作用详解
2020/02/28 Javascript
axios解决高并发的方法:axios.all()与axios.spread()的操作
2020/11/09 Javascript
python 切片和range()用法说明
2013/03/24 Python
使用Python对微信好友进行数据分析
2018/06/27 Python
Centos下实现安装Python3.6和Python2共存
2018/08/15 Python
Python元组常见操作示例
2019/02/19 Python
python实现机器人卡牌
2019/10/06 Python
利用Pytorch实现简单的线性回归算法
2020/01/15 Python
高档奢华时装在线目的地:FORWARD by elyse walker
2017/10/16 全球购物
法律进社区实施方案
2014/03/21 职场文书
大学社团活动总结
2014/04/26 职场文书
师范大学生求职信
2014/06/13 职场文书
教师年度考核个人总结
2015/02/12 职场文书
2015大学生求职信范文
2015/03/20 职场文书
2016公司年会通知范文
2015/04/25 职场文书
学生病假条怎么写
2015/08/17 职场文书
民政局2016年“六一”儿童节慰问活动总结
2016/04/06 职场文书
python ConfigParser库的使用及遇到的坑
2022/02/12 Python