利用flex布局实现仿微信群的多头像九宫格自适应布局

2 篇文章 0 订阅

利用flex布局实现微信群的多头像自适应布局

微信群聊的头像是多个成员的组合,例如下图。
在这里插入图片描述
可以看到根据单个小头像的大小可以分为三类,分别可以看做是整个大头像的98%,47%和31%。所以可以根据单个数组的长度,分别匹配不同的样式。而为了模拟后台返回的多头像接口,需要额外做一个判断,即最多只能显示9张头像。

<div id="app">
    <div class="avatar" v-for="(avatarItem,index) in avatarList">
        <image v-for="(item,index) in avatarItem" :src="item" :class="computedAvatar(avatarItem)" v-if="index < 9"></image>
    </div>
</div>
//计算头像布局
computedAvatar(avatarList) {
    if(avatarList.length > 4) {
        return "avatarItem--3"
    }else if(avatarList.length > 1) {
        return "avatarItem--2"
    }else {
        return "avatarItem--1"
    }
}

要做到多排的自适应布局,最简单的就是利用flex布局。最基本的是居中,即justify-content: center;align-content: center;
除此之外,当单轴线排不下后,需换行。flex的换行方式有两种。分别是wrapwrap-reverse。这里用wrap-reverse
在这里插入图片描述

.avatar {
     width: 60px;
     height: 60px;
     overflow: hidden;
     display: flex;
     justify-content: center;
     align-items: center;
     align-content: center;
     flex-wrap: wrap-reverse;
     border: #000 1px solid;
     margin: 30px;
 }
 .avatarImg {
     width: 60px;
     height: 60px;
 }

 .avatarItem--1 {
     width: 98%;
     height: 98%;
 }

 .avatarItem--2 {
     width: 47%;
     height: 47%;
     margin: 1%;
 }

 .avatarItem--3 {
     width: 32%;
     height: 30%;
     margin: 1%;
 }
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现九宫格抽奖的微信小程序可以分为以下几个步骤: 1. 绘制九宫格布局,可以使用 `view` 和 `text` 标签来实现。 2. 定义一个数组用来存储九宫格每个格子的内容,包括奖品图片和名称等。 3. 编写一个随机数生成函数,用来随机选取一个格子作为中奖格子。 4. 给九宫格格子添加点击事件,点击后触发抽奖逻辑,根据随机数生成函数生成的结果判断是否中奖,如果中奖则弹出中奖提示框,否则弹出未中奖提示框。 下面是一个简单的示例代码: ```html <view class="container"> <!-- 绘制九宫格布局 --> <view class="grid"> <view class="item" bindtap="onTap"> <image src="奖品图片1"></image> <text>奖品名称1</text> </view> <view class="item" bindtap="onTap"> <image src="奖品图片2"></image> <text>奖品名称2</text> </view> <!-- 其他格子省略 --> </view> </view> ``` 在 JS 文件中定义九宫格格子的内容数组和随机数生成函数: ```javascript const items = [ { image: '奖品图片1', name: '奖品名称1' }, { image: '奖品图片2', name: '奖品名称2' }, // 其他格子省略 ]; function random(max) { return Math.floor(Math.random() * max); } ``` 在 `onTap` 函数中实现抽奖逻辑: ```javascript function onTap(event) { const index = event.currentTarget.dataset.index; const item = items[index]; if (random(9) === 0) { wx.showModal({ title: '恭喜中奖', content: `您获得了${item.name}`, showCancel: false, }); } else { wx.showModal({ title: '很遗憾', content: '未中奖,请再接再厉', showCancel: false, }); } } ``` 注意,这里的 `random(9)` 表示生成一个 0 到 8 的随机数,表示有 1/9 的概率中奖。实际中可以根据需要调整中奖概率,例如设置为 `random(10)` 表示有 1/10 的概率中奖。 最后在 CSS 文件中定义九宫格布局和格子样式: ```css .container { display: flex; justify-content: center; align-items: center; height: 100vh; } .grid { display: flex; flex-wrap: wrap; width: 300rpx; height: 300rpx; border: 1rpx solid #ccc; } .item { display: flex; flex-direction: column; justify-content: center; align-items: center; width: 100rpx; height: 100rpx; border: 1rpx solid #ccc; } ``` 这样就可以实现一个简单的九宫格抽奖微信小程序了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值