給每篇文章添加個postIdcss
posts-data.jshtml
var local_database = [{ date: "2018/8/16", title: "荷塘月色", imgSrc: '/images/post/sls.JPG', content: '這幾天內心頗不寧靜。今晚在院子裏坐着乘涼,突然想起日日走過的荷塘,在這滿月的光裏,總該另有一番樣子吧。', reading: "100", collection: '50', avatar: '/images/avatar/1.png', postId:0 }, { date: "2018/7/15", title: "背影", imgSrc: '/images/post/bl.png', content: '我與父親不相見已二年餘了,我最不能忘記的是他的背影 。那年冬天,祖母死了,父憨穿封費莩渡鳳殺脯輯親的差使也交卸了,正是禍不單行的日子', reading: "120", collection: '150', avatar: '/images/avatar/2.png', postId: 1 }, { date: "2018/6/2", title: "濟南的冬天", imgSrc: '/images/post/crab.png', content: '對於一個在北平住慣的人,像我,冬天要是不颳風,便以爲是奇蹟;濟南的冬天是沒有風聲的。', reading: "80", collection: '50', avatar: '/images/avatar/3.png', postId: 2 }, { date: "2018/5/1", title: "江南之雨", imgSrc: '/images/post/vr.png', content: '江南之春雨如此纏綿,然煽情,如此醉,影青青之煙雨巷,雨絲風,潤之令人動心如此', reading: "80", collection: '50', avatar: '/images/avatar/3.png', postId: 3 }, { date: "2018/4/6", title: "憶江南", imgSrc: '/images/post/xiaolong.jpg', content: '昨晚和阿浩談起諸多童年記憶,不知不覺眼前浮現一片油菜花海,黃燦燦,一眼望不到頭,連空氣都帶着甜香。', reading: "80", collection: '50', avatar: '/images/avatar/4.png', postId: 4 }, ] module.exports = { postlist:local_database }
post.wxmlpython
事件文檔json
<block wx:for="{{postlist}}" wx:for-item="item"> <view catchtap="onPostTap" data-postid="{{item.postId}}"> <template is="postItem" data="{{...item}}" /> </view> </block>
post.jsapi
var postsData = require('../../data/posts-data.js') Page({ data: { postlist: postsData.postlist }, onPostTap: function (event) { var postId = event.currentTarget.dataset.postid; wx:wx.navigateTo({ url: 'post-detail/post-detail' }) } })
在posts目錄下新建post-detail目錄緩存
post-detail.wxmlapp
<view>這是文章詳情頁</view>
app.json全局配置xss
{ "pages": [ "pages/welcome/welcome", "pages/posts/post", "pages/posts/post-detail/post-detail" ], "window": { "navigationBarBackgroundColor": "#405f80" } }
如今點擊文章,就能夠跳轉到文章詳情頁面了。函數
post-detail.wxml佈局
<view class="container"> <image class="head-image" src="/images/post/sls.JPG"></image> <view class="author-date"> <image class="avatar" src="/images/avatar/2.png"></image> <text class="author">朱自清</text> <text class="const-text">發表於</text> <text class="date">一天前</text> </view> <text class="title">背影</text> <view class="tool"> <view class="circle-img"> <image src="/images/icon/collection.png"></image> <image class="share-img" src="/images/icon/share.png"></image> </view> <view class="horizon"></view> </view> <text class='detail'>我與父親不相見已二年餘了,我最不能忘記的是他的背影。那年冬天,祖母死了,父親的差使也交卸了,正是禍不單行的日子,我從北京到徐州,打算跟着父親奔喪回家。到徐州見着父親,看見滿院狼藉的東西,又想起祖母,不由簌簌地流下眼淚。父親說,「事已如此,沒必要難過,好在天無絕人之路!」</text> </view>
post-detail.wxss
.container{ display:flex; flex-direction: column; } .head-image{ width: 100%; height: 460rpx; } .author-date{ flex-direction: row; margin-left:30rpx; margin-top: 20rpx; } .avatar{ height: 64rpx; width: 64rpx; vertical-align: middle; } .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{ margin-left: 40rpx; font-size: 36rpx; font-weight: 700; margin-top: 30rpx; letter-spacing: 2px; color: #4b556c; } .tools{ 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; }
app.wxss
text{ font-family:MicroSoft yahei; font-size: 24rpx; }
效果預覽
post-detial.wxml添加一個播放按鈕圖片
<view class="container"> <image class="head-image" src="/images/post/sls.JPG"></image> <image class="audio" src="/images/music/music-start.png"></image> <view class="author-date"> <image class="avatar" src="/images/avatar/2.png"></image> <text class="author">朱自清</text> <text class="const-text">發表於</text> <text class="date">一天前</text> </view> <text class="title">背影</text> <view class="tool"> <view class="circle-img"> <image src="/images/icon/collection.png"></image> <image class="share-img" src="/images/icon/share.png"></image> </view> <view class="horizon"></view> </view> <text class='detail'>我與父親不相見已二年餘了,我最不能忘記的是他的背影。那年冬天,祖母死了,父親的差使也交卸了,正是禍不單行的日子,我從北京到徐州,打算跟着父親奔喪回家。到徐州見着父親,看見滿院狼藉的東西,又想起祖母,不由簌簌地流下眼淚。父親說,「事已如此,沒必要難過,好在天無絕人之路!」</text> </view>
post-detail.wxss
.audio{ width: 102rpx; height: 110rpx; position: absolute; left: 50%; margin-left: -51rpx; top: 180rpx; opacity: 0.6 }
post-detail.json添加導航欄文字
{ "navigationBarTitleText": "閱讀" }
效果
post-data.js
var local_database = [{ date: "2018/8/16", title: "荷塘月色", imgSrc: '/images/post/sls.JPG', content: '這幾天內心頗不寧靜。今晚在院子裏坐着乘涼,突然想起日日走過的荷塘,在這滿月的光裏,總該另有一番樣子吧。', reading: "100", collection: '50', avatar: '/images/avatar/1.png', postId: 0, headImgSrc: "/images/post/sls.JPG", author: "朱自清", datetime: "24小時前", detail: "這幾天內心頗不寧靜。今晚在院子裏坐着乘涼,突然想起日日走過的荷塘,在這滿月的光裏,總該另有一番樣子吧。月亮漸漸地升高了,牆外馬路上孩子們的歡笑,已經聽不見了;妻在屋裏拍着閏兒,迷迷糊糊地哼着眠歌。我悄悄地披了大衫,帶上門出去。沿着荷塘,是一條曲折的小煤屑路。這是一條幽僻的路;白天也少人走,夜晚更加寂寞。荷塘四面,長着許多樹,蓊蓊鬱鬱的。路的一旁,是些楊柳,和一些不知道名字的樹。沒有月光的晚上,這路上陰森森的,有些怕人。今晚卻很好,雖然月光也仍是淡淡的。路上只我一我的,揹着手踱着。這一片天地好像是個人;我也像超出了日常的本身,到了另外一個世界裏。我愛熱鬧,也愛冷靜;愛羣居,也愛獨處。像今晚上,一我的在這蒼茫的月下,什麼均可以想,什麼均可以不想,便覺是個自由的人。白天裏必定要作的事,必定要說的話,現 在均可不理。這是獨處的妙處,我且受用這無邊的荷香月色好了。" }, { date: "2018/7/15", title: "背影", imgSrc: '/images/post/bl.png', content: '我與父親不相見已二年餘了,我最不能忘記的是他的背影 。那年冬天,祖母死了,父憨穿封費莩渡鳳殺脯輯親的差使也交卸了,正是禍不單行的日子', reading: "120", collection: '150', avatar: '/images/avatar/2.png', postId: 1, headImgSrc: "/images/post/bl.png", author: "朱自清", datetime: "三天前", detail: "我與父親不相見已二年餘了,我最不能忘記的是他的背影。那年冬天,祖母死了,父親的差使也交卸了,正是禍不單行的日子,我從北京到徐州,打算跟着父親奔喪回家。到徐州見着父親,看見滿院狼藉的東西,又想起祖母,不由簌簌地流下眼淚。父親說,「事已如此,沒必要難過,好在天無絕人之路!」回家變賣典質,父親還了虧空;又借錢辦了喪事。這些日子,家中光景非常慘淡,一半爲了喪事,一半爲了父親賦閒。喪事完畢,父親要到南京謀事,我也要回北京唸書,咱們便同行。到南京時,有朋友約去遊逛,勾留了一日;第二日上午便須渡江到浦口,下午上車北去。父親由於事忙,本已說定不送我,叫旅館裏一個熟識的茶房陪我同去。他再三囑咐茶房,甚是仔細。但他終於不放心,怕茶房不妥帖;頗躊躇了一會。其實我那年已二十歲,北京已來往過兩三次,是沒有甚麼要緊的了。他躊躇了一會,終於決定仍是本身送我去。我兩三回勸他沒必要去;他只說,「沒關係,他們去很差!」 " }, { date: "2018/6/2", title: "濟南的冬天", imgSrc: '/images/post/crab.png', content: '對於一個在北平住慣的人,像我,冬天要是不颳風,便以爲是奇蹟;濟南的冬天是沒有風聲的。', reading: "80", collection: '50', avatar: '/images/avatar/3.png', postId: 2, headImgSrc: "/images/post/crab.png", author: "老舍", datetime: "一月前", detail: "對於一個在北平住慣的人,像我,冬天要是不颳風,便以爲是奇蹟;濟南的冬天是沒有風聲的。對於一個剛由倫敦回來的人,像我,冬天要能看得見日光,便以爲是怪事;濟南的冬天是響晴的。天然,在熱帶的地方,日光是永遠那麼毒,響亮的天氣,反有點叫人懼怕。但是,在北中國的冬天,而能有溫晴的天氣,濟南真得算個寶地。設若單單是有陽光,那也算不了出奇。請閉上眼睛想:一個老城,有山有水,全在天底下曬着陽光,暖和安適地睡着,只等春風來把它們喚醒,這是否是個理想的境界?小山整把濟南圍了個圈兒,只有北邊缺着點口兒。這一圈小山在冬天特別可愛,好像是把濟南放在一個小搖籃裏,它們安靜不動地低聲地說「大家放心吧,這兒準保暖和。真的,濟南的人們在冬天是面上含笑的。他們一看那些小山,心中便以爲有了着落,有了依靠." }, { date: "2018/5/1", title: "江南之雨", imgSrc: '/images/post/vr.png', content: '江南之春雨如此纏綿,然煽情,如此醉,影青青之煙雨巷,雨絲風,潤之令人動心如此', reading: "80", collection: '50', avatar: '/images/avatar/3.png', postId: 3, headImgSrc: "/images/post/crab.png", author: "老舍", datetime: "一月前", detail: "對於一個在北平住慣的人,像我,冬天要是不颳風,便以爲是奇蹟;濟南的冬天是沒有風聲的。對於一個剛由倫敦回來的人,像我,冬天要能看得見日光,便以爲是怪事;濟南的冬天是響晴的。天然,在熱帶的地方,日光是永遠那麼毒,響亮的天氣,反有點叫人懼怕。但是,在北中國的冬天,而能有溫晴的天氣,濟南真得算個寶地。設若單單是有陽光,那也算不了出奇。請閉上眼睛想:一個老城,有山有水,全在天底下曬着陽光,暖和安適地睡着,只等春風來把它們喚醒,這是否是個理想的境界?小山整把濟南圍了個圈兒,只有北邊缺着點口兒。這一圈小山在冬天特別可愛,好像是把濟南放在一個小搖籃裏,它們安靜不動地低聲地說「大家放心吧,這兒準保暖和。真的,濟南的人們在冬天是面上含笑的。他們一看那些小山,心中便以爲有了着落,有了依靠." }, { date: "2018/4/6", title: "憶江南", imgSrc: '/images/post/xiaolong.jpg', content: '昨晚和阿浩談起諸多童年記憶,不知不覺眼前浮現一片油菜花海,黃燦燦,一眼望不到頭,連空氣都帶着甜香。', reading: "80", collection: '50', avatar: '/images/avatar/4.png', postId: 4, headImgSrc: "/images/post/crab.png", author: "老舍", datetime: "一月前", detail: "對於一個在北平住慣的人,像我,冬天要是不颳風,便以爲是奇蹟;濟南的冬天是沒有風聲的。對於一個剛由倫敦回來的人,像我,冬天要能看得見日光,便以爲是怪事;濟南的冬天是響晴的。天然,在熱帶的地方,日光是永遠那麼毒,響亮的天氣,反有點叫人懼怕。但是,在北中國的冬天,而能有溫晴的天氣,濟南真得算個寶地。設若單單是有陽光,那也算不了出奇。請閉上眼睛想:一個老城,有山有水,全在天底下曬着陽光,暖和安適地睡着,只等春風來把它們喚醒,這是否是個理想的境界?小山整把濟南圍了個圈兒,只有北邊缺着點口兒。這一圈小山在冬天特別可愛,好像是把濟南放在一個小搖籃裏,它們安靜不動地低聲地說「大家放心吧,這兒準保暖和。真的,濟南的人們在冬天是面上含笑的。他們一看那些小山,心中便以爲有了着落,有了依靠." }, ] module.exports = { postlist: local_database }
post.js
var postsData = require('../../data/posts-data.js') Page({ data: { postlist: postsData.postlist }, onPostTap: function (event) { var postId = event.currentTarget.dataset.postid; wx:wx.navigateTo({ url: "post-detail/post-detail?id=" + postId }) } })
post-detail.js
var postsData = require('../../../data/posts-data.js') Page({ /** * 頁面的初始數據 */ data: { }, /** * 生命週期函數--監聽頁面加載 */ onLoad: function (options) { var postId = options.id; var postData = postsData.postlist[postId]; this.setData({ postData:postData }) }, })
post-detail.wxml
<view class="container"> <image class="head-image" src="{{postData.headImgSrc}}"></image> <image class="audio" src="/images/music/music-start.png"></image> <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.datetime}}</text> </view> <text class="title">{{postData.title}}</text> <view class="tool"> <view class="circle-img"> <image src="/images/icon/collection.png"></image> <image class="share-img" src="/images/icon/share.png"></image> </view> <view class="horizon"></view> </view> <text class='detail'>{{postData.detail}}</text> </view>
post-detail.wxml
<view class="circle-img"> <image wx:if="{{collected}}" catchtap='onCollectionTap' src="/images/icon/collection.png"></image> <image wx:else catchtap='onCollectionTap' src="/images/icon/collection-anti.png"></image> <image class="share-img" src="/images/icon/share.png"></image> </view>
post-detail.js
var postsData = require('../../../data/posts-data.js') Page({ data: { }, onLoad: function(options) { var postId = options.id; var postData = postsData.postlist[postId]; this.setData({ postData: postData, 'currentPostId': postId }) var postsCollected = wx.getStorageSync('posts_Collected') if (postsCollected) { var postCollected = postsCollected[postId] this.setData({ collected: postCollected }) } else { var postsCollected = {}; postsCollected[postId] = false; wx.setStorageSync('posts_Collected', postsCollected); } }, onCollectionTap: function(ev) { var postsCollected = wx.getStorageSync('posts_Collected') var postCollected = postsCollected[this.data.currentPostId] postCollected = !postCollected; postsCollected[this.data.currentPostId] = postCollected; // 更新文章是否收藏的緩存值 wx.setStorageSync('posts_Collected', postsCollected) // 更新數據綁定變量,實現切換圖片 this.setData({ collected: postCollected }) } })
能夠看到緩存的狀態