2021-02-08

微信小程序-仿微信朋友圈

一、效果

在这里插入图片描述

二、实现

1.wxml

<view class="item" wx:for="{{list}}">
  <view class="left">
    <image class="avatar"></image>
  </view>
  <view class="right">
    <view class="nickname">{{item.nickName}}</view>
    <view class="content">{{item.content}}</view>
    <view class="image-list">
      <image class="image" wx:for="{{item.imageList}}"></image>
    </view>
    <view class="time-area">
      <view class="time">{{item.time}}</view>
      <view>
        <image class="operation-button" src="../../images/caozuo.png"></image>
        <view class="operation-pannel">
          <view class="tab">
            <image class="image" src="../../images/love-white.png"></image>
            <text></text>
          </view>
          <view class="tab">
            <image class="image" src="../../images/comment-white.png"></image>
            <text>评论</text>
          </view>
        </view>
      </view>
      
    </view>
    <view class="love-comment">
      <!-- 
        item.loveList=重复
        item.loveList
        <view class="love" wx:if="{{item.loveList.length > 0}}">
        <image class="love-icon" src="../../images/love-blue.png"></image>
        <text class="love-nickname" wx:for="{{item.loveList}}">老夫子 兰陵王</text>
      </view>
       -->
      <view class="love" wx:if="{{item.loveList.length > 0}}">
        <image class="love-icon" src="../../images/love-blue.png"></image>
        <!-- love和整个循环的item不冲突 -->
        <text class="love-nickname" wx:for-items="{{item.loveList}}"
        wx:for-item = "love"
        >{{love.nickName}}</text>
      </view>
      <view class="comment" wx:if="{{item.commentList.length > 0}}">
        <view wx:for-items="{{item.commentList}}"
        wx:for-item = "comment">
          <text class="comment-nickname">{{comment.nickName}}</text>
          <text class="comment-content">{{comment.content}}</text>
        </view>
      </view>
    </view>
  </view>
</view>

2.js实现

// pages/circle/list.js
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    //假写数据绑定
    for (var i = 1; i < 10; i++) {
      // 定义一个对象存储数据
      var circleData = {};
      circleData.nickName = "朋友-" + i;
      circleData.content = "朋友发布内容-" + i;
      circleData.time = "2020-05-0" + i;

      //图片列表
      var imageList = [];
      // 点赞列表
      var loveList = [];
      // 评论列表
      var commentList = [];


      // 这三个数组赋值给circleData
      circleData.imageList = imageList;
      circleData.loveList = loveList;
      circleData.commentList = commentList;

      // 给3个数组赋值
      for (var j = 1; j < i; j++) {
        // 图片路径,占位
        imageList.push(j);
        // loveList,定义loveData对象
        var loveData = {};
        loveData.nickName = '点赞-' + j;
        // 点赞数组加入loveList
        loveList.push(loveData);

        // 评论数据
        var commentData = {};
        commentData.nickName = "兰陵王-" + j + ":";
        commentData.content = "评论内容-" + j;
        // 加入数据
        commentList.push(commentData);
      }

      that.data.list.push(circleData);
    }
    // 重新渲染
    that.setData({
      list: that.data.list
    })
  },
})

3.wxss实现

/* 页面设置白色 */
page{
  background-color: #fff;
}

/* 整个页面的宽度时750rpx
  设置左右边距20rpx
  头像设置80rpx
  头像据右侧10rpx
  右侧剩余:750-80-20-20-10 = 620
*/
.item{
  display: flex;
  width: 100%;
  padding: 20rpx 20rpx;
  border-bottom: 1px solid #f0f0f0;
}
/* 左侧,宽80,距离右侧10rpx:即头像的位置 */
.left{
  width: 80rpx;
  margin-right: 10rpx;
}
.avatar{
  width: 80rpx;
  height: 80rpx;
  margin-right: 10rpx;
  background: #f3f3f3;
}

.right{
  /* 右侧剩余 */
  width: 620rpx;
  /* flex布局 */
  display: flex;
  /* 按列排 */
  flex-direction: column;
}
.nickname{
  font-size: 32rpx;
  line-height: 50rpx;
  color: #3b4977;
}

.content{
  font-size: 32rpx;
  line-height: 40rpx;
  color: #181818;
  margin-bottom: 10rpx;
}

.image-list{
  display: flex;
  /* 可以换行 */
  flex-wrap: wrap;
}
.image-list .image{
  width: 160rpx;
  height: 160rpx;
  /* 设置背景颜色,未加载时占位 */
  background: #f3f3f3;
  /* 图片排列空隙 */
  margin-right: 10rpx;
  margin-bottom: 10rpx;
}

.time-area{
  display: flex;
  height: 72rpx;
  /* 相对定位 */
  position: relative;
  align-items: center;
}
.time-area .time{
  font-size: 26rpx;
  /* 和父元素相同 */
  line-height: 70rpx;
  color: #696969;
}
.time-area .operation-button{
  height: 40rpx;
  width: 70rpx;
  /* 绝对定位,相对定位里又设置,要生效就要先设置relative */
  /* 在父元素中的绝对位置 */
  position: absolute;
  /* 时间区域最右侧 */
  right: 0rpx;
  /* (70-40)  / 2 */
  top: 15rpx;
}
.time-area .operation-pannel{
  position: absolute;
  /* 行高跟父控件一样了 */
  top: 0rpx;
  /* 离操作按钮还有10rpx */
  right: 80rpx;
  width: 300rpx;
  line-height: 70rpx;
  background-color: #4c5154;
  border-radius: 10rpx;
  font-size: 24rpx;
  color: white;
  /* 文字居中 */
  text-align: center;
}
.time-area .operation-pannel .tab{
  /* 不换行 */
  display: inline-block;
  /* 平分300 */
  width: 150rpx;
}
.time-area .operation-pannel .tab .image{
  width: 26rpx;
  height: 26rpx;
  /* 图片居中 */
  vertical-align: middle;
  /* 透明的 */
  background-color: transparent;
}
.love-comment{
  margin-top: 10rpx;
  background-color: #f7f7f7;
  width: 100%;
}

.love{
  padding: 6rpx 16rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.love .love-icon{
  width: 26rpx;
  height: 26rpx;
}

.love .love-nickname{
  font-size: 30rpx;
  line-height: 40rpx;
  color: #3b4977;
}
.comment{
  padding: 6rpx 16rpx;
  padding-bottom: 12rpx;
  font-size: 30rpx;
}
.comment .comment-nickname{
  color: #3b4977;
}
.comment .comment-content{
  color: #181818;
  line-height: 40rpx;
}

三、记录

代码差异查看网站
传送门
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值