微信小程序数字滚动效果

效果图

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

相关推荐
魔云连洲3 小时前
详细解释浏览器是如何渲染页面的?
前端·css·浏览器渲染
Kx…………4 小时前
Day2—3:前端项目uniapp壁纸实战
前端·css·学习·uni-app·html
东部欧安时6 小时前
研一自救指南 - 07. CSS面向面试学习
前端·css
—Qeyser7 小时前
用 Deepseek 写的html油耗计算器
前端·javascript·css·html·css3·deepseek
ᥬ 小月亮9 小时前
uniapp(Vue)开发微信小程序 之 保存图片到本地
vue.js·微信小程序·uni-app
清风细雨_林木木9 小时前
小程序返回按钮,兼容所有机型的高度办法
微信小程序·小程序
汐汐咯9 小时前
HTML+CSS+JS(1)
javascript·css·html
code袁9 小时前
基于微信小程序的中医小妙招系统的设计与实现
微信小程序·小程序·notepad++·小程序开发·中医小妙招
倒霉男孩10 小时前
CSS继承
前端·css