鍍金池/ 問(wèn)答/HTML/ qq音樂(lè),歌手頁(yè)面,引入better-scroll沒(méi)有效果

qq音樂(lè),歌手頁(yè)面,引入better-scroll沒(méi)有效果

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;
}

效果如圖:

clipboard.png

不能滾動(dòng),看著像是沒(méi)有引入成功,但是直接引入的話:

clipboard.png
這樣是引入成功的,昨天還是可以滾動(dòng)的,我不知道都改了什么 現(xiàn)在 也不能滾動(dòng)了

回答
編輯回答
愛(ài)礙唉

用$nextTick()試試,能獲取更新之后的DOM然后調(diào)用滾動(dòng)

2018年1月7日 21:30