经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序实现数字滚动动画
来源:jb51  时间:2022/7/19 9:55:38  对本文有异议

本文实例为大家分享了微信小程序实现数字滚动效果的具体代码,供大家参考,具体内容如下

效果图

实现思路

1、为了实现数字的无限滚动效果,每个数字框的内部,其实包含了两组0~9的view,每个View的高度都一样
2、数字框内使用绝对定位,通过调整top位置,显示出指定的数字
3、使用transtion动画,top发生变化时就会滚动,然后通过指定动画的delay、duration,使数字之间延时动画

项目代码

js文件

  1. // components/scroll-number/index.js
  2. Component({
  3. ? externalClasses: ['container-class', 'item-class', 'dot-class'],
  4. ? properties: {
  5. ? ? value: {
  6. ? ? ? type: String,
  7. ? ? ? value: ''
  8. ? ? },
  9. ? ? /** 一次滚动耗时 单位ms */
  10. ? ? duration: {
  11. ? ? ? type: Number,
  12. ? ? ? value: 1600
  13. ? ? },
  14. ? ? /** 每个数字之间的延迟滚动 */
  15. ? ? delay: {
  16. ? ? ? type: Number,
  17. ? ? ? value: 200
  18. ? ? }
  19. ? },
  20. ? data: {
  21. ? ? valArr: [],
  22. ? ? aniArr: [], ?// 动画列表,和valArr对应
  23. ? ? numArr: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], ?// 所有数字
  24. ? ? itemHeight: 0 ?// 数字项的高度
  25. ? },
  26. ? observers: {
  27. ? ? value: function (newVal) {
  28. ? ? ? // 监听value变化,格式化为valArr
  29. ? ? ? let valArr = []
  30. ? ? ? if (newVal) {
  31. ? ? ? ? valArr = newVal.split('').map(o => {
  32. ? ? ? ? ? return { val: o, isNaN: isNaN(o)}
  33. ? ? ? ? })
  34. ? ? ? }
  35. ? ? ? this.setData({
  36. ? ? ? ? valArr: valArr
  37. ? ? ? })
  38. ? ? ? this.getNumberHeight()
  39. ? ? }
  40. ? },
  41. ? methods: {
  42. ? ? /** 计算数字高度 */
  43. ? ? getNumberHeight() {
  44. ? ? ? if (this.data.itemHeight > 0) {
  45. ? ? ? ? this.startScrollAni()
  46. ? ? ? ? return
  47. ? ? ? }
  48. ? ? ? const query = this.createSelectorQuery()
  49. ? ? ? query.select('.number-item').boundingClientRect()
  50. ? ? ? query.exec((res) => {
  51. ? ? ? ? this.setData({
  52. ? ? ? ? ? itemHeight: res[0].height
  53. ? ? ? ? })
  54. ? ? ? ? this.startScrollAni()
  55. ? ? ? })
  56. ? ? },
  57. ? ? /** 开始滚动动画 */
  58. ? ? startScrollAni() {
  59. ? ? ? if (this.data.itemHeight <= 0) return
  60.  
  61. ? ? ? const aniArr = []
  62. ? ? ? this.data.valArr.forEach((item, index) => {
  63. ? ? ? ? if(!item.isNaN) {
  64. ? ? ? ? ? aniArr.push(`transition-delay: ${this.data.delay * index}ms; top: ${-this.data.itemHeight * (this.data.numArr[parseInt(item.val)] + 10)}px;`)
  65. ? ? ? ? } else {
  66. ? ? ? ? ? aniArr.push(null)
  67. ? ? ? ? }
  68. ? ? ? })
  69. ? ? ? this.setData({
  70. ? ? ? ? aniArr: aniArr
  71. ? ? ? })
  72. ? ? }
  73. ? }
  74. })

wxml文件

  1. <!--components/scroll-number/index.wxml-->
  2. <view class="scroll-number container-class">
  3. ? <block wx:for="{{valArr}}" wx:key="index">
  4. ? ? <view wx:if="{{item.isNaN}}" class="scroll-number-item number-dot dot-class">{{item.val}}</view>
  5. ? ? <view wx:else class="scroll-number-item number-item item-class">
  6. ? ? ? <view class="scroll-ani" style="transition-duration: {{duration}}ms; {{aniArr[index]}}">
  7. ? ? ? ? <view wx:for="{{numArr}}" wx:for-item="num" wx:for-index="idx" wx:key="idx" class="num{{num}}">{{num}}</view>
  8. ? ? ? ? <view wx:for="{{numArr}}" wx:for-item="num" wx:for-index="idx" wx:key="idx" class="num{{num}}">{{num}}</view>
  9. ? ? ? </view>
  10. ? ? </view>
  11. ? </block>
  12. </view>

wxss文件

  1. /* components/scroll-number/index.wxss */
  2. .scroll-number {
  3. ? display: flex;
  4. ? align-items: flex-end;
  5. }
  6. .scroll-number-item {
  7. ? color: #0079FE;
  8. ? font-size: 48rpx;
  9. ? font-weight: bold;
  10. ? margin: 0 24rpx;
  11. ? font-family: Microsoft YaHei;
  12. }
  13. .number-item {
  14. ? background-color: rgba(0, 121, 254, 0.2);
  15. ? border-radius: 8rpx;
  16. ? width: 56rpx;
  17. ? height: 72rpx;
  18. ? line-height: 72rpx;
  19. ? overflow: hidden;
  20. ? text-align: center;
  21. ? position: relative;
  22. }
  23. .number-dot {
  24. ? margin: 0 12rpx;
  25. }
  26. .scroll-ani {
  27. ? position: absolute;
  28. ? top: 0;
  29. ? left: 0;
  30. ? width: 100%;
  31. ? height: 100%;
  32. ? transition: all 2s ease-in-out 0s;
  33. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持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号