better-scroll封裝成了組件 scroll.vue:
<template>
<div ref="wrapper">
<slot></slot>
</div>
</template>
<script>
import BScroll from 'better-scroll'
export default {
props: {
/**
* 1 滾動(dòng)的時(shí)候會(huì)派發(fā)scroll事件,會(huì)截流。
* 2 滾動(dòng)的時(shí)候?qū)崟r(shí)派發(fā)scroll事件,不會(huì)截流。
* 3 除了實(shí)時(shí)派發(fā)scroll事件,在swipe的情況下仍然能實(shí)時(shí)派發(fā)scroll事件
*/
probeType: {
type: Number,
default: 1
},
/**
* 點(diǎn)擊列表是否派發(fā)click事件
*/
click: {
type: Boolean,
default: true
},
/**
* 是否開(kāi)啟橫向滾動(dòng)
*/
scrollX: {
type: Boolean,
default: false
},
/**
* 是否派發(fā)滾動(dòng)事件
*/
listenScroll: {
type: Boolean,
default: false
},
/**
* 列表的數(shù)據(jù)
*/
data: {
type: Array,
default: null
},
/**
* 是否派發(fā)滾動(dòng)到底部的事件,用于上拉加載
*/
pullup: {
type: Boolean,
default: false
},
/**
* 是否派發(fā)頂部下拉的事件,用于下拉刷新
*/
pulldown: {
type: Boolean,
default: false
},
/**
* 是否派發(fā)列表滾動(dòng)開(kāi)始的事件
*/
beforeScroll: {
type: Boolean,
default: false
},
/**
* 當(dāng)數(shù)據(jù)更新后,刷新scroll的延時(shí)。
*/
refreshDelay: {
type: Number,
default: 20
}
},
mounted() {
setTimeout(() => {
this._initScroll
}, 20)
},
methods: {
_initScroll() {
if (!this.$refs.wrapper) {
return
}
this.scroll = new Bscroll(this.$refs.wrapper, {
probeType: this.probeType,
click: this.click,
scrollX: this.scrollX
})
// 是否派發(fā)滾動(dòng)事件
if (this.listenScroll) {
this.scroll.on('scroll', pos => {
this.$emit('scroll', pos)
})
}
// 是否派發(fā)滾動(dòng)到底部事件,用于上拉加載
if (this.pullup) {
this.scroll.on('scrollEnd', () => {
// 滾動(dòng)到底部
if (this.scroll.y <= this.scroll.maxScrollY + 50) {
this.$emit('scrollToEnd')
}
})
}
// 是否派發(fā)列表滾動(dòng)開(kāi)始的事件
if (this.beforeScroll) {
this.scroll.on('beforeScrollStart', () => {
this.$emit('beforeScroll')
})
}
},
enable() {
this.scroll && this.scroll.enable()
},
disable() {
this.scroll && this.scroll.disable()
},
refresh() {
this.scroll && this.scroll.refresh()
},
scrollTo() {
// 代理better-scroll的scrollTo方法
this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
},
scrollToElement() {
// 代理better-scroll的scrollToElement方法
this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
}
},
watch: {
data() {
setTimeout(() => {
this.refresh()
}, this.refreshDelay)
}
}
}
</script>
<style scoped>
</style>
在singgerlists.vue里使用scroll:
<template>
<scroll :data="data" class="wrapper" ref="listview">
<ul class="content">
<li v-for="group in data" ref="listGroup" class="list-group">
<h4 class="list-group-title">{{group.title}}</h4>
<ul>
<li class="list-group-item" v-for="item in group.items">
<img class="avatar" v-lazy="'https://y.gtimg.cn/music/photo_new/T001R300x300M000' + item.id.id + '.jpg?max_age=259200'" alt="">
<span class="name">{{item.id.name}}</span>
</li>
</ul>
</li>
</ul>
</scroll>
</template>
<script>
import Scroll from 'base/scroll/scroll'
import { getData } from '../../common/dom'
export default {
props: {
data: {
type: Array,
default: []
}
},
methods: {},
components: {
Scroll
}
}
</script>
<style>
.wrapper {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.list-group {
padding-bottom: 30px;
}
.list-group-title {
height: 30px;
line-height: 30px;
padding-left: 20px;
}
.list-group-item {
display: flex;
align-items: center;
padding: 20px 0 0 30px;
}
.avatar {
width: 50px;
height: 50px;
border-radius: 50%;
}
.name {
margin-left: 20px;
}
.list-shortcut {
position: absolute;
z-index: 30;
right: 0;
top: 50%;
transform: translateY(-50%);
width: 20px;
padding: 20px 0;
border-radius: 10px;
text-align: center;
font-family: Helvetica;
}
.item {
padding: 3px;
line-height: 1;
}
.list-fixed {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
.fixed-title {
height: 30px;
line-height: 30px;
padding-left: 20px;
background: red;
}
.loading-container {
position: absolute;
width: 100%;
top: 50%;
transform: translateY(-50%);
}
</style>
在singer.vue里引入singerlists這個(gè)組件:
<template>
<div class="singer" ref="singer">
<singer-lists :data="singerList" ref="list"></singer-lists>
<router-view></router-view>
</div>
</template>
<script>
import Axios from 'axios'
import Singer from '../../common/singer'
import SingerLists from '../../base/singerlist/singerlists'
const HOT_NAME = '熱門(mén)'
const HOT_SINGER_LEN = 10
export default {
data() {
return {
singerList: [],
pulldown: true
}
},
computed: {
shortcutList() {
return this.singerList.map(item => {
return item.title.substr(0, 1)
})
}
},
methods: {
getSingerData() {
var url = '/api/v8/fcg-bin/v8.fcg'
const commonParams = {
g_tk: 5381,
inCharset: 'utf-8',
outCharset: 'utf-8',
notice: 0,
format: 'jsonp'
}
const data = Object.assign({}, commonParams, {
channel: 'singer',
page: 'list',
key: 'all_all_all',
pagesize: 100,
pagenum: 1,
hostUin: 0,
needNewCode: 0,
platform: 'yqq'
})
return Axios.get(url, {
headers: {
referer: 'https://c.y.qq.com',
host: 'c.y.qq.com'
},
params: data
}).then(response => {
var rusl = JSON.parse(response.request.response)
this.singerList = this._shortList(rusl.data.list)
console.log(this.singerList)
})
},
_shortList(list) {
let map = {
hot: {
title: HOT_NAME,
items: []
}
}
list.forEach((item, index) => {
if (index < HOT_SINGER_LEN) {
map.hot.items.push(
new Singer({
name: item.Fsinger_name,
id: item.Fsinger_mid
})
)
}
const key = item.Findex
if (!map[key]) {
map[key] = {
title: key,
items: []
}
}
map[key].items.push(
new Singer({
name: item.Fsinger_name,
id: item.Fsinger_mid
})
)
})
console.log(map)
// 有序列表,處理map
let hot = []
let ret = []
for (let key in map) {
let val = map[key]
if (val.title.match(/[a-zA-Z]/)) {
ret.push(val)
} else if (val.title === HOT_NAME) {
hot.push(val)
}
}
ret.sort((a, b) => {
return a.title.charCodeAt(0) - b.title.charCodeAt(0)
})
return hot.concat(ret)
}
this.$refs.listview.scrollToElement(this.$refs.listItem[anchorIndex], 0)
},
created() {
this.getSingerData()
},
components: {
// Scroll,
SingerLists
}
}
</script>
<style>
.singer {
position: fixed;
width: 100%;
top: 60px;
bottom: 0;
}
效果如圖:
不能滾動(dòng),看著像是沒(méi)有引入成功,但是直接引入的話:
這樣是引入成功的,昨天還是可以滾動(dòng)的,我不知道都改了什么 現(xiàn)在 也不能滾動(dòng)了
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專(zhuān)業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。