小程序数字滚动效果

互联网 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.…

 效果图

 

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;
}

 

随时随地学软件编程-关注百度小程序和微信小程序
关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[小程序数字滚动效果]http://www.zyiz.net/tech/detail-228473.html

上一篇:微信小程序--rpx及与px的运算

下一篇:微信小程序-手持弹幕_文字内容横屏滚动_小程序弹幕源码

赞(0)
关注微信小程序
程序员编程王-随时随地学编程

扫描二维码或查找【程序员编程王】

可以随时随地学编程啦!

技术文章导航 更多>
扫一扫关注最新编程教程