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 17 Vue.js
Vue实现购物小球抛物线的方法实例
Nov 22 Vue.js
vue开发chrome插件,实现获取界面数据和保存到数据库功能
Dec 01 Vue.js
详解vue-cli项目在IE浏览器打开报错解决方法
Dec 10 Vue.js
使用vue3重构拼图游戏的实现示例
Jan 25 Vue.js
详解vite+ts快速搭建vue3项目以及介绍相关特性
Feb 25 Vue.js
vue 使用 v-model 双向绑定父子组件的值遇见的问题及解决方案
Mar 01 Vue.js
vue如何批量引入组件、注册和使用详解
May 12 Vue.js
详解Vue router路由
Nov 20 Vue.js
Vue3中toRef与toRefs的区别
Mar 24 Vue.js
解决vue中provide inject的响应式监听
Apr 19 Vue.js
vue/cli 配置动态代理无需重启服务的方法
May 20 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 应用程序安全防范技术研究
2009/09/25 PHP
基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法详解
2013/05/07 PHP
编写PHP程序检查字符串中的中文字符个数的实例分享
2016/03/17 PHP
Yii2实现ajax上传图片插件用法
2016/04/28 PHP
浅谈PHP封装CURL
2019/03/06 PHP
Gambit vs CL BO3 第一场 2.13
2021/03/10 DOTA
Display SQL Server Version Information
2007/06/21 Javascript
jquery.alert 弹出式复选框实现代码
2009/06/15 Javascript
js实现收缩菜单效果实例代码
2013/10/30 Javascript
jqGrid日期格式的判断示例代码(开始日期与结束日期)
2013/11/08 Javascript
Javascript中的异步编程规范Promises/A详细介绍
2014/06/06 Javascript
纯js实现div内图片自适应大小(已测试,兼容火狐)
2014/06/16 Javascript
网页下载文件期间如何防止用户对网页进行其他操作
2014/06/27 Javascript
jQuery插件kinMaxShow扩展效果用法实例
2015/05/04 Javascript
JS组件Bootstrap ContextMenu右键菜单使用方法
2016/04/17 Javascript
详解JavaScript中Hash Map映射结构的实现
2016/05/21 Javascript
Javascript动画效果(1)
2016/10/11 Javascript
微信小程序 详解下拉加载与上拉刷新实现方法
2017/01/13 Javascript
javascript 使用正则test( )第一次是 true,第二次是false
2017/02/22 Javascript
jQuery实现火车票买票城市选择切换功能
2017/09/15 jQuery
vue 项目如何引入微信sdk接口的方法
2017/12/18 Javascript
vue keep-alive请求数据的方法示例
2018/05/16 Javascript
Python3.x中自定义比较函数
2015/04/24 Python
在Linux命令行终端中使用python的简单方法(推荐)
2017/01/23 Python
Python批量查询域名是否被注册过
2017/06/21 Python
使用django-crontab实现定时任务的示例
2018/02/26 Python
利用python Selenium实现自动登陆京东签到领金币功能
2019/10/31 Python
numpy的Fancy Indexing和array比较详解
2020/06/11 Python
keras 回调函数Callbacks 断点ModelCheckpoint教程
2020/06/18 Python
python爬虫调度器用法及实例代码
2020/11/30 Python
amazeui模态框弹出后立马消失并刷新页面
2020/08/19 HTML / CSS
加拿大休闲和工业服装和鞋类零售商:L’Équipeur
2018/01/12 全球购物
2014国培学习感言
2014/03/05 职场文书
小学毕业演讲稿
2014/04/25 职场文书
勿忘国耻9.18演讲稿(经典篇)
2014/09/14 职场文书
python基础入门之普通操作与函数(三)
2021/06/13 Python