二.Vue2.5开发去哪儿网app城市列表页③——Better-scroll的使用和字母表布局

插件链接https://github.com/ustbhuangyi/better-scroll

安装Better-scroll

npm install better-scroll --save

这是Better-scroll插件结构要求,所以我们要在list下还要添加一个总div使其成为此格式

<div class="wrapper">
  <ul class="content">
    <li>...</li>
    <li>...</li>
    ...
  </ul>
  <!-- you can put some other DOMs here, it won't affect the scrolling -->
</div>

ref可以帮助我们获取DOM,所以在list类处添加

ref='wrapper'

然后在DOM挂载后创建一个Bscroll实例来使用这个DOM

  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  }

总代码

<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
          <div class="button-wrapper">
            <div class="button">北京</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">A</div>
        <ul class="item-list">
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
        </ul>
      </div>
      <div class="area">
        <div class="title border-topbottom">A</div>
        <ul class="item-list">
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
            <li class="item">阿拉尔</li>
        </ul>
      </div>
      <div class="area">
      <div class="title border-topbottom">A</div>
      <ul class="item-list">
          <li class="item">阿拉尔</li>
          <li class="item">阿拉尔</li>
          <li class="item">阿拉尔</li>
          <li class="item">阿拉尔</li>
          <li class="item">阿拉尔</li>
          <li class="item">阿拉尔</li>
          <li class="item">阿拉尔</li>
      </ul>
    </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    hotCities: Array
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  }
}
</script>

<style lang="stylus" scoped>
  @import "~styles/varibles.styl"
  .border-topbottom
  &:before
    border-color #ccc
  &:after
    border-color #ccc
  .list
    overflow hidden
    position absolute
    top 1.58rem
    bottom 0
    left 0
    right 0
    .title
      line-height .44rem
      background #eee
      padding-left .2rem
      color #666
      font-size .26rem
    .button-list
      overflow hidden
      padding .1rem .6rem .1rem .1rem
      .button-wrapper
        float left
        width 33.33%
        .button
          margin .1rem
          padding .1rem 0
          text-align center
          border .02rem solid #ccc
          border-radius .06rem
    .item-list
      .item
        line-height .76rem
        padding-left .2rem
        border-bottom .02rem solid #ccc
</style>

 

上一篇:everyday


下一篇:2130 Problem D More is better