vue实现 金额数字单个上下滚动切换

话不多少,主要是自己记录下以便后期查看

<template>
 <div class="chartNum">
      <div class="box-item">
        <li :class="{'number-item': !isNaN(item), 'mark-item': isNaN(item) }"
            v-for="(item,index) in orderNum"
            :key="index">
              <span v-if="!isNaN(item)">
                <i ref="numberItem">0123456789</i>
              </span>
          <span class="comma" v-else>{{item}}</span>
        </li>
      </div>
  </div>

</template>

<script>
export default {
    data() {
      return {
        orderNum: [ '0', '0','.', '0', '0'], 
      }
    },
    mounted() {
      this.toOrderNum(12.35) // 这里输入数字即可调用
      this.setNumberTransform()

      
      setTimeout(() => {
        this.toOrderNum(12.28)
        this.setNumberTransform()
      }, 2000);
    },
    methods: {
      // 设置文字滚动
      setNumberTransform() {
        const numberItems = this.$refs.numberItem // 拿到数字的ref,计算元素数量
        const numberArr = this.orderNum.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) {
        console.log('11111')
        num = num.toString()
        console.log(num.length)
        if (num.length === 5) {
          this.orderNum = num.split('') // 将其变成数据,渲染至滚动数组
          console.log(this.orderNum,'this.orderNum')
        }
      },
    
    },
  }
</script>

<style lang='less'>

 
  /*金额滚动数字设置*/
  .box-item {
    width: 100%;
    text-align: center;
    line-height: 40px;
    font-size: 24px;
    padding: 2% 0;
    position: relative;
    list-style: none;
    writing-mode: vertical-lr;
    text-orientation: upright;
    /*文字禁止编辑*/
    -moz-user-select: none; /*火狐*/
    -webkit-user-select: none; /*webkit浏览器*/
    -ms-user-select: none; /*IE10*/
    -khtml-user-select: none; /*早期浏览器*/
    user-select: none;
  }
 
  /* 默认逗号设置 */
  .mark-item {
    width: 10px;
    line-height: 10px;
    font-size: 24px;
    position: relative;
  }
  .mark-item span {
    position: absolute;
    width: 100%;
    bottom: 0;
    writing-mode: vertical-rl;
    text-orientation: upright;
  }
  /*滚动数字设置*/
  .number-item {
    width: 20px;
    height: 30px;
    list-style: none;
    margin-right: 5px;
    background-size: 100% 100%;
 
  }
 
  .number-item:first-child {
    margin-left: 0;
  }
 
  .number-item span {
    position: relative;
    display: inline-block;
    margin-right: 10px;
    width: 100%;
    height: 100%;
    writing-mode: vertical-rl;
    text-orientation: upright;
    overflow: hidden;
 
  }
 
  .number-item span i {
    font-style: normal;
    position: absolute;
    top: 0px;
    left: 50%;
    transform: translate(-50%,0);
    transition: transform .5s ease-in-out;
    letter-spacing: 10px;
   
  }
  
 
  .number-item:last-child {
    margin-right: 0;
  }

</style>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值