微信小程序数字滚动效果

效果图

.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,很少看博客,对你有帮助别忘记点赞收藏。

相关推荐
今日热点1 小时前
微信小程序主体变更公证全流程实操解析:场景条件、材料规范、驳回避坑与Python校验脚本实现
python·微信小程序·小程序
ly76893 小时前
CSS 从入门到进阶:系统掌握现代网页样式与布局
前端·css
前端技术官3 小时前
一行 CSS 新特性干掉 20 行 JavaScript ?
css·滚动驱动动画·容器查询·:has()·anchor positioning
fthux6 小时前
下一次提交,隔了八年:掘金小程序回来了
微信小程序·开源·github
Beginner x_u6 小时前
Tailwind CSS 速通:有 CSS 基础的实战入门
css·tailwindcss·工程化
ZiYiMing1 天前
miniprogram-automator 在 2026 年还能用吗?——官方 SDK 三处硬伤的排查与重建
微信小程序
WL_arm1 天前
Vibe Coding(氛围编程)入门
javascript·css·人工智能·html5
拖孩1 天前
一个人 + AI 做的小程序,上线 15 天赚了 10 块 5
前端·后端·微信小程序
show4331 天前
2026微信小程序创作工具生态技术趋势:从单一功能到AI整合型平台演进
人工智能·微信小程序·小程序