多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # 总结 > 1.` wx.navigateTo` 跳转事件,通过`id`将页面从从yuedu跳转到yuedu-detail > 2. 获取数据 const local = require("../../../data/local"); > 3. 音乐播放的监听 可将监听变量存入app全局中,(页面有周期性,每次加载页面将重新运行周期) # yuedu-detail ## yuedu-detail.js ``` //获取data里的数据 const local = require("../../../data/local"); //获取app里的data 即globalData const app = getApp(); Page({ /** - 页面的初始数据 */ data: { // 音乐的播放初始状态 isPlay: false }, /** - 生命周期函数--监听页面加载 */ onLoad: function (options) { var id = options.id; var data = local.postList[options.id]; this.setData({ data, postId: id, music: local.postList[options.id].music }); this.onMusic(); // 进入页面退出页面音乐播放按钮一致 //判断g_isPlay是否为真 g_isPlay的Boolean值由音乐监听判断,当前id与页面id是否一致,当id一致时即进入的是相同的页面,音乐显示为播放,当不一致时显示为停止 if (app.globalData.g_isPlay && app.globalData.g_currentId == id) { this.setData({ isPlay: true }); } }, // 监听音乐播放 onMusic() { var self = this; //监听音乐播放 wx.onBackgroundAudioPlay((result) => { self.setData({ isPlay: true }); //当页面监听到音乐播放时 定义监听变量值 app.globalData.g_isPlay = true; app.globalData.g_currentId = self.data.postId; }); //监听音乐暂停。 wx.onBackgroundAudioPause((result) => { self.setData({ isPlay: false }); app.globalData.g_isPlay = false; app.globalData.g_currentId = null; }); //监听音乐停止 wx.onBackgroundAudioStop((result)=>{ self.setData({ isPlay:false }); }); }, // 点击音乐播放 playMusic() { var self = this; if (this.data.isPlay) { //停止播放音乐。如果音乐正在播放停止播放 wx.stopBackgroundAudio(); this.setData({ isPlay: false }); } else { // 使用后台播放器播放音乐 wx.playBackgroundAudio({ // 对音乐url title coverImgUrl 从新定义 dataUrl: self.data.music.url, title: self.data.music.title, coverImgUrl: self.data.music.coverImgUrl }); this.setData({ isPlay: true }); } } }) ``` ### 完成这个页面app.js里应增加的内容 存放音乐监听的变量值 > 在app.js里的存放是全局的变量,音乐播放对全局有效,所有要将音乐播放里的监听变量放入app.js,如果全局里没有定义,从详情页跳出后音乐会停止播放 ``` //app.js App({ globalData:{ g_isPlay:false, g_currentId:null } }) ``` ## yuedu-detail.json ``` { "navigationBarTitleText": "阅读" /* "window": { "navigationBarBackgroundColor": "#ffffff", "navigationBarTextStyle": "black", "navigationBarTitleText": "小程序", "backgroundColor": "#eeeeee", "backgroundTextStyle": "light", "enablePullDownRefresh": false } } */ window是对app.json设置的 要对单个页面进行设置不用加window,直接设置如上 ``` ## yuedu-detail.wxml ``` <!--pages/index/index-detail/index-detail.wxml--> <view class='indexDetail'> <view class='top'> <image src='{{isPlay?music.coverImg:data.imgSrc}}'></image> <image src="{{isPlay?'/images/music/music-start.png':'/images/music/music-stop.png'}}" class='music' bindtap="playMusic" ></image> </view> <view class='content'> <view class='evaluate'> <image src='{{data.avatar}}' class='avatar'></image> <text>{{data.author}}</text> 发表与 <text>{{data.dateTime}}</text> </view> <text class='title'>{{data.title}}</text> <view class='sc row'> <view class='line'></view> <image src='/images/icon/share.png'></image> <image src='/images/icon/collection.png'></image> </view> <view class='c-content'> {{data.detail}} </view> </view> </view> ``` ### 1. 三目运算 变换图片 ``` <image src="{{isPlay?'/images/music/music-start.png':'/images/music/music-stop.png'}}" class='music' bindtap="playMusic" ></image> ``` * 判断isPlay是否为真,不同情况下 访问路径不同 ## index-detail.wxss ``` /* pages/index/index-detail/index-detail.wxss */ .top .music{ width: 150rpx; height: 150rpx; position: absolute; top: 0; right: 0; z-index: 10; left: 0; bottom: 0; margin: auto; opacity: 0.5; } .top{ position: relative; } .top image{ width: 100%; } .content{ display: flex; flex-direction: column; padding: 0 15rpx; font-size: 24rpx; } .avatar{ width: 80rpx; height: 80rpx; vertical-align: bottom; position: relative; top: 20rpx; } .title{ margin: 25rpx 0 20rpx; font-size: 40rpx; font-weight: bold; } .sc{ position: relative; width: 100%; } .sc .line{ width: 100%; height: 4rpx; background: #999; position: absolute; top: 50rpx; z-index: -10; } .sc image{ width: 100rpx; height: 100rpx; vertical-align: bottom; float: right; margin-left: 15rpx; } .row::after{ display: block; content: ""; clear: both; } .c-content{ text-indent: 20rpx; padding: 25rpx 0; } ``` # 完成这个页面 yuedu 应增加的内容 ## yuedu.wxml > 获取点击事件的 id 将 id 传入相应页面里 相应页面通过id 获取数据, > wx.navigateTo 跳转事件可返回 > '路径?+id=' + id //第二个加是连接符 ``` handleClick(event){ // console.log(event.currentTarget.dataset) var id = event.currentTarget.dataset.id; wx.navigateTo({ url: 'yuedu-detail/yuedu-detail?id='+ id, }) } ``` ## yuedu.wxml ``` <block wx:for="{{postList}}" wx:key="index"> <view catchtap="handleClick" data-id='{{item.postId}}'> //对template设置点击事件无效,template 不占位,页面也不显示,因此加一个view设置事件 <template is="yueduList" data="{{...item}}" ></template> </view> </block> ```