微信小程序学习(三)补充

尝试实现九宫格布局

实现目标:

  • 实现一个等宽等高的九宫格布局。
  • 每行有三个元素,共三行。
  • 子元素内容可以是按钮、图片或文本。

WXML 文件(九宫格布局)

html 复制代码
<view class="grid-container">
  <view class="grid-item" wx:for="{{gridData}}" wx:key="id">
    <text>{{item.text}}</text>
  </view>
</view>

JS 文件

cpp 复制代码
Page({
  data: {
    gridData: [
      { id: 1, text: '1' },
      { id: 2, text: '2' },
      { id: 3, text: '3' },
      { id: 4, text: '4' },
      { id: 5, text: '5' },
      { id: 6, text: '6' },
      { id: 7, text: '7' },
      { id: 8, text: '8' },
      { id: 9, text: '9' },
    ]
  }
});

WXSS 文件

css 复制代码
.grid-container {
  display: flex;
  flex-wrap: wrap; /* 子元素换行 */
  gap: 8px; /* 元素之间的间距 */
  justify-content: space-between; /* 子元素均匀分布 */
  background-color: #f5f5f5; /* 背景色 */
  padding: 16px; /* 内边距 */
}

.grid-item {
  width: calc(33.33% - 8px); /* 每行三列,减去间距 */
  height: 100px; /* 固定高度,确保等高 */
  text-align: center;
  line-height: 100px; /* 文本垂直居中 */
  background-color: #007aff; /* 元素背景色 */
  color: white; /* 文本颜色 */
  border-radius: 8px; /* 圆角 */
  font-size: 16px; /* 字体大小 */
}

创建瀑布流效果的产品列表

WXML 文件

html 复制代码
<view class="waterfall">
  <!-- 左列 -->
  <view class="column">
    <view class="product" wx:for="{{col1}}" wx:key="id">
      <image src="{{item.img}}" mode="widthFix"></image>
      <view class="name">{{item.name}}</view>
      <view class="price">¥{{item.price}}</view>
    </view>
  </view>

  <!-- 右列 -->
  <view class="column">
    <view class="product" wx:for="{{col2}}" wx:key="id">
      <image src="{{item.img}}" mode="widthFix"></image>
      <view class="name">{{item.name}}</view>
      <view class="price">¥{{item.price}}</view>
    </view>
  </view>
</view>

JS文件

javascript 复制代码
Page({
  data: {
    col1: [],
    col2: [],
    col1Height: 0,
    col2Height: 0,
    products: [
      { id: 1, name: '商品1', price: 99, img: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.RzU5hR-mzlIYuMuEGi-RpQHaJE?w=156&h=211&c=8&rs=1&qlt=90&o=6&pid=3.1&rm=2' },
      { id: 2, name: '商品2', price: 199, img: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.pRVKlm5kgzxD2GbMXdE5YAHaGu?w=203&h=211&c=8&rs=1&qlt=90&o=6&pid=3.1&rm=2' },
      { id: 3, name: '商品3', price: 299, img: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Jbm1xujyMshAY49FIOVEBgHaIH?w=199&h=218&c=7&r=0&o=7&pid=1.7&rm=3' },
      { id: 5, name: '商品3', price: 41, img: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.KyVzHnnAMW7FEuP4X3ICNQHaNJ?w=187&h=333&c=7&r=0&o=7&pid=1.7&rm=3' },
      { id: 4, name: '商品3', price: 55, img: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.M2stilLkI2iUYcIdEGV34AHaL8?w=196&h=317&c=7&r=0&o=5&pid=1.7' },
      { id: 6, name: '商品3', price: 2969, img: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.RzU5hR-mzlIYuMuEGi-RpQHaJE?w=156&h=211&c=8&rs=1&qlt=90&o=6&pid=3.1&rm=2' },
      { id: 7, name: '商品3', price: 75, img: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.RzU5hR-mzlIYuMuEGi-RpQHaJE?w=156&h=211&c=8&rs=1&qlt=90&o=6&pid=3.1&rm=2' },
      // 后续可以用接口动态加载
    ]
  },

  onLoad() {
    this.loadProducts();
  },

  // 加载产品并分列
  loadProducts() {
    const products = this.data.products;

    products.forEach(item => {
      wx.getImageInfo({
        src: item.img,
        success: res => {
          const windowWidth = wx.getWindowInfo().windowWidth;
          const imgHeight = res.height * (windowWidth * 0.48 / res.width);
          
          if (this.data.col1Height <= this.data.col2Height) {
            this.data.col1.push(item);
            this.data.col1Height += imgHeight;
          } else {
            this.data.col2.push(item);
            this.data.col2Height += imgHeight;
          }

          this.setData({
            col1: this.data.col1,
            col2: this.data.col2
          });
        }
      });
    });
  }
});

WXSS文件

css 复制代码
.waterfall {
  display: flex;
  justify-content: space-between;
  padding: 10px;
}

.column {
  width: 48%;
}

.product {
  margin-bottom: 10px;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.product image {
  width: 100%;
  display: block;
}

.name {
  font-size: 14px;
  padding: 5px;
}

.price {
  font-size: 16px;
  font-weight: bold;
  color: red;
  padding: 5px;
}

如遇到这种问题,

这时候需要在微信平台,进入:开发 → 开发管理 → 开发设置,找到 服务器域名 配置区域:downloadFile合法域名。然后刷新。

效果如图:

设计多层级导航菜单

WXML 文件

html 复制代码
<view class="menu-container">
  <!-- 一级菜单 -->
  <block wx:for="{{menuData}}" wx:key="id" wx:for-item="menu">
    <view class="menu-parent" bindtap="toggleMenu" data-id="{{menu.id}}" data-level="1" data-item="{{menu}}">
      <text>{{menu.name}}</text>
      <text class="arrow" wx:if="{{menu.children}}">{{menu.open ? '▲' : '▼'}}</text>
    </view>

    <!-- 二级菜单 -->
    <view class="menu-child" wx:if="{{menu.open}}">
      <block wx:for="{{menu.children}}" wx:key="id" wx:for-item="sub1">
        <view class="menu-second" bindtap="toggleMenu" data-id="{{sub1.id}}" data-level="2" data-parent="{{menu.id}}" data-item="{{menu}}">
          <text>{{sub1.name}}</text>
          <text class="arrow" wx:if="{{sub1.children}}">{{sub1.open ? '▲' : '▼'}}</text>
        </view>

        <!-- 三级菜单 -->
        <view class="menu-third" wx:if="{{sub1.open}}">
          <block wx:for="{{sub1.children}}" wx:key="id" wx:for-item="sub2">
            <view class="menu-leaf">
              <text>{{sub2.name}}</text>
            </view>
          </block>
        </view>
      </block>
    </view>
  </block>
</view>

JS 文件

cpp 复制代码
Page({
  data: {
    menuData: [
      {
        id: 1,
        name: '分类 A',
        open: false,
        children: [
          {
            id: 11,
            name: '子分类 A1',
            open: false,
            children: [
              { id: 111, name: '子分类 A1-1' },
              { id: 112, name: '子分类 A1-2' }
            ]
          },
          { id: 12, name: '子分类 A2' }
        ]
      },
      {
        id: 2,
        name: '分类 B',
        open: false,
        children: [
          { id: 21, name: '子分类 B1' }
        ]
      }
    ]
  },

  toggleMenu(e) {
    const id = e.currentTarget.dataset.id;
    let menuData = [...this.data.menuData];
    this.toggleOpenById(menuData, id);
    this.setData({ menuData });
  },
  
  // 递归查找并切换 open
  toggleOpenById(list, id) {
    list.forEach(item => {
      if (item.id === id) {
        item.open = !item.open;
      } else if (item.children) {
        this.toggleOpenById(item.children, id);
      }
    });
  }
  
});

WXSS 文件

css 复制代码
.menu-container {
  padding: 10px;
  background-color: #f8f8f8;
}

.menu-parent, .menu-second {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fff;
  padding: 10px;
  margin-bottom: 2px;
  border-radius: 4px;
}

.menu-second {
  padding-left: 20px;
  background-color: #fafafa;
}

.menu-third {
  padding-left: 40px;
  background-color: #f0f0f0;
}

.menu-leaf {
  padding: 8px 10px;
  border-bottom: 1px solid #ddd;
}

.arrow {
  color: #666;
}

效果:

相关推荐
Le1Yu2 小时前
2025-9-28学习笔记
java·笔记·学习
老华带你飞2 小时前
机电公司管理小程序|基于微信小程序的机电公司管理小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·小程序·机电公司管理小程序
yuxb733 小时前
Ceph 分布式存储学习笔记(三):块存储和对象存储管理
笔记·ceph·学习
yuxb733 小时前
Ceph 分布式存储学习笔记(一):介绍、部署与集群配置(上)
笔记·ceph·学习
涛声依旧4 小时前
基于springBoot鲜花商城小程序
java·spring·微信小程序
GoldenaArcher5 小时前
Postman 学习笔记 IV:Workflow、Newman 与 Mock Server 实战技巧
笔记·学习·postman
卷Java6 小时前
小程序前端功能更新说明
java·前端·spring boot·微信小程序·小程序·uni-app
卷Java6 小时前
小程序原生导航栏返回键实现
spring boot·云原生·微信小程序·uni-app
知识分享小能手6 小时前
微信小程序入门学习教程,从入门到精通,微信小程序常用API(下)——知识点详解 + 案例实战(5)
前端·javascript·学习·微信小程序·小程序·vue·前端开发