微信小程序入门与实战(4)——阅读详情页面的html/css页面,以及从阅读文章列表页面的数据传送

在这里插入图片描述

html页面

<!--pages/posts/post-detail/post-detail.wxml-->
<view class="container">
  <view class="head-container">
    <image class="audio" src="/img/music/music-stop.png"></image>
    <image class="head-image" src="/img/post/bl.png"></image>
  </view>
  <view class="author-date">
    <image class="avatar" src="/img/avatar/1.png"></image>
    <text class="author">用户名称</text>
    <text class="const-text">发表于</text>
    <text class="date">24小时之前</text>
  </view>
  <text class="title">文章标题</text>
  <view class="tool">
    <view class="circle-img">
      <image src="/img/icon/collection-anti.png"></image>
      <image class="share-img" src="/img/icon/share.png"></image>
    </view>
    <view class="horizon"></view>
  </view>
  <text class="detail">文章正文</text>
</view>

css

/* pages/posts/post-detail/post-detail.wxss */
.container{
  width: 100%;
  display: flex;
  flex-direction: column;
  padding-top: 0rpx;
}
.head-container{
  width: 100%;
  height: 460rpx;
  position: relative;
}
.head-image{
  width: 100%;
  height: 460rpx;
}
.audio{
  width:102rpx;
  height: 110rpx;
  position: absolute;
  top:50%;
  margin-top: -55rpx;
  left:50%;
  margin-left: -51rpx;
  opacity: 0.6;
}
.author-date{
  display: flex;
  flex-direction: row;
  margin-top: 20rpx;
  width: 100%;
  height: 64rpx;
  line-height: 64rpx;
}
.avatar{
  height: 64rpx;
  width:64rpx;
  vertical-align: middle;
  margin-left: 20rpx;
}
.author{
  font-size:30rpx;
  font-weight: 300;
  margin-left: 20rpx;
  vertical-align: middle;
  color:#666;
  
}
.const-text{
  font-size: 24rpx;
  color:#999;
  margin-left: 20rpx;
}

.date{
  font-size: 24rpx;
  margin-left: 30rpx;
  vertical-align: middle;
  color:#999;
}
.title{
  width: 100%;
  padding-left: 50rpx;
  font-size: 36rpx;
  font-weight: 700;
  margin-top: 30rpx;
  letter-spacing: 2px;
  color:#4b556c;
}
.tool{
  margin-top: 20rpx;
}
.circle-img{
  float: right;
  margin-right: 40rpx;
  vertical-align: middle;
}

.circle-img image{
  width: 90rpx;
  height: 90rpx;
}
.share-img{
  margin-left: 30rpx;
}

.horizon{
  width: 660rpx;
  height:1px;
  background-color: #e5e5e5;
  vertical-align: middle;
  position: relative;
  top:46rpx;
  margin: 0 auto;
  z-index: -99;
}

.detail{
  color:#666;
  margin-left: 30rpx;
  margin-top: 20rpx;
  margin-right: 30rpx;
  line-height: 44rpx;
  letter-spacing: 2px;
  
}

post.detail页面,要根据在post.detail中点击的模块跳转到对应内容的页面
post.wxml

<!--pages/posts/posts.wxml-->
<swiper indicator-dots="{{true}}" vertical="{{false}}" autoplay="{{true}}" intervsl="3000">
  <swiper-item>
    <image src="/img/wx.png" />
  </swiper-item>
  <swiper-item>
    <image src="/img/vr.png" />
  </swiper-item>
  <swiper-item>
    <image src="/img/iqiyi.png" />
  </swiper-item>
</swiper>

<block wx:for="{{postLists}}" wx:for-item="item">
  <view catchtap="onPostTap" data-postId="{{item.postId}}">
    <view class="post-container">
      <view class="post-author-date">
        <image class="post-author" src="{{item.avatar}}"></image>
        <text class="post-date">{{item.date}}</text>
      </view>
      <text class="post-title">{{item.title}}</text>
      <image class="post-image" src="{{item.imgSrc}}"></image>
      <text class="post-content">{{item.content}}</text>
      <view class="post-like">
        <image class="post-like-image" src="../../img/icon/chat.png"></image>
        <text class="post-like-font">{{item.reading}}</text>
        <image class="post-like-image" src="../../img/icon/view.png"></image>
        <text class="post-like-font">{{item.collection}}</text>
      </view>
    </view>
  </view>

</block>

在这里插入图片描述

post.js

// pages/posts/posts.js

import {postLists} from '../../data/data.js'

Page({

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      postLists
    })
    // console.log(postLists)
  },
  
  //此次新增的代码
  onPostTap(event){
    console.log(event);
    var postId = event.currentTarget.dataset.postid;
    console.log(postId);
    wx.navigateTo({
      url: 'post-detail/post-detail?id='+postId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

此时打印event,就可以知道为什么要获取event.currentTarget.dataset.postid的值
在这里插入图片描述

然后post-detail.js获取从posts中获取到的postid的值,然后从data.js获取整个实例,然后重新编写posts-detail.wxml

先打桩看看怎么获取posts页面传送过来的postid的值

// pages/posts/post-detail/post-detail.js
import {postLists} from '../../../data/data.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options:",options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

在这里插入图片描述
post-detail.js

// pages/posts/post-detail/post-detail.js
import {postLists} from '../../../data/data.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options:",options);
    
    var postId = options.id;
    var postData = postLists[postId];
    this.setData({
      postData
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

post-detail.wxml

<!--pages/posts/post-detail/post-detail.wxml-->
<view class="container">
  <view class="head-container">
    <image class="audio" src="/img/music/music-stop.png"></image>
    <image class="head-image" src="{{postData.headImgSrc}}"></image>
  </view>
  <view class="author-date">
    <image class="avatar" src="{{postData.avatar}}"></image>
    <text class="author">{{postData.author}}</text>
    <text class="const-text">发表于</text>
    <text class="date">{{postData.date}}</text>
  </view>
  <text class="title">{{postData.title}}</text>
  <view class="tool">
    <view class="circle-img">
      <image src="/img/icon/collection-anti.png"></image>
      <image class="share-img" src="/img/icon/share.png"></image>
    </view>
    <view class="horizon"></view>
  </view>
  <text class="detail">{{postData.detail}}</text>
</view>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值