温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序音乐播放器页面渲染的方法

发布时间:2022-03-09 10:14:12 来源:亿速云 阅读:222 作者:iii 栏目:开发技术

本文小编为大家详细介绍“微信小程序音乐播放器页面渲染的方法”,内容详细,步骤清晰,细节处理妥当,希望这篇“微信小程序音乐播放器页面渲染的方法”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

  页面渲染

  为了解决这个问题,我们给图片添加一个渐变的遮罩,就像图10-8那样,这样到达文字部分时,背景就变成了黑色,不会影响文字的显示,而且达到了由图片到底下列表颜色渐变的效果,非常美观。

  这个效果主要靠我们的格式文件实现,我们先写我们熟悉的部分。

  1. .list-top {

  2.     position: relative;

  3.     height: 100%;

  4. }

  5. .list-top::after {         

  6.     content: " ";

  7.     display: block;

  8.     padding-top: 100%;

  9. }

  10. .top-info {

  11.     position: absolute;

  12.     bottom: 0;

  13.     width: 100%;

  14.     z-index: 3;

  15. }

  16. .top-img {

  17.     width: 100%;

  18.     height: 100%;

  19.     position: absolute;

  20. }

  21.  

  22. .top-info-inner {

  23.     display: -webkit-box;

  24.     -webkit-box-align: center;

  25.     margin: 0 15px 25px;

  26.     color: #fff;

  27. }

  28.  

  29. .top-info-text {

  30.     -webkit-box-flex: 1;

  31.     margin-right: 10px;

  32. }

  33. .top-info-title {

  34.     font-size: 24px;

  35.     line-height: 36px;

  36.     white-space: nowrap;

  37.     overflow: hidden;

  38. }

  39. .top-info-base {

  40.     font-size: 14px;

  41.     line-height: 20px;

  42. }

复制代码

  “::after”表示在“.list-top”后边添加,为了是在不修改布局文件的情况下,添加视图以达到美化的效果。

  

  我们需要添加的遮罩为布局里“top—back”这部分,格式文件为:

  1. .tl-top-b {

  2.     position: absolute;

  3.     bottom: 0;

  4.     width: 100%;

  5.     background-image: -webkit-linear-gradient(top,transparent,currentColor 80%);

  6. }

  7. .tl-top-b::after {

  8.     content: " ";

  9.     display: block;

  10.     padding-top: 60%;

  11. }

复制代码

  -webkit-linear-gradient(top,transparent,currentColor 80%)这行代码为我们建立了线性渐变的效果,这样我们的图片底部就会出现渐变为黑色的效果了。

  剩下播放按钮的样式,这里因为用到了渐变的遮罩和背景图,为了达到最好的效果,这个按钮就不能用图片来显示了,我们使用代码来创建一个播放按钮。

  1. .tl-top-play {

  2.     position: relative;

  3.     display: block;

  4.     width: 42px;

  5.     height: 42px;

  6.     margin-left: 10px;

  7.     border: solid 3px;

  8.     border-radius: 999px;

  9. }

  10. .tl-top-play::after {

  11.     content: " ";

  12.     position: absolute;

  13.     left: 50%;

  14.     top: 50%;

  15.     margin-top: -10px;

  16.     margin-left: -5px;

  17.     display: inline-block;

  18.     vertical-align: middle;

  19.     width: 0;

  20.     height: 0;

  21.     border-style: solid;

  22.     border-width: 10px 16px;

  23.     border-color: transparent transparent  transparent #fff;

  24. }

复制代码

  视图建立完毕,开始为视图填充数据。

  1. //加载网络请求函数

  2. var MusicService = require('../../services/music');

  3. //获取应用实例

  4. var app = getApp();

  5.  

  6. Page({

  7.     data: {

  8.         // text:"这是一个页面"

  9.         songList: [],

  10.         imgUrl: '',

  11.         id: 0,

  12.         topinfo: {},

  13.         update_time: '',

  14.     },

  15.     onLoad: function (options) {

  16.         // 页面初始化 options为页面跳转所带来的参数

  17.         var self = this;

  18.         var id = app.globalData.topListId;

  19.         this.setData({

  20.             id: id

  21.         });

  22.         MusicService.getTopListInfo(id, this.getTopListCallback)

  23.     },

  24. })

复制代码

  这里我们获取了保存于全局变量里的topListId(即我们点击的排行分类的ID),然后使用这个ID请求网络。

  1. getTopListCallback: function (data) {

  2.         var imgUrl = data.topinfo.pic_album;

  3.         this.setData({

  4.             topinfo: data.topinfo,

  5.             update_time: data.update_time

  6.         });

  7.         this.setSongList(data.songlist);

  8.     },

复制代码

  使用回调函数为我们的data赋值之后,这里调用了setSongList这个方法,通过这个方法我们把返回数据里我们需要的内容保存到songList里。

  1. setSongList: function (songs) {

  2.         var list = [];

  3.         for (var i = 0; i < songs.length; i++) {

  4.             var item = songs[i];

  5.             var song = {};

  6.             var album = {};

  7.  

  8.             album.mid = item.data.albummid

  9.             album.id = item.data.albumid

  10.             album.name = item.data.albumname;

  11.             album.desc = item.data.albumdesc

  12.  

  13.             song.id = item.data.songid;

  14.             song.mid = item.data.songmid;

  15.             song.name = item.data.songname;

  16.             song.title = item.data.songorig;

  17.             song.subTitle = '';

  18.             song.singer = item.data.singer;

  19.             song.album = album;

  20.             song.time_public = item.time_public;

  21.             song.img = 'http://y.gtimg.cn/music/photo_new/T002R150x150M000' + album.mid + '.jpg?max_age=2592000'

  22.             list.push(song);

  23.         }

  24.         this.setData({

  25.             songList: list

  26.         })

  27.     }

复制代码

  最好完成此页面里的点击事件:

  1. mainTopTap: function (e) {

  2.         var list = this.data.songList;

  3.         app.setGlobalData({                //使用全局变量playList来保存我们当前的list

  4.             playList: list,

  5.             playIndex: 0                    //表示从第一首歌曲开始播放

  6.         });

  7.         wx.navigateTo({

  8.             url: '../play/play'            //跳转到播放页

  9.         });

  10.     },

  11.     musicItemTap: function (e) {

  12.         var dataSet = e.currentTarget.dataset;

  13.         var index = dataSet.index;                //获取点击的item的序号

  14.         var list = this.data.songList;

  15.         app.setGlobalData({

  16.             playList: list,

  17.             playIndex: index                      //从点击歌曲开始播放

  18.         });

  19.         wx.navigateTo({

  20.             url: '../play/play'

  21.         });

  22.     },

读到这里,这篇“微信小程序音乐播放器页面渲染的方法”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI