微信小程序实现九宫格

微信小程序使用样式实现九宫格布局

使用微信小程序实现九宫格样式,可以直接使用样式进行编写,具体图片如下:1、js代码:

javascript 复制代码
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 4
  },

  // 监听
  activeClick(e) {
    let index = e.currentTarget.dataset.tag;
    this.setData({
      current: index
    })
  }
})

2、wxml代码:

xml 复制代码
<view class="box">
  <block wx:for="{{9}}" wx:key="item" wx:for-item="item" wx:for-index="index">
    <view class="item {{current==index?'active':''}}" bind:tap="activeClick" data-tag="{{index}}">{{index}}</view>
  </block>
</view>

3、wxss代码:

css 复制代码
.box {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-gap: 10rpx;
  margin: 20rpx;
}

.item {
  background-color: #f0f0f0;
  padding: 60rpx;
  text-align: center;
}

.active {
  background-color: #2979ff;
  color: white;
}

4、json代码:

javascript 复制代码
{
  "usingComponents": {},
  "navigationBarTitleText": "九宫格",
  "navigationBarBackgroundColor": "#44ADFB"
}

更多示例,关注我,分享更多呦~

相关推荐
OpenTiny社区3 小时前
重磅预告|OpenTiny 亮相 QCon 北京,共话生成式 UI 最新技术思考
前端·开源·ai编程
前端老实人灬3 小时前
web前端面试题
前端
Moment3 小时前
AI 全栈指南:NestJs 中的 Service Provider 和 Module
前端·后端·面试
IT_陈寒3 小时前
为什么我的JavaScript异步回调总是乱序执行?
前端·人工智能·后端
Moment3 小时前
AI全栈入门指南:NestJs 中的 DTO 和数据校验
前端·后端·面试
小码哥_常4 小时前
告别RecyclerView卡顿!8个优化技巧让列表丝滑如德芙
前端
小村儿4 小时前
Harness Engineering:为什么你用 AI 越用越累?
前端·后端·ai编程
enoughisenough4 小时前
浏览器判断控制台是否开启
前端
Moment4 小时前
当前端开始做 Agent 后,我才知道 LangGraph 有多重要❗❗❗
前端·后端·面试
竹林8184 小时前
RainbowKit 快速集成多链钱包连接:从“连不上”到丝滑切换的踩坑实录
前端·javascript