小程序数字滚动效果

2021/9/15 12:34:45

本文主要是介绍小程序数字滚动效果,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 效果图

 

wxml

<view class="scroll-number container-class">
  <block wx:for="{{valArr}}" wx:key="index">
    <view wx:if="{{item.isNaN}}" class="scroll-number-item number-dot dot-class">{{item.val}}</view>
    <view wx:else class="scroll-number-item number-item item-class" >
      <view class="scroll-ani" style="transition-duration: {{duration}}ms; {{aniArr[index]}}">
        <view wx:for="{{numArr}}" wx:for-item="num" wx:for-index="idx" wx:key="idx" class="num{{num}}">{{num}}</view>
        <view wx:for="{{numArr}}" wx:for-item="num" wx:for-index="idx" wx:key="idx" class="num{{num}}">{{num}}</view>
      </view>
    </view>
  </block>
</view>

js

Component({
  externalClasses: ['container-class', 'item-class', 'dot-class'],
  properties: {
    value: {
      type: String,
      value: ''
    },
    /** 一次滚动耗时 单位ms */
    duration: {
      type: Number,
      value: 1600
    },
    /** 每个数字之间的延迟滚动 */
    delay: {
      type: Number,
      value: 200
    }
  },
  data: {
    valArr: [],
    aniArr: [],  // 动画列表,和valArr对应
    numArr: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],  // 所有数字
    itemHeight: 0  // 数字项的高度
  },
  observers: {
    value: function (newVal) {
      console.log(111)
      // 监听value变化,格式化为valArr
      let valArr = []
      if (newVal) {
        valArr = newVal.split('').map(o => {
          return { val: o, isNaN: isNaN(o)}
        })
      }
      this.setData({
        valArr: valArr
      })
      console.log(valArr)
      this.getNumberHeight()
    }
  },
  methods: {
    /** 计算数字高度 */
    getNumberHeight() {
      let _that = this;
      console.log(666)
      if (this.data.itemHeight > 0) {
        this.startScrollAni()
        return
      }
      wx.createSelectorQuery().selectAll('.scroll-number').boundingClientRect(function (rect) {
        console.log(rect)
        _that.setData({
          itemHeight: rect[0].height
        })
        _that.startScrollAni()
      }).exec()
    },
    /** 开始滚动动画 */
    startScrollAni() {
      console.log(1111)
      console.log(this.data.itemHeight)
      if (this.data.itemHeight <= 0) return

      const aniArr = []
      this.data.valArr.forEach((item, index) => {
        console.log(999)
        if(!item.isNaN) {
          console.log(999)
          aniArr.push(`transition-delay: ${this.data.delay * index}ms; top: ${-this.data.itemHeight * (this.data.numArr[parseInt(item.val)] + 10)}px;`)
        } else {
          console.log(999555)
          aniArr.push(null)
        }
      })
      console.log(aniArr)
      this.setData({
        aniArr: aniArr
      })
    }
  },
  ready:function(){
    this.setData({
      value:1560,
      duration:500,
      delay:200
    })
  }
})

css

/* pages/ceshi/index.wxss */
.scroll-number {
  display: flex;
  align-items: flex-end;
  height: 72rpx;
}
.scroll-number-item {
  color: #0079FE;
  font-size: 48rpx;
  font-weight: bold;
  margin: 0 24rpx;
  font-family: Microsoft YaHei;
}
.number-item {
  background-color: rgba(0, 121, 254, 0.2);
  border-radius: 8rpx;
  width: 56rpx;
  height: 72rpx;
  line-height: 72rpx;
  overflow: hidden;
  text-align: center;
  position: relative;
}
.number-dot {
  margin: 0 12rpx;
}
.scroll-ani {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: all 2s ease-in-out 0s;
}

 



这篇关于小程序数字滚动效果的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程