微信小程序数字滚动效果

效果图

.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 分钟前
32.CSS魔术师 (CSS Houdini)
前端·css·人工智能·tensorflow·houdini
用户3153247795456 小时前
Tailwind CSS 学习手册
前端·css
拜无忧11 小时前
css卡片,重叠div,顶部错开,底部对齐
css
纯情小萝卜11 小时前
2026前端CSS黑科技技巧
css
whinc2 天前
🚀 两年小程序开发,我把踩过的坑做成了开源 Skills
前端·微信小程序·ai编程
Lupino2 天前
烧掉 10 刀 API 费,我才明白小程序虚拟列表根本不用“库”!
react.js·微信小程序
bluceli2 天前
CSS容器查询:响应式设计的新范式
前端·css
Bigger2 天前
CSS 这些年都经历了什么?一次看懂 CSS 的演化史
前端·css·前端工程化
小溪彼岸3 天前
是时候给想做小程序的小伙伴泼盆冷水了
微信小程序
大漠_w3cpluscom4 天前
使用 clip-path: shape() 创建 Squircle 形状
前端·css·weui