小程序开发七:动态首页Home页面的搭建(假数据呈现)

ps:上一节我们对登录页面进行了大概的讲解和说明,这一节,我们重点讲动态首页Home的实现与逻辑分析;
传送门:
六:登录Login页面的实现与讲解


实现效果

首先关于动态首页,大家可以看看实现的效果图,

动态首页.png

开始编写如下代码

<view class="dynamicListContainer">
  <view class="dynamic1">
    <image>头像</image>
    <text>姓名</text>
    <text>日期</text>
    <text>内容</text>
    <image>图片</image>
    <image>点赞图标</image>
    <text>点赞数量</text>
    <image>评论图标</image>
    <text>评论数量</text>
  </view>
  <view class="dynamic2">
    <image>头像</image>
    <text>姓名</text>
    <text>日期</text>
    <text>内容</text>
    <image>图片</image>
    <image>点赞图标</image>
    <text>点赞数量</text>
    <image>评论图标</image>
    <text>评论数量</text>
  </view>
  .
  .
  .
 // 有几个就写多少个
</view>

对于一些重复样式的view,我们建议使用“Template”的模板来进行封装。


准备工作

首先我们在app.json中添加home页面,并且暂时先将home页面移至程序启动的第一页面。

"pages": [
    "pages/home/home",
    "pages/login/login"
  ],

编译运行;


image.png

接着我们还要创建一个名为“ images”的目录,再新建home的目录,home里面放入home页面所需要的图片资源文件。


image.png

接着让我们开始编写home的布局文件代码。

home.wxml

<view class="innerContainer">

  <view class="dynamicContainer">

    <view class="dynamicTopView">
      <image class="userAvatar" mode="aspectFill" src = "/images/home.avatar.png"></image>
      <view class="userNick-postDate">
        <text class="userNick" >名字</text>
        <text class="postDate">2019-01-01</text>
      </view>
      <image src="/images/home/moreIcon.png" class="moreIcon" ></image>
    </view>

    <text class="contentText"></text>动态内容

    <image class="contentImg"  mode="aspectFill" src="/images/home/basePic2.jpg"></image>

    <view class="dynamicBottomView">
     <image class="favoriteIcon" src="/images/home/xin.png"  mode="aspectFit"  ></image> 
      <text class="favoriteCount">11</text>
      <image class="commentIcon" src="/images/home/pinglun.png"></image>
      <text class="commentCount">22</text>
    </view>

    <view class="bottomLine"></view>

  </view>
  
</view>

home.wxss

.innerContainer {
  margin: 0 30rpx 20rpx 30rpx;
  /* height: 1200rpx; */
  display: flex;
  flex-direction: column;

}
.dynamicContainer {
  display: flex;
  flex-direction: column;
  padding: 10rpx 0;
}

.dynamicTopView {
  display: flex;
  flex-direction: row;
}

.userAvatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 40rpx;
  vertical-align: middle;
  background-color: lightgray;
}

.userNick-postDate {
  margin-left: 20rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.userNick {
  font-size: 28rpx;
  color: #333;
}

.postDate {
  font-size: 20rpx;
  color: lightgray;
}

.contentText {
  font-size: 28rpx;
  color: #555;
  margin-top: 20rpx;
}

.contentImg {
  margin-top: 20rpx;
  width: 200rpx;
  height: 200rpx;
  border-radius: 8rpx;
  margin-bottom: 20rpx;
  background-color: lightgray;
}

.dynamiBottomView {
  margin-top: 20rpx;
  height: 80rpx;
  display: flex;
  flex-direction: row;
}

.dynamicBottomView image {
  vertical-align: middle;
}

.favoriteIcon {
  width: 28rpx;
  height: 24rpx;
}

.commentIcon {
  width: 28rpx;
  height: 30rpx;
}

.dynamicBottomView text {
  font-size: 22rpx;
  color: #888;
  vertical-align: middle;
  padding-left: 10rpx;
}

.commentIcon {
  margin-left: 20rpx;
  margin-top: 2rpx;
}

.bottomLine {
  height: 2rpx;
  background-color:#f0f0f0;
  margin: 20rpx 0rpx 10rpx 0rpx;
}


.moreIcon{
  width: 40rpx;
  height: 40rpx;
  vertical-align: middle;
  margin-left: 440rpx;
  margin-top: 10rpx;

}

首先我们创建一个template的文件夹,专门用来存在项目中的模板文件,这里我们创建好“dynamicTemplate”的模板文件()

模板文件

Template的介绍:

注意:模板文件只有.wxml(布局文件)和.wxss(样式文件),在模板中不做任何的逻辑处理,故此次不需要.js和.json文件

dynamic-template.wxml

//假数据布局
//name是必须要的,作为引用的唯一标志符
<template name="dynamicTemplate">
  <view class="dynamicContainer">

    <view class="dynamicTopView">
      <image class="userAvatar" mode="aspectFill" src = "/images/home.avatar.png"></image>
      <view class="userNick-postDate">
        <text class="userNick" >名字</text>
        <text class="postDate">2019-01-01</text>
      </view>
      <image src="/images/home/moreIcon.png" class="moreIcon" ></image>
    </view>

    <text class="contentText"></text>动态内容

    <image class="contentImg"  mode="aspectFill" src="/images/home/basePic2.jpg"></image>

    <view class="dynamicBottomView">
     <image class="favoriteIcon" src="/images/home/xin.png"  mode="aspectFit"  ></image> 
      <text class="favoriteCount">11</text>
      <image class="commentIcon" src="/images/home/pinglun.png"></image>
      <text class="commentCount">22</text>
    </view>

    <view class="bottomLine"></view>

  </view>
</template>

dynamic-template.wxss

.dynamicContainer {
  display: flex;
  flex-direction: column;
  padding: 10rpx 0;
}

.dynamicTopView {
  display: flex;
  flex-direction: row;
}

.userAvatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 40rpx;
  vertical-align: middle;
  background-color: lightgray;
}

.userNick-postDate {
  margin-left: 20rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.userNick {
  font-size: 28rpx;
  color: #333;
}

.postDate {
  font-size: 20rpx;
  color: lightgray;
}

.contentText {
  font-size: 28rpx;
  color: #555;
  margin-top: 20rpx;
}

.contentImg {
  margin-top: 20rpx;
  width: 200rpx;
  height: 200rpx;
  border-radius: 8rpx;
  margin-bottom: 20rpx;
  background-color: lightgray;
}

.dynamiBottomView {
  margin-top: 20rpx;
  height: 80rpx;
  display: flex;
  flex-direction: row;
}

.dynamicBottomView image {
  vertical-align: middle;
}

.favoriteIcon {
  width: 28rpx;
  height: 24rpx;
}

.commentIcon {
  width: 28rpx;
  height: 30rpx;
}

.dynamicBottomView text {
  font-size: 22rpx;
  color: #888;
  vertical-align: middle;
  padding-left: 10rpx;
}

.commentIcon {
  margin-left: 20rpx;
  margin-top: 2rpx;
}

.bottomLine {
  height: 2rpx;
  background-color:#f0f0f0;
  margin: 20rpx 0rpx 10rpx 0rpx;
}


.moreIcon{
  width: 40rpx;
  height: 40rpx;
  vertical-align: middle;
  margin-left: 440rpx;
  margin-top: 10rpx;

}

编译运行:

image.png

我们此处来用一个循环来加载多条假数据

<view class="innerContainer">

  <block wx:for = "{{[1,2,3]}}" >

  <view class="dynamicContainer">

    <view class="dynamicTopView">
      <image class="userAvatar" mode="aspectFill" src = "/images/home.avatar.png"></image>
      <view class="userNick-postDate">
        <text class="userNick" >名字</text>
        <text class="postDate">2019-01-01</text>
      </view>
      <image src="/images/home/moreIcon.png" class="moreIcon" ></image>
    </view>

    <text class="contentText"></text>动态内容

    <image class="contentImg"  mode="aspectFill" src="/images/home/basePic2.jpg"></image>

    <view class="dynamicBottomView">
     <image class="favoriteIcon" src="/images/home/xin.png"  mode="aspectFit"  ></image> 
      <text class="favoriteCount">11</text>
      <image class="commentIcon" src="/images/home/pinglun.png"></image>
      <text class="commentCount">22</text>
    </view>

    <view class="bottomLine"></view>

  </view>

  </block>
</view>

编译:

image.png

这里我们就完成了一个粗略的动态列表页面了,只不过我们的所有内容都是以假数据的形式来展示的。

下一节,我将为大家介绍如何动态的展示数据,抛开假数据的形式,所有的内容 ,我们都从服务器后台来加载。

下一节:
传送门:
八:动态首页Home的数据绑定

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容