10.5.2 页面渲染

为了解决这个问题,我们给图片添加一个渐变的遮罩,就像图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 30rpx 50rpx;
    color: #fff;
}

.top-info-text {
    -webkit-box-flex: 1;
    margin-right: 20rpx;
}
.top-info-title {
    font-size: 48rpx;
    line-height: 72rpx;
    white-space: nowrap;
    overflow: hidden;
}
.top-info-base {
    font-size: 28rpx;
    line-height: 40rpx;
}

“::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%)这行代码为我们建立了线性渐变的效果,这样我们的图片底部就会出现渐变为黑色的效果了。

剩下播放按钮的样式,这里因为用到了渐变的遮罩和背景图,为了达到最好的效果,这里选择的图片应该为透明背景的图片,如果你熟悉CSS的话,也可以使用代码创建这个图标。

.tl-top-play {
    position: relative;
    display: block;
    width: 84rpx;
    height: 84rpx;
    margin-left: 20rpx;
    background: url("../../resources/images/play_btn_play.png");
    background-size: cover;
}

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

//加载网络请求函数
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'
        });
    },

results matching ""

    No results matching ""