经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
JavaScript实现余额数字滚动效果
来源:jb51  时间:2021/12/24 8:45:52  对本文有异议

1.实现背景

上周在一个完成任务领取红包的活动需求中,需要实现一个用户点击按钮弹出领取红包弹窗后,在关 闭弹窗返回原来的页面时,页面余额数字部分要展示一个每一位数字滚动后的效果。
因为之前没做过这样的效果,一开始也不知道要如何实现,本来想在GitHub上找一下相关的库,看到一个最高star的库,但发现它是依赖jQuery的,而且不可以npm包引入。感觉就很没有必要,本来项目是react框架的,就是要尽量少的操作DOM,为了解决这个滚动就要引入jQuery,感觉不太合适。所以我决定还是自己实现,先看了一下别人的思路,然后自己再去实现。

2.实现思路

其实就是将传入的带滚动的n位数字拆分成每一个要滚动的数,然后动态的创建装着滚动到每一位相应数字的容器,然后放入传入的目标容器中。滚动到每一位相应的数字的实现可以通过动态创建从0到相应数字的间隔数的div,div的内容分别为对应的数字,就像一个竖直写着从0-n的长纸条,然后拉着它在指定时间内从0上拉到目标数字。

3.实现过程

既然要封装,还是写成class的形式吧,话不多说,直接上代码吧

  1. /**
  2. * 实现数字滚动的效果的类
  3. */
  4. class DigitScroll {
  5. constructor(options) {
  6. //获取容器的DOM,没有则抛出错误
  7. this.container = document.querySelector(options.container);
  8. if (!this.container) {
  9. throw Error("no container");
  10. }
  11. this.container.style.overflow = "hidden";
  12. this.container.style.display = "flex";
  13. //可视容器高度 也是滚动间隔距离,容器要设置高度,否则默认30px
  14. this.rollHeight = parseInt(getComputedStyle(this.container).height) || 30;
  15. this.container.style.height = this.rollHeight + "px";
  16. }
  17. roll(num) {
  18. // 将传入的要滚动的数字拆分后初始化每一位数字的容器
  19. this.initDigitEle(num);
  20. const numEles = this.container.querySelectorAll(".single-num");
  21. // 遍历生成每一位数字的滚动队列,如滚动到7,则生成内容为0,1,2,3,4,5,6,7的7个div,用于滚动动画
  22. [...numEles].forEach((numEle, index) => {
  23. const curNum = 0;
  24. let targetNum = Number(this.numberArr[index]);
  25. if (curNum >= targetNum) {
  26. targetNum = targetNum + 10;
  27. }
  28. let cirNum = curNum;
  29. // 文档碎片,拼凑好后一次性插入节点中
  30. const fragment = document.createDocumentFragment();
  31. // 生成从0到目标数字对应的div
  32. while (targetNum >= cirNum) {
  33. const ele = document.createElement("div");
  34. ele.innerHTML = cirNum % 10;
  35. cirNum++;
  36. fragment.appendChild(ele);
  37. }
  38. numEle.innerHTML = "";
  39. numEle.appendChild(fragment);
  40. //重置位置
  41. numEle.style.cssText +=
  42. "-webkit-transition-duration:0s;-webkit-transform:translateY(0)";
  43. setTimeout(() => {
  44. numEle.style.cssText += `-webkit-transition-duration:1s;-webkit-transform:translateY(${
  45. -(targetNum - curNum) * this.rollHeight
  46. }px);`;
  47. }, 50);
  48. });
  49. }
  50. // 初始化容器
  51. initDigitEle(num) {
  52. // 数字拆分位数
  53. const numArr = num.toString().split("");
  54. // 文档碎片,拼凑好后一次性插入节点中
  55. const fragment = document.createDocumentFragment();
  56. numArr.forEach((item) => {
  57. const el = document.createElement("div");
  58. // 数字是要滚动的,非数字如.是不滚动的
  59. if (/[0-9]/.test(item)) {
  60. el.className = "single-num";
  61. el.style.height = this.rollHeight + "px";
  62. el.style.lineHeight = this.rollHeight + "px";
  63. } else {
  64. el.innerHTML = item;
  65. el.className = "no-move";
  66. el.style.verticalAlign = "bottom";
  67. }
  68. // el.style.float='left';
  69. fragment.appendChild(el);
  70. }, []);
  71. this.container.innerHTML = "";
  72. this.container.appendChild(fragment);
  73. // 存储滚动的数字
  74. this.numberArr = numArr.filter((item) => /[0-9]/.test(item));
  75. }
  76. }

到此这篇关于JavaScript实现余额数字滚动效果的文章就介绍到这了,更多相关JavaScript实现 数字滚动 内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号