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 相关文章推荐
JavaScript控制Session操作方法
Jan 17 Javascript
js opener的使用详解
Jan 11 Javascript
IE、FF浏览器下修改标签透明度
Jan 28 Javascript
js封装可使用的构造函数继承用法分析
Jan 28 Javascript
以jQuery中$.Deferred对象为例讲解promise对象是如何处理异步问题
Nov 13 Javascript
常用的Javascript设计模式小结
Dec 09 Javascript
微信小程序中多个页面传参通信的学习与实践
May 05 Javascript
vue.js的手脚架vue-cli项目搭建的步骤
Aug 30 Javascript
基于vue的换肤功能的示例代码
Oct 10 Javascript
elementUI select组件使用及注意事项详解
May 29 Javascript
angular异步验证防抖踩坑实录
Dec 01 Javascript
在Vue里如何把网页的数据导出到Excel的方法
Sep 30 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
php网页病毒清除类
2014/12/08 PHP
基于laravel belongsTo使用详解
2019/10/18 PHP
js获取单元格自定义属性值的代码(IE/Firefox)
2010/04/05 Javascript
windows系统下简单nodejs安装及环境配置
2013/01/08 NodeJs
Javascript实现返回上一页面并刷新的小例子
2013/12/11 Javascript
2014 HTML5/CSS3热门动画特效TOP10
2014/12/07 Javascript
js基本算法:冒泡排序,二分查找的简单实例
2016/10/08 Javascript
jquery插件bootstrapValidator表单验证详解
2016/12/15 Javascript
js实现简单的计算器功能
2017/01/16 Javascript
React Native之ListView实现九宫格效果的示例
2017/08/02 Javascript
jQuery Collapse1.1.0折叠插件简单使用
2017/08/28 jQuery
mockjs+vue页面直接展示数据的方法
2018/12/19 Javascript
JavaScript设计模式之命令模式实例分析
2019/01/16 Javascript
使用vue-router切换页面时,获取上一页url以及当前页面url的方法
2019/05/06 Javascript
小程序接口的promise化的实现方法
2019/12/11 Javascript
使用JavaScript实现贪吃蛇游戏
2020/09/29 Javascript
[34:44]Liquid vs TNC Supermajor 胜者组 BO3 第二场 6.4
2018/06/05 DOTA
用Python编写一个简单的俄罗斯方块游戏的教程
2015/04/03 Python
使用Python脚本在Linux下实现部分Bash Shell的教程
2015/04/17 Python
Python中使用urllib2模块编写爬虫的简单上手示例
2016/01/20 Python
Python使用poplib模块和smtplib模块收发电子邮件的教程
2016/07/02 Python
对numpy的array和python中自带的list之间相互转化详解
2018/04/13 Python
Django Web开发中django-debug-toolbar的配置以及使用
2018/05/06 Python
Python Django基础二之URL路由系统
2019/07/18 Python
pandas中read_csv、rolling、expanding用法详解
2020/04/21 Python
python logging模块的使用详解
2020/10/23 Python
深入解析HTML5中的Blob对象的使用
2015/09/08 HTML / CSS
中学生家长评语大全
2014/04/16 职场文书
护理专业自荐书
2014/06/04 职场文书
黑暗中的舞者观后感
2015/06/18 职场文书
严以律己专题学习研讨会发言材料
2015/11/09 职场文书
上手简单,功能强大的Python爬虫框架——feapder
2021/04/27 Python
详解MySQL多版本并发控制机制(MVCC)源码
2021/06/23 MySQL
Python re.sub 反向引用的实现
2021/07/07 Python
Nginx隐藏式跳转(浏览器URL跳转后保持不变)
2022/04/07 Servers
Nginx 配置 HTTPS的详细过程
2022/05/30 Servers