微信小程序数字滚动效果

效果图

.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 小时前
基于微信小程序的英语词汇学习小程序
学习·微信小程序·小程序
陈振wx:zchen20081 小时前
CSS2-3
css·css3·css2
星尘库1 小时前
[开发者服务器响应] 发货请求调用失败. 【ret:172935489】
微信小程序·小程序·小游戏
开利网络1 小时前
第2天:构建多维标签体系——立体化勾勒客户轮廓
大数据·微信小程序
蜗牛攻城狮1 小时前
CSS中的 `dvh` 与 `vh`: 深入理解视口单位
前端·css
a1117762 小时前
电流卡片特效(html网页 开源)
javascript·css·css3
2601_949804921 天前
开源多商户商城源码最新版_适配微信小程序+H5+APP+PC多端
微信小程序·小程序
码云数智-大飞1 天前
2026主流自助建站平台对比评测
微信小程序
干前端1 天前
Vue3 组件库工程化实战:BEM 命名规范与 useNamespace 深度解析
前端·css
RFCEO1 天前
前端编程 课程十四、:CSS核心基础2:选择器优先级 + 伪类选择器(解决冲突+交互效果)
前端·css·交互·css选择器优先级判断规则详解·css important使用·css链接伪类lvha顺序·实现悬浮交互效果