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>

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

(0)

相关推荐

  • Vue实现移动端左右滑动效果的方法

    1. 最近得到一个新需求,需要在Vue项目的移动端页面上加上左右滑动效果,在网上查阅资料,最终锁定了vue-touch 2. 下载vue-touch (https://github.com/vuejs/vue-touch/tree/next) 注意:如果Vue是2.x 的版本的话,一定要下next分支上的. 3. 使用: 3.1 npm install vue-touch@next --save 3.2 在main.js 中 引入: import VueTouch from 'vue-touch

  • 基于Vue实现页面切换左右滑动效果

    基于Vue的页面切换左右滑动效果,具体内容如下 HTML文本页面: <template> <div id="app> <transition :name="direction" mode="out-in"> <!--动态获得transition 的name值--> <router-view class="app-view" wechat-title></router-vi

  • vue中选项卡点击切换且能滑动切换功能的实现代码

    具体代码如下所述: <div> <div class="navlist"> <ul> <li class="navli" v-for="(item,index) in navList" :class="{'activeT':nowIndex===index}" @click="tabClick(index)"><i>{{item.name}}<

  • vue中使用better-scroll实现滑动效果及注意事项

    一.首先需要在项目中引入better-scroll 1. 在package.json 直接写入 "better-scroll":"^1.11.1"  版本以github上为准(目前最新) 2.cpnm install  在node_modules  可以查看版本是否安装 3.直接在你的组件里面写入import BScroll from 'better-scroll'; 二.better-scroll优点 1.体验像原生:滚动非常流畅,而且没有滚动条. 2.滚动位置固

  • vue自定义移动端touch事件之点击、滑动、长按事件

    用法: **HTML** <div id="app" class="box" v-tap="vuetouch" //vuetouch为函数名,如没有参数,可直接写函数名 v-longtap="{fn:vuetouch,name:'长按'}" //如果有参数以对象形式传,fn 为函数名 v-swipeleft="{fn:vuetouch,name:'左滑'}" v-swiperight="{f

  • 写一个移动端惯性滑动&回弹Vue导航栏组件 ly-tab

    前段时间写了一个移动端的自适应滑动Vue导航栏组件,觉得有一定实用性,大家可能会用得到(当然有些大佬自己写得更好的话就没必要啦),于是前两天整理了一下,目前已经发布到npm和GitHub上了,点我到npm,点我到GitHub项目 ,有需要的同学可以在项目中 npm install ly-tab -S 或者 yarn add ly-tab 使用,具体用法下面会讲到. 好了,先看看效果吧 好的,开始废话了,实习差不多3个月了,这段时间跟着导师大佬也有接触过一些项目,也学到了不少东西,接触到的项目基本

  • vue实现一个移动端屏蔽滑动的遮罩层实例

    在扯废话浪费大家的时间之前,先上个代码好了,使用vue实现起来很简单-- <div class="overlayer" @touchmove.prevent > </div> 对,就是这么简单,加上@touchmove.prevent就可以屏蔽滑动页面了,然后再和普通的遮罩层一样,加点样式 /*遮罩层*/ .overlayer{ position:fixed; left:0; top:0; width:100%; height:100%; z-index:10;

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

    本文实例为大家分享了vue实现滑动时红黄色块左右滑动相应距离,效果如下图 实现过程主要在于实时跟踪手指滑动位置与原位置之间的偏移量,再相应移动红黄块. 红黄块布局如下 back中包含back-l,back-r左右两块,正常情况下为了隐藏其中一块,子模块需要设置display: inline-block,并且宽度都需要设置width: 100%.父模块中设置white-space: nowrap用于处理两个子模块之间的空白. <template lang="html"> &l

  • Qt实现界面滑动切换效果的思路详解

    目录 一.Qt实现界面滑动切换效果 二. 设计思路 三.主要函数讲解 四.源代码解析 4.1 初始化界面 4.2 上一页滑动效果 4.3  下一页滑动效果 4.4 动画结束处理 五.源码地址 一.Qt实现界面滑动切换效果 效果如下图,滑动效果移动上下屏幕. 二. 设计思路 利用QStackWidget将页面存储起来,因为页面比较少,因此我直接将所有的页面存储在QStachWidget中,如果页面相对较多,可以使用使用使渲染的方式. 然后使用show函数同时展示两个页面的内容,这个很重要,如果使用

  • jQuery实现TAB风格的全国省份城市滑动切换效果代码

    本文实例讲述了jQuery实现TAB风格的全国省份城市滑动切换效果代码.分享给大家供大家参考.具体如下: 这里演示jQuery实现的全国省市菜单,加入了选项卡风格,把全国城市按字母范围归类,鼠标移到某一分类的时候,滑动门展开显示所属的全国省分和城市.特别适合于分类信息网站使用.当然,在需要选择省市的时候,也是可以用的.仔细看了下,菜单里面的城市还比较全,你不用再手动添加城市了. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-tab

  • jQuery和hwSlider实现内容响应式可触控滑动切换效果附源码下载(二)

    今天我们继续内容滑动切换效果的第二部分讲解.如今我们的web开发都要适应移动设备,就是说我们的web页面要在移动设备如手机端也能正常访问,所以我将第一部分的基本切换效果做了加强,增加了响应式和触控滑动效果. 效果展示    源码下载 本文是hwSlider-内容滑动切换效果的第二部分,演示DEMO都是基于第一部分内容的基础上的,所以,如果您还没阅读过第一部分的话,请先移步参阅:基于jQuery和hwSlider实现内容左右滑动切换效果附源码下载(一) 响应式 什么是响应式设计,这里我就不描述了.

  • animate 实现滑动切换效果【实例代码】

    今天和大家分享一下用 animate 实现滑动切换效果的小例子 大家都知道jQuery 提供的有一下几种方法能够实现滑动效果: 1.slideDown() 2.slideUp() 3.slideToggle() 但是以上的滑动不太方便控制其滑动的方向,所以我们还是自己动手写一个吧... 其代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equ

  • jq实现左侧显示图片右侧文字滑动切换效果

    分享一款jQuery左侧图片右侧文字滑动切换代码.这是一款基于jQuery实现的列表图片控制图片滑动切换代码.效果图如下: 下面给大家分享下基于jq实现左侧显示图片右侧文字滑动切换效果,用div+css布置表单借用jq实现特效,具体不多说了,请看下面代码. html代码: <div class="index-new w1200 mt30"> <div class="indexadd mt50 mb60"> <div id="b

  • 基于jQuery和hwSlider实现内容左右滑动切换效果附源码下载(一)

    内容滑动切换应用非常广,常见的有幻灯片焦点图.画廊切换等.随着WEB前端技术的广泛应用,内容滑动切换效果占据着web页面重要地位,因此本站Helloweba特别给广大前端爱好者安排了浅显易懂的内容滑动切换效果的开发教程. 先给大家展示下效果图,感觉还不错请参数实现代码,具体效果如下所示: 效果展示      源码下载 本次教程分三个部分: 1.使用jQuery开发基本的内容滑动切换效果, 2.支持移动端触控自适应的内容滑动切换效果, 3.封装内容滑动切换效果jQuery插件. 本文讲解第一部分,

  • Android开发之使用ViewPager实现图片左右滑动切换效果

    Android中图片的左右切换随处可见,今天我也试着查阅资料试着做了一下,挺简单的一个小Demo,却也发现了一些问题,话不多说,上代码~: 使用了3个xml文件作为ViewPager的滑动page,布局都是相同的,如下只展示其中之一: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/

  • vue实现图片切换效果

    本文实例为大家分享了vue实现图片切换效果的具体代码,供大家参考,具体内容如下 1)v-if/v-show 二者都可以实现让元素隐藏和显示.但是实现原理不同: v-if 是通过将元素从dom树中移除和添加来实现元素的隐藏和显示效果. v-show 是通过修改元素的displace值来实现元素的隐藏和显示效果. 2)v-bind v-bind可以对元素的属性值进行修改. 基于这些背景知识,下面来实现图片切换实例. 功能需求 1)点击左边按钮,显示前一张图片:如果图片是第一张,则隐藏该按钮 2)点击

  • Android如何使用ViewPager2实现页面滑动切换效果

    目录 1.引言 2.实现页面滑动切换 2.1 引入ViewPager2库 2.2 使用ViewPager2 2.3 构建Fragment 2.4 继承FragmentStateAdapter 2.5 将ViewPager2与适配器绑定 2.6 垂直方向滑动切换 2.7 Fragment更新 3.总结 1.引言 在很多应用中,我们经常会看到多个页面之间滑动切换的场景,ViewPager2是ViewPager的升级版,本文将简要介绍如何使用ViewPager2.FragmentStateAdapte

随机推荐