设置数字串动态滚动效果

HTML代码段

 <div class="data">
   <span>今日入区车辆</span>
   <div class="box-item">
       <li
         v-for="(item, index) in orderCarsNum"
         :key="index"
         :class="{ 'number-item': !isNaN(item), 'mark-item': isNaN(item) }"
       >
         <em v-if="!isNaN(item)">
           <i ref="numberItem">0123456789</i>
         </em>
         <em v-else class="comma">{{ item }}</em>
      </li>
   </div>
   <span>辆</span>
</div>

script代码段

methods:{
    initData() {
      const num = 1234
      this.toOrderNum(num)
      this.setNumberTransform()
    },
// 设置文字滚动
    setNumberTransform() {
      const _this = this
      const numberItems = _this.$refs.numberItem // 拿到数字的ref,计算元素数量
      const numberArr = this.orderCarsNum.filter((item) => !isNaN(item))
      // 结合CSS 对数字字符进行滚动,显示订单数量
      for (let index = 0; index < numberItems.length; index++) {
        const elem = numberItems[index]
        elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`
      }
    },
// 处理数字
    toOrderNum(num) {
      num = num.toString()
      // 把订单数变成字符串
      if (num.length < 5) {
        num = '0' + num // 如未满5位数,添加"0"补位
        this.toOrderNum(num) // 递归添加"0"补位
      } else if (num.length === 5) {
        // 订单数中加入逗号
        num = num.slice(0, 2) + num.slice(2, 5) + num.slice(5, 8)
        this.orderCarsNum = num.split('') // 将其便变成数据,渲染至滚动数组
      } else {
        // 数字超过5位显示异常
        this.$message.warning('数字过大,显示异常,请联系客服')
      }
    },

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值