• 【微信小程序开发(三)】实现卡片的层叠滑动


    效果图是这样的,可以左右滑动

    在这里插入图片描述


    前言

    动画:透明度 + 缩放
    就是swiper的切换效果,那为什么不用swiper呢?
    微信小程序内置的swiper组件,还是挺不友好的。像h5一样的swiper插件在小程序是没有的。


    一、实现左滑右滑事件捕捉

    // 滑动手势开始事件
      startEvent: function(event) {
        if (event.changedTouches[0].pageX) {
          this.data.startPageX = event.changedTouches[0].pageX
        } else {
          this.data.startPageX = event.changedTouches[0].x
        }
      },
      // 滑动手势结束事件
      endEvent: function(event) {
        let endPageX = 0
        if (event.changedTouches[0].pageX) {
          endPageX = event.changedTouches[0].pageX
        } else {
          endPageX = event.changedTouches[0].x
        }
        const moveX = endPageX - this.data.startPageX
        if (Math.abs(moveX) < 20) return
        if (moveX > 0) {
          // 右滑
          this.prev()  //这里写你的右滑方法
        } else {
          // 左滑
          this.next()  //这里写你的左滑方法
        }
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在卡片滑动区域绑定滑动方法

    
    		<div class="slider-container" bindtouchstart="startEvent" bindtouchend="endEvent">
    
    		div>
    
    • 1
    • 2
    • 3
    • 4

    这时候左右滑动事件捕捉完毕,接下来开始写动画

    二、左右滑动用wx.animation

    1. 首先静态布局要完整,接下来才能实施自己的动画

    
    <div class="slider-container" bindtouchstart="startEvent" bindtouchend="endEvent">
    <div class="{{card.isActive ? 'slide-item-active': card_index==cardsData.length-1? 'slide-item-last' : 'slide-item'}}" wx:for="{{cardsData}}" bindtap="cardLinkByType" data-type="{{card.type}}" data-index="{{card.index || 0}}" wx:for-item="card" wx:for-index="card_index" wx:key="unique" animation="{{card.animation}}" style="z-index:{{card.zIndex?card.zIndex:199 - card_index}}" data-idx="{{card_index}}" data-tittle="{{card.title}}">
    	
    	<image src="/images/page_index/card_{{card.image_name}}.jpg" mode='widthFix'>image>
    	<div class="bottom-mask">
    		<div class="title">{{card.title}}div>
    		<div class="desc">{{card.desc}}div>
    		<image src="/images/page_index/btn_play@2x.png" mode='widthFix'>image>
    	div>
    div>
    div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    .slider-container {
      margin-top: -65px;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      z-index: 99;
      height: 800rpx;
      width: 100%;
    }
    
    .slide-item-active {
      width: 630rpx;
      height: 800rpx;
      border-radius: 20rpx;
      overflow: hidden;
      position: absolute;
      transform: translateX(0%);
    }
    .slide-item-last {  /* 注意: // 因为可以左右滑动 所以初始化左边就有一个隐藏的 (这里放的是最后一个,也就是至少有3个卡片)*/
      width: 630rpx;
      height: 800rpx;
      border-radius: 20rpx;
      overflow: hidden;
      position: absolute;
      transform: translateX(-500px);
    }
    .slide-item {
      border-radius: 20rpx;
      overflow: hidden;
      position: absolute;
      right: 30rpx;
      width: 550rpx;
      height: 640rpx;
    }
    
    .slide-item-active image ,.slide-item image,.slide-item-last image{
      width: 100%;
      height: auto;
      display: block;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    2. 循环动画

    原理: 将卡片对象的animation初始化为null , 在需要动画的时候用this.setData 给他赋值动画:

     prev: function() {
        this.animationCardRight()
      },
      next: function() {
        this.animationCardLeft()
      },
      animationCardLeft: function() {
        clearInterval(this.timer);
        let translateX = -500;
        let animation = wx.createAnimation({
          duration: 680,
          timingFunction: "ease",
        });
        animation.translateX(translateX).opacity(0.5).step();
        let animationNext = wx.createAnimation({
          duration: 680,
          timingFunction: "ease",
        });
        animationNext.width('630rpx').height('100%').right('60rpx').step();
        let _index = this.data.currentActiveCardIndex;
        let _nextIndex = this.data.currentActiveCardIndex + 1;
        let _preIndex = this.data.currentActiveCardIndex - 1;
    
        // 现在已经是最后一个 做交接
        if (this.data.currentActiveCardIndex == this.data.cardsData.length - 1) {
          _nextIndex = 0;
        }
        // 当下一个是最后一个的时候 把第一个移动到那里
        let _next_nextIndex = _nextIndex + 1;
        if (_nextIndex == this.data.cardsData.length - 1) {
          _next_nextIndex = 0;
        }
    
        // 把左边的移动到右边下面藏起来
        let animationNextNext = wx.createAnimation({
          duration: 0,
          timingFunction: "ease",
        });
        animationNextNext.width('550rpx').height('640rpx').opacity(1).translateX(0).right('30rpx').step();
        this.setData({
          ['cardsData['+ _index +'].animation']: animation.export(),
          ['cardsData['+ _nextIndex +'].animation']: animationNext.export(),
          ['cardsData['+ _next_nextIndex +'].animation']: animationNextNext.export(),
          ['cardsData['+ _index +'].zIndex']: this.data.zIndexInit,
          ['cardsData['+ _nextIndex +'].zIndex']: this.data.zIndexInit-2,
          ['cardsData['+ _next_nextIndex +'].zIndex']: this.data.zIndexInit-3,
        })
        this.data.zIndexInit--
        this.data.currentActiveCardIndex = _nextIndex
        setTimeout(()=>{
          this.createBanerTimer();
        },680)
      },
      animationCardRight: function() {
        clearInterval(this.timer);
        let translateX = 0;
        let animation = wx.createAnimation({
          duration: 680,
          timingFunction: "ease",
        });
        animation.translateX(translateX).opacity(1).step();
        let animationNext = wx.createAnimation({
          duration: 680,
          timingFunction: "ease",
        });
        animationNext.width('550rpx').height('640rpx').right('30rpx').step();
        let _index = this.data.currentActiveCardIndex;
        let _nextIndex = this.data.currentActiveCardIndex + 1;
        let _preIndex = this.data.currentActiveCardIndex - 1;
        if (this.data.currentActiveCardIndex == 0) {
          _preIndex = this.data.cardsData.length - 1;
          
        }
        let _pre_preIndex = _preIndex - 1;
        if (_preIndex == 0) {
          _pre_preIndex = this.data.cardsData.length - 1;
        }
        // 把再前面的一个移动到左边
        let animationPrePre = wx.createAnimation({
          duration: 0,
          timingFunction: "ease",
        });
        animationPrePre.width('630rpx').height('100%').opacity(0).translateX(-500).right('60rpx').step();
        this.setData({
          ['cardsData['+ _pre_preIndex +'].animation']: animationPrePre.export(),
          ['cardsData['+ _preIndex +'].animation']: animation.export(),
          ['cardsData['+ _index +'].animation']: animationNext.export(),
          ['cardsData['+ _pre_preIndex +'].zIndex']: this.data.zIndexInit+2,
          ['cardsData['+ _preIndex +'].zIndex']: this.data.zIndexInit+1,
          ['cardsData['+ _index +'].zIndex']: this.data.zIndexInit,
        })
        this.data.zIndexInit++
        this.data.currentActiveCardIndex = _preIndex
        setTimeout(()=>{
          this.createBanerTimer();
        },680)
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    总结

    可以在微信搜索【简单心理冥想睡眠】 小程序,首页即可看到效果

  • 相关阅读:
    芒果汁行业调研:2022年市场规模及产量分析
    Elasticsearch7 入门 & 进阶
    css滚动动画网站
    唯物辩证法-条件论
    使用 Vue3 构建 Web Components
    周总结【java项目】
    网络安全笔记-文件包含
    Spire.Office for .NET 7.12.0 2022年最后版本?
    EN 13859-2防水用柔性薄板—CE认证
    关于JAVA中字节码文件版本号、产品版本号及开发版本号的关系
  • 原文地址:https://blog.csdn.net/sinat_29843547/article/details/125844094