vue实现歌手列表字母排序下拉滚动条侧栏排序实时更新
程序员文章站
2023-12-04 16:08:28
今天写项目的时候遇到了歌手排序的问题,联想到了我们平时的手机通讯录侧栏字母排序,按照abcde这样的顺序对名字进行排序。
我们先来看看效果
那就用vue来实现一...
今天写项目的时候遇到了歌手排序的问题,联想到了我们平时的手机通讯录侧栏字母排序,按照abcde这样的顺序对名字进行排序。
我们先来看看效果
那就用vue来实现一遍
首先新建一个vue页面,取名为helloworld.vue
在页面里写入内容
<template> <div class="hello"> <div class="singer" id="singer"> <div class="singer-top-tag">{{singertoptag | filtersingertag}}</div> <ul class="singer-ul"> <li v-for="(item, index) in list" :key="index" class="singer-ul-li"> <div class="singer-tag" :id="item.tag">{{item.tag | filtersingertag}}</div> <ul> <li v-for="(fitem, findex) in item.data" :key="findex"> <img :src="`https://y.gtimg.cn/music/photo_new/t001r300x300m000${fitem.fsinger_mid}.jpg?max_age=2592000`"> <div>{{fitem.fsinger_name}}</div> </li> </ul> </li> </ul> </div> <div class="sort"> <ul> <li v-for="(item, index) in sortlist" :key="index" @click="jumptag(item)" :class="{current:currentsort == item ? true : false}" > {{item == `hot` ? `热` : item}} </li> </ul> </div> </div> </template>
<script> import axios from 'axios' export default { name: "helloworld", data() { return { list:[], // 歌手列表 sortlist:[], // 侧栏排序列表 currentsort: 'hot', // 当前排序的标签 singertoptag: 'hot', // 歌手栏头部的标签名字 }; }, mounted() { this.testdata() // 监听滚动条 window.addeventlistener('scroll', this.handlescroll) }, destroyed () { // 页面摧毁的时候要关闭监听 window.removeeventlistener('scroll', this.handlescroll) }, methods: { handlescroll () { let scrolltop = window.pageyoffset || document.documentelement.scrolltop || document.body.scrolltop let offsettop = 0 this.list.foreach((item,index) => { // 获取每个排序标签的位置 offsettop = document.queryselectorall('.singer-ul-li')[index].offsettop // 当前滚动条的位置 和 当前的标签偏移顶部的距离进行对比 // 每一个歌手的li标签的高度必须要保持一致,我这里的高度是70,可以计算自己项目的内容的具体高度进行修改 if (scrolltop > offsettop && scrolltop < (offsettop+ 70*item.data.length)) { this.singertoptag = item.tag this.currentsort = item.tag } }) }, // 请求数据 testdata(){ axios.get(`https://c.y.qq.com/v8/fcg-bin/v8.fcg?g_tk=1928093487&incharset=utf-8&outcharset=utf-8¬ice=0&format=jsonp&channel=singer&page=list&key=all_all_all&pagesize=100&pagenum=1&hostuin=0&neednewcode=0&platform=yqq&jsonpcallback=jp1`) .then(res => { res = res.data.substring(5,res.data.length-1) res = json.parse(res).data.list res = res.sort((a,b) => a.findex.localecompare(b.findex)) res.foreach((item,index) => { // 添加侧栏排序 item.findex = item.findex == 9 ? 'hot' : item.findex this.sortlist.push(item.findex) }) // 去除重复 this.sortlist = new set(this.sortlist) this.sortlist = [...this.sortlist] // 添加排序标签和歌手列表 this.sortlist.foreach(e => { this.list.push({ tag:e, data:res.filter(i => i.findex ==e) }) }) }) }, // 跳转标签 jumptag(i){ this.singertoptag = i this.currentsort = i document.queryselector(`#${i}`).scrollintoview() } }, filters :{ filtersingertag(i) { return i == `hot` ? `热门` : i } } }; </script> <!-- add "scoped" attribute to limit css to this component only --> <style scoped> .hello { position: relative; background-color: #222; } .singer { position: relative; width: 100%; height: 100%; overflow: hidden; background: #222; } .singer-top-tag { position: fixed; top: 0px; left: 0; width: 100%; height: 30px; line-height: 30px; padding-left: 20px; font-size: 12px; color: hsla(0,0%,100%,.5); background: #333; } .singer-tag { width: 100%; height: 30px; margin-bottom: 20px; line-height: 30px; padding-left: 20px; font-size: 12px; color: hsla(0,0%,100%,.5); background: #333; } .singer-ul-li ul li { list-style-type: none; display: flex; justify-content: flex-start; align-items: center; padding: 0 0 20px 20px; color: rgba(255, 255, 255, .5); } .singer-ul-li ul li img { border-radius: 50%; widows: 50px; height: 50px; } .singer-ul-li ul li div { padding-left: 20px; } .sort { position: fixed; z-index: 30; right: 0; top: 50%; -webkit-transform: translatey(-50%); transform: translatey(-50%); width: 20px; padding: 20px 0; border-radius: 10px; text-align: center; background: rgba(0,0,0,.3); font-family: helvetica; } ul { margin: 0; padding: 0; } .sort ul{ color: rgba(255,255,255,.6); } .sort ul li { list-style-type: none; padding: 3px; line-height: 1; font-size: 12px; } .current { color: #ffcd32; } </style>
我是使用的qq音乐接口,获取的数据需要进行处理,如果觉得麻烦可以自己写静态数据来代替
数据的格式
const list = [ { tag:`a`, data:[ { img:`https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558361071&di=0a522afe68fc7e2aa3af34cb5cd8c96a&imgtype=jpg&er=1&src=http%3a%2f%2fwerkstette.dk%2fwp-content%2fuploads%2f2015%2f09%2fentertainment_weekly_photographer_marc_hom_british_actor_charlie_hunnam_as_king_arthur_retouch_werkstette10-770x841.jpg`, fsinger_name:`奥巴里` } ] }, { tag:`b`, data:[ { img:`https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558361071&di=0a522afe68fc7e2aa3af34cb5cd8c96a&imgtype=jpg&er=1&src=http%3a%2f%2fwerkstette.dk%2fwp-content%2fuploads%2f2015%2f09%2fentertainment_weekly_photographer_marc_hom_british_actor_charlie_hunnam_as_king_arthur_retouch_werkstette10-770x841.jpg`, fsinger_name:`bigbang` } ] }, { tag:`c`, data:[ { img:`https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558361071&di=0a522afe68fc7e2aa3af34cb5cd8c96a&imgtype=jpg&er=1&src=http%3a%2f%2fwerkstette.dk%2fwp-content%2fuploads%2f2015%2f09%2fentertainment_weekly_photographer_marc_hom_british_actor_charlie_hunnam_as_king_arthur_retouch_werkstette10-770x841.jpg`, fsinger_name:`蔡依林` } ] }, { tag:`d`, data:[ { img:`https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558361071&di=0a522afe68fc7e2aa3af34cb5cd8c96a&imgtype=jpg&er=1&src=http%3a%2f%2fwerkstette.dk%2fwp-content%2fuploads%2f2015%2f09%2fentertainment_weekly_photographer_marc_hom_british_actor_charlie_hunnam_as_king_arthur_retouch_werkstette10-770x841.jpg`, fsinger_name:`邓紫棋` } ] }, ]
再者还要注意页面的img标签,直接复制上面的数据的话要对img标签进行修改,去掉http那一串,直接用:src="item.img"
代替
const list = [ { tag:`a`, data:[ { img:`https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558361071&di=0a522afe68fc7e2aa3af34cb5cd8c96a&imgtype=jpg&er=1&src=http%3a%2f%2fwerkstette.dk%2fwp-content%2fuploads%2f2015%2f09%2fentertainment_weekly_photographer_marc_hom_british_actor_charlie_hunnam_as_king_arthur_retouch_werkstette10-770x841.jpg`, fsinger_name:`奥巴里` } ] }, { tag:`b`, data:[ { img:`https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558361071&di=0a522afe68fc7e2aa3af34cb5cd8c96a&imgtype=jpg&er=1&src=http%3a%2f%2fwerkstette.dk%2fwp-content%2fuploads%2f2015%2f09%2fentertainment_weekly_photographer_marc_hom_british_actor_charlie_hunnam_as_king_arthur_retouch_werkstette10-770x841.jpg`, fsinger_name:`bigbang` } ] }, { tag:`c`, data:[ { img:`https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558361071&di=0a522afe68fc7e2aa3af34cb5cd8c96a&imgtype=jpg&er=1&src=http%3a%2f%2fwerkstette.dk%2fwp-content%2fuploads%2f2015%2f09%2fentertainment_weekly_photographer_marc_hom_british_actor_charlie_hunnam_as_king_arthur_retouch_werkstette10-770x841.jpg`, fsinger_name:`蔡依林` } ] }, { tag:`d`, data:[ { img:`https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558361071&di=0a522afe68fc7e2aa3af34cb5cd8c96a&imgtype=jpg&er=1&src=http%3a%2f%2fwerkstette.dk%2fwp-content%2fuploads%2f2015%2f09%2fentertainment_weekly_photographer_marc_hom_british_actor_charlie_hunnam_as_king_arthur_retouch_werkstette10-770x841.jpg`, fsinger_name:`邓紫棋` } ] }, ]
总结
以上所述是小编给大家介绍的vue实现歌手列表字母排序下拉滚动条侧栏排序实时更新,希望对大家有所帮助
上一篇: 类似新浪网的 弹出视频功能实现代码
下一篇: python通过加号运算符操作列表的方法