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如何循环提取对象数组中的值
Nov 18 Vue.js
vue中defineProperty和Proxy的区别详解
Nov 30 Vue.js
Vue在H5 项目中使用融云进行实时个人单聊通讯
Dec 14 Vue.js
Vue使用鼠标在Canvas上绘制矩形
Dec 24 Vue.js
Vue仿百度搜索功能
Dec 28 Vue.js
vue3+typeScript穿梭框的实现示例
Dec 29 Vue.js
Vue仿Bibibili首页的问题
Jan 21 Vue.js
Vue.js中v-for指令的用法介绍
Mar 13 Vue.js
VUE中的v-if与v-show区别介绍
Mar 13 Vue.js
vue二维数组循环嵌套方式 循环数组、循环嵌套数组
Apr 24 Vue.js
vue css 相对路径导入问题级踩坑记录
Jun 05 Vue.js
vue实现input输入模糊查询的三种方式
Aug 14 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
php 获取SWF动画截图示例代码
2014/02/10 PHP
php线性表的入栈与出栈实例分析
2015/06/12 PHP
对PHP依赖注入的理解实例分析
2016/10/09 PHP
基于php(Thinkphp)+jquery 实现ajax多选反选不选删除数据功能
2017/02/24 PHP
PHP实现执行外部程序的方法详解
2017/08/17 PHP
jquery 插件开发方法小结
2009/10/23 Javascript
Web前端设计模式  制作漂亮的弹出层
2010/10/29 Javascript
表单验证的完整应用案例探讨
2013/03/29 Javascript
js将控件隐藏的方法及display属性介绍
2013/07/04 Javascript
javascript生成随机颜色示例代码
2014/05/05 Javascript
jquery操作select元素和option的实例代码
2016/02/03 Javascript
Bootstrap布局组件应用实例讲解
2016/02/17 Javascript
javascript中apply、call和bind的使用区别
2016/04/05 Javascript
jquery 全选、全不选、反选效果的实现代码【推荐】
2016/05/05 Javascript
js中scrollTop()方法和scroll()方法用法示例
2016/10/03 Javascript
Javascript实现数组中的元素上下移动
2017/04/28 Javascript
在iFrame子页面里实现模态框的方法
2018/08/17 Javascript
基于layui数据表格以及传数据的方式
2018/08/19 Javascript
Vue+ElementUI使用vue-pdf实现预览功能
2019/11/26 Javascript
详解ES6新增字符串扩张方法includes()、startsWith()、endsWith()
2020/05/12 Javascript
[00:10]DOTA2全国高校联赛速递
2018/05/30 DOTA
[04:16]完美世界DOTA2联赛PWL S2 集锦第一期
2020/11/23 DOTA
python实现单向链表详解
2018/02/08 Python
Python Flask框架模板操作实例分析
2019/05/03 Python
python3.7实现云之讯、聚合短信平台的短信发送功能
2019/09/26 Python
解析Python3中的Import
2019/10/13 Python
Python 过滤错误log并导出的实例
2019/12/26 Python
Python&amp;&amp;GDAL实现NDVI的计算方式
2020/01/09 Python
python GUI库图形界面开发之PyQt5滑块条控件QSlider详细使用方法与实例
2020/02/28 Python
荟萃全球保健品:维他购
2018/05/09 全球购物
家长会邀请书
2014/01/25 职场文书
超市中秋节活动方案
2014/02/12 职场文书
大学生标准自荐书
2014/06/15 职场文书
高中物理教学反思
2016/02/19 职场文书
Go 语言结构实例分析
2021/07/04 Golang
解决vue自定义组件@click点击失效问题
2022/04/30 Vue.js