微信小程序数字滚动效果

效果图

.wxml

复制代码
<view class="container">
    <view class="container-num" wx:for="{{number}}" wx:key="index">
    <view class="num-container" style="--h:{{h}}px;--y:{{-item * h }}px;">
      <view wx:for="{{10}}" wx:key="index" class="num-item">
        {{index}}
      </view>
    </view>
  </view>
</view>
<button bind:tap="getRandomNum">生成随机数</button>

.js

复制代码
Page({
  data: {
    number:'0',
    h:30,
  },
  getRandomNum(){
    const num = `${Math.floor(Math.random() * 1000)}`;
    this.setData({
      number:num
    })
  }
})

.wxss

复制代码
.container{
  padding: 100rpx 0;
  display: flex;
  justify-content: center;
}
.container-num{
  overflow: hidden;
}
.num-container{
  height: var(--h);
  transform:translateY(var(--y));
  transition-duration: 1s;
  margin: 0 1px;
}
.num-item{
  height: var(--h);
  flex-direction: column;
  display: flex;
  align-items: center;
  justify-content: center;
}

遇到问题可以看我主页加我Q,很少看博客,对你有帮助别忘记点赞收藏。

相关推荐
什么时候星期五6 小时前
antd 4.x Tabs 点击阻止冒泡
css·react.js
八哥程序员6 小时前
从border-image 到 mask + filer 实现圆角渐变边框
前端·css
顾安r6 小时前
11.29 脚本游戏 单页面格斗游戏模板
前端·javascript·css·游戏·virtualenv
天蓝色的鱼鱼8 小时前
写Tailwind CSS像在写屎山?这锅该不该它背
前端·css
吹水一流8 小时前
微信小程序页面栈:从一个 Bug 讲到彻底搞懂
前端·微信小程序
Jing_Rainbow13 小时前
【 Weapp-3 /Lesson20(2025-11-04)】路虎卫士小程序开发详解:从架构到细节的深度解析🚙📱
微信·微信小程序·程序员
大怪v13 小时前
【创世界01】头脑热一把,我要带你纯前端手搓虚拟世界💪!
javascript·css·html
@大迁世界14 小时前
03.CSS嵌套 (Nesting)
前端·css
@大迁世界14 小时前
02.CSS变量 (Variables)
前端·css