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

本文介绍如何在Vue中实现手机模式下的滑动切换效果。通过监听touchstart, touchmove和touchend事件,实时计算手指滑动偏移量,并据此移动红黄块。当横向偏移量大于纵向时认为滑动有效,根据偏移量调整元素位置。在touchend时判断滑动距离,决定是否切换或恢复原位。" 108634047,8764403,堆数据结构详解:实现与应用,"['数据结构', '算法', '堆排序']
摘要由CSDN通过智能技术生成

实现滑动时红黄色块左右滑动相应距离,效果如下图

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

  • 红黄块布局如下
 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%
   
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值