vue 数字翻牌器动态加载数据


Posted in Vue.js onApril 20, 2022

数字动态翻牌器

最近项目里使用到了数字翻牌器,于是自己写了一个,动态的翻牌器

第一步创建一个组件页面,NumberCount.vue

思路:大概就是显示几位数,然后从0开始滚动到当前的数值的位置,在每一个位置都有0-9的数,然后就是往上滚动当前数值的次数到当前的数,话不多说上代码

<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 {
  props: {
    // 显示的数字
    number: {
      type: Number,
    },
    // 显示的长度
    length: {
      type: Number,
    },
  },
  data() {
    return {
      orderNum: ['0', '0', '0', '0', '0', '0', '0', '0'], // 默认总数
    };
  },
  mounted() {
    setTimeout(() => {
      this.toOrderNum(this.number); // 这里输入数字即可调用
    }, 500);
  },
  watch: {
    number: {
      handler(val) {
        this.toOrderNum(val);
      },
      deep: true,
    },
  },
  methods: {
    // 设置文字滚动
    setNumberTransform() {
      const numberItems = this.$refs.numberItem; // 拿到数字的ref,计算元素数量
      // eslint-disable-next-line no-restricted-globals
      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];
        elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`;
      }
    },
    // 处理总数字
    toOrderNum(num) {
      const numtext = num.toString();
      if (this.length) {
        if (numtext.length < this.length) {
          const numlist = `0${numtext}`; // 如未满固定数,添加"0"补位
          this.toOrderNum(numlist); // 递归添加"0"补位
        } else if (numtext.length === num.length) {
          this.orderNum = numtext.split(''); // 将其便变成数据,渲染至滚动数组
        }
      } else {
        this.orderNum = numtext.split('');
      }
      // 数字中加入逗号
      // 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.setNumberTransform();
    },
  },
};
</script>
<style scoped lang="scss">
/*总量滚动数字设置*/
.box-item {
  position: relative;
  height: 34px;
  font-size: 20px;
  font-family: AzonixRegular;
  color: #021c25;
  line-height: 41px;
  text-align: center;
  list-style: none;
  writing-mode: vertical-lr;
  text-orientation: upright;
}
/* 默认逗号设置 */
.mark-item {
  width: 28px;
  height: 34px;
  position: relative;
  /* 背景图片 */
  background: url('~@/assets/images/overview/bg-chartNum.svg') no-repeat center
    center;
  background-size: 100% 100%;
  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;
  }
}
/*滚动数字设置*/
.number-item {
  width: 28px;
  height: 34px;
  /* 背景图片 */
  background: url('~@/assets/images/overview/bg-chartNum.svg') no-repeat center
    center;
  background-size: 100% 100%;
  // background: #ccc;
  list-style: none;
  margin-right: 1px;
  & > 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-style: normal;
      position: absolute;
      top: 8px;
      left: 50%;
      transform: translate(-50%, 0);
      transition: transform 1s ease-in-out;
      letter-spacing: 10px;
    }
  }
}
.number-item:last-child {
  margin-right: 0;
}
</style>

不加逗号:

vue 数字翻牌器动态加载数据

加入逗号:

vue 数字翻牌器动态加载数据

至于样式背景可以自定义

以上就是本文的全部内容,希望对大家的学习有所帮助。

Vue.js 相关文章推荐
vue中echarts的用法及与elementui-select的协同绑定操作
Nov 17 Vue.js
vue3.0+vue-router+element-plus初实践
Dec 02 Vue.js
Vue项目中使用mock.js的完整步骤
Jan 12 Vue.js
Vue中引入svg图标的两种方式
Jan 14 Vue.js
Vue中的nextTick作用和几个简单的使用场景
Jan 25 Vue.js
Vue项目打包部署到apache服务器的方法步骤
Feb 01 Vue.js
Vue基本指令实例图文讲解
Feb 25 Vue.js
vue项目实现分页效果
Mar 24 Vue.js
vue使用节流函数的踩坑实例指南
May 20 Vue.js
vue2的 router在使用过程中遇到的一些问题
Apr 13 Vue.js
vue3种table表格选项个数的控制方法
Apr 14 Vue.js
Vue Mint UI mt-swipe的使用方式
Jun 05 Vue.js
vue3.0 数字翻牌组件的使用方法详解
Apr 20 #Vue.js
vue封装数字翻牌器
Apr 20 #Vue.js
vue特效之翻牌动画
Apr 20 #Vue.js
解决vue中provide inject的响应式监听
Apr 19 #Vue.js
vue3种table表格选项个数的控制方法
Apr 14 #Vue.js
vue项目配置sass及引入外部scss文件
Apr 14 #Vue.js
解决vue-router的beforeRouteUpdate不能触发
Apr 14 #Vue.js
You might like
很温暖很温暖的Lester Young
2021/03/03 冲泡冲煮
require(),include(),require_once()和include_once()的异同
2007/01/02 PHP
php实现mysql同步的实现方法
2009/10/21 PHP
队列在编程中的实际应用(php)
2010/09/04 PHP
深入理解:单一入口、MVC、ORM、CURD、ActiveRecord概念
2013/06/06 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
js输入框邮箱自动提示功能代码实现
2013/12/10 Javascript
仿JQuery输写高效JSLite代码的一些技巧
2015/01/13 Javascript
Bootstrap中的Dropdown下拉菜单更改为悬停(hover)触发
2016/08/31 Javascript
jQuery中的AjaxSubmit使用讲解
2016/09/25 Javascript
javascript读取文本节点方法小结
2016/12/15 Javascript
vue事件修饰符和按键修饰符用法总结
2017/07/25 Javascript
微信小程序实现漂亮的弹窗效果
2020/05/26 Javascript
React如何解决fetch跨域请求时session失效问题
2018/11/02 Javascript
JS实现checkbox互斥(单选)功能示例
2019/05/04 Javascript
微信小程序开发之转发分享功能
2019/10/22 Javascript
基于JS实现计算24点算法代码实例解析
2020/07/23 Javascript
Python 正则表达式的高级用法
2016/12/04 Python
python实现FTP服务器服务的方法
2017/04/11 Python
Python中单例模式总结
2018/02/20 Python
pandas.dataframe按行索引表达式选取方法
2018/10/30 Python
Pycharm+Scrapy安装并且初始化项目的方法
2019/01/15 Python
Python K最近邻从原理到实现的方法
2019/08/15 Python
python3中的logging记录日志实现过程及封装成类的操作
2020/05/12 Python
美国大码时尚女装购物网站:ELOQUII
2017/12/28 全球购物
英国顶级珠宝品牌之家:John Greed
2018/06/09 全球购物
FILA德国官方网站:来自意大利的体育和街头服饰品牌
2019/07/19 全球购物
奥地利票务门户网站:oeticket.com
2019/12/31 全球购物
经典c++面试题四
2015/05/14 面试题
大学生简历中个人的自我评价
2013/10/06 职场文书
物业管理应届生求职信
2013/10/28 职场文书
个人借款担保书
2014/04/02 职场文书
员工安全责任书范本
2014/07/24 职场文书
2014副局长群众路线对照检查材料思想汇报
2014/09/22 职场文书
2015秋季田径运动会广播稿
2015/08/19 职场文书
Python保存并浏览用户的历史记录
2022/04/29 Python