微信小程序数字滚动效果

效果图

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

相关推荐
Lhuu(重开版17 小时前
CSS:动效布局动画
前端·css
不羁的fang少年20 小时前
前端常见问题(vue,css,html,js等)
前端·javascript·css
yivifu20 小时前
CSS Grid 布局详解(2025最新标准)
前端·css
姜太小白1 天前
【前端】CSS媒体查询响应式设计详解:@media (max-width: 600px) {……}
前端·css·媒体
蓝胖子的多啦A梦1 天前
ElementUI表格错位修复技巧
前端·css·vue.js·el-table表格错位
喂自己代言1 天前
HTML ``元素:链接外部资源的关键角色与用法
css·html
猴猴不是猴1 天前
js实现卷轴,中间可滑动方块,左右两侧对比
javascript·css·css3
y***86691 天前
前端CSS-in-JS方案
前端·javascript·css
项目題供诗1 天前
微信小程序黑马优购(项目)(一)
微信小程序·小程序
项目題供诗1 天前
微信小程序黑马优购(项目)(三)
微信小程序·小程序