滑动tab及3d翻页组件

2,442 阅读1分钟

滑动Tab组件(swipe-tab)

基本思路

1.监听以下touch事件

touchstart: //手指放到屏幕上时触发

touchmove: //手指在屏幕上滑动时触发

touchend: //手指离开屏幕时触发

2.计算滑动值并设置

在touchmove事件回调中动态设置容器的transform中的translate3d属性,以达到tab随滑动而移动的效果

3.在touchend回调中设置滑动切换tab的阈值

即在tab滑动超出一定阈值时释放,tab自动切换

在线浏览

推荐手机访问

源码

详见我的github

翻页组件(touch-flipbook

dom结构

<div class="touch-flipbook" ref="touchCon" @touchstart.prevent="TouchStart" @touchmove.prevent="TouchMove"
@touchend="TouchEnd">
    <div class="page p0">
    <!-- /* page有前后两面 */ -->
        <div class="front">0正</div>
        <div class="back">0反</div>
    </div>
    <!-- /* page2为翻页之后显示的*/  -->
    <div class="page p1">
        <div class="front">1正</div>
        <div class="back">1反</div>
    </div>
    <div class="page p2">
        <div class="front">2正</div>
        <div class="back">2反</div>
    </div>
</div>

touch事件的处理与上述滑动Tab的处理方式大致相同,不同在于

此处需动态设置为transform中的rotate属性。

需要注意的有以下几点:

  • 由于front跟back是重叠的,通过翻转之后back也会出现镜像,此时需设置 scaleY(1)来解决镜像问题。
  • z-index需要特别关注,越上面的page的z-index越大,翻页前front的z-index大于back,翻页后相反
  • 每一个page的翻转值需保存起来,方便后续翻页翻回该页的时候修改。

在线浏览

推荐手机访问

源码

详见我的github