微信小程序音乐播放器,音乐播放器小程序制作步骤(页面渲染)
页面渲染
为了解决这个问题,我们给图片添加一个渐变的遮罩,就像图10-8那样,这样到达文字部分时,背景就变成了黑色,不会影响文字的显示,而且达到了由图片到底下列表颜色渐变的效果,非常美观。
这个效果主要靠我们的格式文件实现,我们先写我们熟悉的部分。
- .list-top {
- position: relative;
- height: 100%;
- }
- .list-top::after {
- content: " ";
- display: block;
- padding-top: 100%;
- }
- .top-info {
- position: absolute;
- bottom: 0;
- width: 100%;
- z-index: 3;
- }
- .top-img {
- width: 100%;
- height: 100%;
- position: absolute;
- }
- .top-info-inner {
- display: -webkit-box;
- -webkit-box-align: center;
- margin: 0 15px 25px;
- color: #fff;
- }
- .top-info-text {
- -webkit-box-flex: 1;
- margin-right: 10px;
- }
- .top-info-title {
- font-size: 24px;
- line-height: 36px;
- white-space: nowrap;
- overflow: hidden;
- }
- .top-info-base {
- font-size: 14px;
- line-height: 20px;
- }
“::after”表示在“.list-top”后边添加,为了是在不修改布局文件的情况下,添加视图以达到美化的效果。
我们需要添加的遮罩为布局里“top—back”这部分,格式文件为:
- .tl-top-b {
- position: absolute;
- bottom: 0;
- width: 100%;
- background-image: -webkit-linear-gradient(top,transparent,currentColor 80%);
- }
- .tl-top-b::after {
- content: " ";
- display: block;
- padding-top: 60%;
- }
-webkit-linear-gradient(top,transparent,currentColor 80%)这行代码为我们建立了线性渐变的效果,这样我们的图片底部就会出现渐变为黑色的效果了。
剩下播放按钮的样式,这里因为用到了渐变的遮罩和背景图,为了达到最好的效果,这个按钮就不能用图片来显示了,我们使用代码来创建一个播放按钮。
- .tl-top-play {
- position: relative;
- display: block;
- width: 42px;
- height: 42px;
- margin-left: 10px;
- border: solid 3px;
- border-radius: 999px;
- }
- .tl-top-play::after {
- content: " ";
- position: absolute;
- left: 50%;
- top: 50%;
- margin-top: -10px;
- margin-left: -5px;
- display: inline-block;
- vertical-align: middle;
- width: 0;
- height: 0;
- border-style: solid;
- border-width: 10px 16px;
- border-color: transparent transparent transparent #fff;
- }
视图建立完毕,开始为视图填充数据。
- //加载网络请求函数
- var MusicService = require('../../services/music');
- //获取应用实例
- var app = getApp();
- Page({
- data: {
- // text:"这是一个页面"
- songList: [],
- imgUrl: '',
- id: 0,
- topinfo: {},
- update_time: '',
- },
- onLoad: function (options) {
- // 页面初始化 options为页面跳转所带来的参数
- var self = this;
- var id = app.globalData.topListId;
- this.setData({
- id: id
- });
- MusicService.getTopListInfo(id, this.getTopListCallback)
- },
- })
这里我们获取了保存于全局变量里的topListId(即我们点击的排行分类的ID),然后使用这个ID请求网络。
- getTopListCallback: function (data) {
- var imgUrl = data.topinfo.pic_album;
- this.setData({
- topinfo: data.topinfo,
- update_time: data.update_time
- });
- this.setSongList(data.songlist);
- },
使用回调函数为我们的data赋值之后,这里调用了setSongList这个方法,通过这个方法我们把返回数据里我们需要的内容保存到songList里。
- setSongList: function (songs) {
- var list = [];
- for (var i = 0; i < songs.length; i++) {
- var item = songs[i];
- var song = {};
- var album = {};
- album.mid = item.data.albummid
- album.id = item.data.albumid
- album.name = item.data.albumname;
- album.desc = item.data.albumdesc
- song.id = item.data.songid;
- song.mid = item.data.songmid;
- song.name = item.data.songname;
- song.title = item.data.songorig;
- song.subTitle = '';
- song.singer = item.data.singer;
- song.album = album;
- song.time_public = item.time_public;
- song.img = 'http://y.gtimg.cn/music/photo_new/T002R150x150M000' + album.mid + '.jpg?max_age=2592000'
- list.push(song);
- }
- this.setData({
- songList: list
- })
- }
最好完成此页面里的点击事件:
- mainTopTap: function (e) {
- var list = this.data.songList;
- app.setGlobalData({ //使用全局变量playList来保存我们当前的list
- playList: list,
- playIndex: 0 //表示从第一首歌曲开始播放
- });
- wx.navigateTo({
- url: '../play/play' //跳转到播放页
- });
- },
- musicItemTap: function (e) {
- var dataSet = e.currentTarget.dataset;
- var index = dataSet.index; //获取点击的item的序号
- var list = this.data.songList;
- app.setGlobalData({
- playList: list,
- playIndex: index //从点击歌曲开始播放
- });
- wx.navigateTo({
- url: '../play/play'
- });
- },
上一节:微信小程序小白项目开发案例之音乐播放器-获取列表页数据
下一节:微信小程序小白项目开发案例之音乐播放器-完成相似页面
第二部分:如何开通一个小商店
1、本网站发布的该篇文章,目的在于分享电商知识及传递、交流相关电商信息,以便您学习或了解电商知识,请您不要用于其他用途;
2、该篇文章中所涉及的商标、标识的商品/服务并非来源于本网站,更非本网站提供,与本网站无关,系他人的商品或服务,本网站对于该类商标、标识不拥有任何权利;
3、本网站不对该篇文章中所涉及的商标、标识的商品/服务作任何明示或暗示的保证或担保;
4、本网站不对文章中所涉及的内容真实性、准确性、可靠性负责,仅系客观性描述,如您需要了解该类商品/服务详细的资讯,请您直接与该类商品/服务的提供者联系。