美文网首页
数字带边框滚动变化特效

数字带边框滚动变化特效

作者: 衬fzy | 来源:发表于2023-10-06 11:13 被阅读0次
微信截图_20231007105600.png

引入使用

import Number from './components/Number'
<Number name="今日省内监测" :number="996"></Number>

组件代码(感叹号最需要部分)

<template>
  <div class="numDiv">
    <table>{{ name }}</table>
    <div class="chartNum">
      <li v-for="(item, index) in orderNum" :key="index"
        :class="{ 'number-item': !isNaN(item), 'mark-item': isNaN(item) }">
        <span v-if="!isNaN(item)">
          <i ref="numberItem">0123456789</i>
        </span>
        <span v-else class="comma">{{ item }}</span>
      </li>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    name: {
      type: [Array, Object, Number, String],
      default: null
    },
    number: {
      type: [Array, Object, Number, String],
      default: null
    }
  },
  data() {
    return {
      orderNum: [0, 0, 0, 0] // 当前要渲染的元素长度
    };
  },
  watch: {
    number: {
      handler: function (val) {
        this.toOrderNum(val);
      },
      immediate: true // 首次赋值是否触发,一般不写
    }
  },
  mounted() {
  },
  methods: {
    // 设置文字滚动
    setNumberTransform() {
      const numberItems = this.$refs.numberItem; // 拿到数字的ref,计算元素数量
      const numberArr = this.orderNum.filter(item => !isNaN(item));
      console.log(numberItems.length, numberArr);
      // 结合CSS 对数字字符进行滚动,显示数量
      for (let index = 0; index < numberItems.length; index += 1) {
        const elem = numberItems[index];
        let n = numberArr[index] * 10 <= 0 ? 0.5 : '-' + (numberArr[index] * 10 - 0.5) // 大小改变需要改变Y轴值保持居中(数字之间是靠字行高隔开的)
        elem.style.transform = `translate(-50%, ${n}%)`;
      }
      // this.$nextTick(() => {
      //   this.$forceUpdate()
      // })
    },
    // 处理总数字
    toOrderNum(num) {
      if (this.orderNum.length == 0) {
        for (let i = 0; i <= String(num).length; i++) {
          this.orderNum.push('0')
        }
      }
      console.log(this.orderNum)
      this.$nextTick(() => {
        const numtext = num.toString()
        // console.log(numtext, 4)
        // if (this.length) {
        if (numtext.length < 4) { // 最少4位数补齐0
          const numlist = `0${numtext}` // 如未满固定数,添加"0"补位
          this.toOrderNum(numlist) // 递归添加"0"补位
        } else if (numtext.length === num.length) {
          this.orderNum = numtext.split('') // 将其便变成数据,渲染至滚动数组
        }
        // } else {
        //   this.orderNum = numtext.split('')
        //   // console.log(this.orderNum)
        // }
        // 数字中加入逗号
        // const length = numtext.length / 3;
        // let count = '';
        // for (let i = 0; i < length; i += 1) {
        //  if (i === 0) {
        //   count += `${numtext.slice(i, i + 3)},`;
        //   console.log(count);
        //  } else if (i === length - 1) {
        //   count += numtext.slice(i * 3, i * 3 + 3);
        //   console.log(count);
        //  } else {
        //   count += `${numtext.slice(i * 3, i * 3 + 3)},`;
        //   console.log(count);
        //  }
        // }
        // this.orderNum = count.split('');
        this.$nextTick(() => {
          setTimeout(() => {
            this.setNumberTransform()
          }, 500)
        })
      })
    }
  }
};
</script>
<style scoped lang="scss">
.numDiv {
  flex: 1;
  height: 58px;
  text-align: center;
  display: flex;
  justify-content: center; //左右居中

  >table {
    float: left; //浮动
    height: 100%;
    line-height: 54px; //!
    font-size: 20px;
    margin-right: 20px;
  }

  .chartNum {
    float: left; //浮动
    position: relative;
    height: 100%;
    font-family: AzonixRegular;
    color: #021c25;
    text-align: center;
    list-style: none;
    writing-mode: vertical-lr;
    text-orientation: upright;

    /*滚动数字设置*/
    .number-item {
      width: 40px; //!
      height: 100%;
      /* 背景图片 */
      margin-right: 14px; //!
      border: 1px solid #688BB9;
      background-image: radial-gradient(circle at center, rgba(118, 161, 222, 0.75), rgba(9, 41, 94, 0.75));
      background-image: radial-gradient(center, ellipse cover, rgba(118, 161, 222, 0.75) 30%, rgba(9, 41, 94, 0.75) 100%);
      background-image: radial-gradient(ellipse at center, rgba(118, 161, 222, 0.75) 30%, rgba(9, 41, 94, 0.75) 100%);

      >span {
        position: relative;
        display: inline-block;
        margin-right: 10px;
        width: 100%;
        height: 100%;
        writing-mode: vertical-rl;
        text-orientation: upright;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;

        >i {
          font-size: 30px; //!
          font-style: normal;
          position: absolute;
          top: 8px;
          left: 50%;
          transform: translate(-50%, 0.5%); //大小改变需要改变Y轴值保持居中(数字之间是靠字行高隔开的)!
          transition: transform 1s ease-in-out;
          letter-spacing: 58px; //数字行高!
          color: #fff;
        }
      }
    }

    .number-item:last-child {
      margin-right: 0;
    }

    /* 默认逗号设置 */
    .mark-item {
      width: 28px;
      height: 100%;
      position: relative;
      list-style: none;
      margin-right: 1px;

      >span {
        position: absolute;
        width: 100%;
        height: 100%;
        bottom: 2px;
        left: 20%;
        font-size: 20px;
        writing-mode: vertical-rl;
        text-orientation: upright;
      }
    }
  }
}
</style>

相关文章

  • iOS新浪新闻首页卡片滚动特效实现浅谈

    iOS新浪新闻首页卡片滚动特效实现浅谈 iOS新浪新闻首页卡片滚动特效实现浅谈

  • vegas视频特效——边框

    有很多用户刚刚接触vegas就看到网上一些很炫酷的效果,于是盲目的下载安装很多插件,其实vegas自身的特效也是非...

  • 用这5个技巧在网页设计中玩转视差特效

    视差特效是目前最流行的网页设计趋势之一。在这个滚动动画特效支撑之下,前景的元素和背景会在滚动浏览时以不同的速度运动...

  • js网页特效-2

    js特效 - Day2 一、水平滚动条 和 垂直滚动条 1.1 核心技术点 1)求滚动条的长度? 2)拖动滚动条,...

  • Image

    直接圆角图片 设置圆角图片度数 设置圆角图片带灰色圆角边框 设置圆角图片带灰色圆角边框带阴影

  • Table的基本标签和属性

    border:显示表格的边框宽,值为数字;cellpadding:内容到边框间的距离,即边框的内边距;cellsp...

  • 手机壳样式 持续更新

    20170527更新 20170526更新无边框 带边框 添加背景图案

  • 基于C++的实现文字序幕上下滚动效果

    VC++ 6.0实现文字上下滚动效果,类似电影字幕,定位于文字框中的文字滚动,在窗口中实现的文字垂直滚动特效。在代...

  • 美化谷歌浏览器的滚动条

    ::-webkit-scrollbar-track-piece { /*滚动条凹槽的颜色,还可以设置边框属性*/ ...

  • 京东移动端初体验

    header栏的一个特效 思路 在滚动事件中 写逻辑 修改 通栏的透明度 需要设置滚动的范围 滚动的距离 跟轮播图...

网友评论

      本文标题:数字带边框滚动变化特效

      本文链接:https://www.haomeiwen.com/subject/kpbpbdtx.html