微信小程序笔记——滚动计数器

  效果实现起来不难,主要用到了小程序的动画。

<template>
  <view class="group">
    <view class="title">滚动计数器</view>
    <view class="body">
      <block wx:for="{{numberWheelArg}}" wx:item="item" wx:key="index">
        <view class="block" animation="{{item.animation}}">
          <block wx:for="{{numberArg}}" wx:item="item" wx:key="index">
            <block wx:if="{{index === 0}}">
              <text style="color:black;">{{item}}</text>
            </block>
            <block wx:else>
              <text>{{item}}</text>
            </block>
          </block>
        </view>
      </block>
    </view>
  </view>
</template>

<script>
  import wepy from 'wepy';

  class NumberWheel {
    constructor(animation = null) {
      this.animation = animation;
    }
  }

  export default class Index extends wepy.page {
    config = {
      navigationBarTitleText: '滚动计数器'
    };

    data = {
      numBerWheelSize: 10,
      numberWheelArg: [],
      numberArg: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    };

    onLoad() {
      this.initData();//初始化数据
    }

    initData() {
      const initArg = [];
      for (let i = 0; i < this.numBerWheelSize; i++) {
        initArg.push(new NumberWheel());
      }
      this.numberWheelArg = initArg;
      let start = 123456789;
      setInterval(() => {
        this.redraw(start++);
      }, 2000);
    }

    redraw = (number) => {
      console.log(number);
      let stamp = number.toString();
      if (stamp.length < this.numBerWheelSize) {
        stamp = '0'.repeat(this.numBerWheelSize - stamp.length) + stamp;
      }
      let copy = [];
      for (let i = 0; i < this.numBerWheelSize; i++) {
        let animation = wx.createAnimation();
        animation.translate(0, (Number(stamp.charAt(i)) * -28)).step();
        copy.push(new NumberWheel(animation.export()));
        this.numberWheelArg = copy;
      }
      this.$apply();
    };

  }
</script>
<style lang="stylus">

  _size = 28px
  _border = white solid 1px
  _titleTextColor = red, orange, yellow, green, cyan, blue, purple
  _numGroupColor = #606060, #cacaca, #606060

  page
    display flex
    display -webkit-flex
    justify-content center
    align-items center
    background-color black
    width 100%
    height 100%

  .group
    display flex
    display -webkit-flex
    justify-content center
    align-items center
    flex-direction column
    color white

  .title
    background radial-gradient(_titleTextColor)
    background -webkit-radial-gradient(_titleTextColor)
    -webkit-background-clip text
    background-clip text
    color transparent
    margin-bottom 20px

  .body
    border _border
    border-right none
    display flex
    display -webkit-flex
    justify-content center
    background linear-gradient(_numGroupColor)
    background -webkit-linear-gradient(_numGroupColor)
    overflow hidden

  .body .block
    height _size
    display -webkit-flex
    display flex
    flex-direction column

  .body .block > text
    height _size
    width _size
    border-right _border
    text-align center
    line-height _size
    font-size 16px

</style>

  实现效果图。
在这里插入图片描述
  原理图。
在这里插入图片描述
参考jQuery滚动数字、计数器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值