scroll.vue 8.0 KB
Newer Older
M
init  
miaodian 已提交
1 2
<template>
  <div ref="wrapper" class="cube-scroll-wrapper">
U
ustbhuangyi 已提交
3
    <div class="cube-scroll-content">
M
init  
miaodian 已提交
4
      <slot>
5 6
        <ul ref="list" class="cube-scroll-list">
          <li @click="clickItem(item)" class="cube-scroll-item border-bottom-1px" v-for="item in data">{{item}}</li>
M
init  
miaodian 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
        </ul>
      </slot>
      <slot name="pullup" :pullUpLoad="pullUpLoad" :isPullUpLoad="isPullUpLoad">
        <div class="cube-pullup-wrapper" v-if="pullUpLoad">
          <div class="before-trigger" v-if="!isPullUpLoad">
            <span>{{ pullUpTxt }}</span>
          </div>
          <div class="after-trigger" v-else>
            <loading></loading>
          </div>
        </div>
      </slot>
    </div>
    <slot
        name="pulldown"
        :pullDownRefresh="pullDownRefresh"
        :pullDownStyle="pullDownStyle"
        :beforePullDown="beforePullDown"
U
ustbhuangyi 已提交
25
        :isPullingDown="isPullingDown"
M
init  
miaodian 已提交
26 27 28 29 30 31
        :bubbleY="bubbleY">
      <div class="cube-pulldown-wrapper" :style="pullDownStyle" v-if="pullDownRefresh">
        <div class="before-trigger" v-if="beforePullDown">
          <bubble :y="bubbleY"></bubble>
        </div>
        <div class="after-trigger" v-else>
U
ustbhuangyi 已提交
32
          <div v-if="isPullingDown" class="loading">
M
init  
miaodian 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
            <loading></loading>
          </div>
          <div v-else><span>{{ refreshTxt }}</span></div>
        </div>
      </div>
    </slot>
  </div>
</template>

<script type="text/ecmascript-6">
  import BScroll from 'better-scroll'
  import Loading from '../loading/loading.vue'
  import Bubble from '../bubble/bubble.vue'
U
ustbhuangyi 已提交
46
  import { getRect } from '../../common/helpers/dom'
M
init  
miaodian 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60

  const COMPONENT_NAME = 'cube-scroll'
  const DIRECTION_H = 'horizontal'
  const DIRECTION_V = 'vertical'
  const DEFAULT_LOAD_TXT_MORE = 'Load more'
  const DEFAULT_LOAD_TXT_NO_MORE = 'No more data'
  const DEFAULT_REFRESH_TXT = 'Refresh success'

  const EVENT_SCROLL = 'scroll'
  const EVENT_BEFORE_SCROLL_START = 'before-scroll-start'
  const EVENT_CLICK = 'click'
  const EVENT_PULLING_DOWN = 'pulling-down'
  const EVENT_PULLING_UP = 'pulling-up'

61 62 63 64 65 66 67 68
  const DEFAULT_OPTIONS = {
    click: true,
    probeType: 1,
    scrollbar: false,
    pullDownRefresh: false,
    pullUpLoad: false
  }

M
init  
miaodian 已提交
69 70 71 72 73 74 75 76 77
  export default {
    name: COMPONENT_NAME,
    props: {
      data: {
        type: Array,
        default() {
          return []
        }
      },
78 79 80 81 82
      options: {
        type: Object,
        default() {
          return {}
        }
M
init  
miaodian 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      },
      listenScroll: {
        type: Boolean,
        default: false
      },
      listenBeforeScroll: {
        type: Boolean,
        default: false
      },
      direction: {
        type: String,
        default: DIRECTION_V
      },
      refreshDelay: {
        type: Number,
        default: 20
      }
    },
    data() {
      return {
        beforePullDown: true,
        isRebounding: false,
        isPullingDown: false,
        isPullUpLoad: false,
        pullUpDirty: true,
        bubbleY: 0,
        pullDownStyle: ''
      }
    },
    computed: {
113 114 115 116 117 118
      pullUpLoad() {
        return this.options.pullUpLoad
      },
      pullDownRefresh() {
        return this.options.pullDownRefresh
      },
M
init  
miaodian 已提交
119
      pullUpTxt() {
120 121 122
        const pullUpLoad = this.pullUpLoad
        const moreTxt = pullUpLoad && pullUpLoad.txt && pullUpLoad.txt.more || DEFAULT_LOAD_TXT_MORE
        const noMoreTxt = pullUpLoad && pullUpLoad.txt && pullUpLoad.txt.noMore || DEFAULT_LOAD_TXT_NO_MORE
M
init  
miaodian 已提交
123 124 125 126

        return this.pullUpDirty ? moreTxt : noMoreTxt
      },
      refreshTxt() {
127 128
        const pullDownRefresh = this.pullDownRefresh
        return pullDownRefresh && pullDownRefresh.txt || DEFAULT_REFRESH_TXT
M
init  
miaodian 已提交
129 130 131 132 133 134
      }
    },
    created() {
      this.pullDownInitTop = -50
    },
    mounted() {
135
      this.$nextTick(() => {
F
funanamy 已提交
136
        this.initScroll()
137
      })
M
init  
miaodian 已提交
138 139
    },
    methods: {
F
funanamy 已提交
140
      initScroll() {
M
init  
miaodian 已提交
141 142 143 144 145 146 147
        if (!this.$refs.wrapper) {
          return
        }
        if (this.$refs.list && (this.pullDownRefresh || this.pullUpLoad)) {
          this.$refs.list.style.minHeight = `${getRect(this.$refs.wrapper).height + 1}px`
        }

148
        let options = Object.assign({}, DEFAULT_OPTIONS, {
M
init  
miaodian 已提交
149
          scrollY: this.direction === DIRECTION_V,
150 151
          scrollX: this.direction === DIRECTION_H
        }, this.options)
M
init  
miaodian 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

        this.scroll = new BScroll(this.$refs.wrapper, options)

        if (this.listenScroll) {
          this.scroll.on('scroll', (pos) => {
            this.$emit(EVENT_SCROLL, pos)
          })
        }

        if (this.listenBeforeScroll) {
          this.scroll.on('beforeScrollStart', () => {
            this.$emit(EVENT_BEFORE_SCROLL_START)
          })
        }

        if (this.pullDownRefresh) {
          this._initPullDownRefresh()
        }

        if (this.pullUpLoad) {
          this._initPullUpLoad()
        }
      },
      disable() {
        this.scroll && this.scroll.disable()
      },
      enable() {
        this.scroll && this.scroll.enable()
      },
      refresh() {
        this.scroll && this.scroll.refresh()
      },
F
funanamy 已提交
184 185 186
      destroy() {
        this.scroll.destroy()
      },
M
init  
miaodian 已提交
187 188 189 190 191 192 193 194 195 196 197
      scrollTo() {
        this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
      },
      scrollToElement() {
        this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
      },
      clickItem(item) {
        this.$emit(EVENT_CLICK, item)
      },
      forceUpdate(dirty) {
        if (this.pullDownRefresh && this.isPullingDown) {
U
ustbhuangyi 已提交
198
          this.isPullingDown = false
M
init  
miaodian 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
          this._reboundPullDown().then(() => {
            this._afterPullDown()
          })
        } else if (this.pullUpLoad && this.isPullUpLoad) {
          this.isPullUpLoad = false
          this.scroll.finishPullUp()
          this.pullUpDirty = dirty
          this.refresh()
        } else {
          this.refresh()
        }
      },
      _initPullDownRefresh() {
        this.scroll.on('pullingDown', () => {
          this.$emit(EVENT_PULLING_DOWN)
          this.beforePullDown = false
          this.isPullingDown = true
        })

        this.scroll.on('scroll', (pos) => {
          if (this.beforePullDown) {
            this.bubbleY = Math.max(0, pos.y + this.pullDownInitTop)
            this.pullDownStyle = `top:${Math.min(pos.y + this.pullDownInitTop, 10)}px`
          } else {
            this.bubbleY = 0
          }

          if (this.isRebounding) {
            this.pullDownStyle = `top:${Math.min(pos.y - 30, 10)}px`
          }
        })
      },
      _initPullUpLoad() {
        this.scroll.on('pullingUp', () => {
          this.$emit(EVENT_PULLING_UP)
          this.isPullUpLoad = true
        })
      },
      _reboundPullDown() {
        const {stopTime = 600} = this.pullDownRefresh
        return new Promise((resolve) => {
          setTimeout(() => {
            this.isRebounding = true
            this.scroll.finishPullDown()
            this.isPullingDown = false
            resolve()
          }, stopTime)
        })
      },
      _afterPullDown() {
        setTimeout(() => {
          this.pullDownStyle = `top:${this.pullDownInitTop}px`
          this.beforePullDown = true
          this.isRebounding = false
          this.refresh()
        }, this.scroll.options.bounceTime)
      }
    },
    watch: {
      data() {
        setTimeout(() => {
          this.forceUpdate(true)
        }, this.refreshDelay)
      }
    },
    components: {
      Loading,
      Bubble
    }
  }

</script>

<style lang="stylus" rel="stylesheet/stylus">
273 274 275 276
  @require "../../common/stylus/variable.styl"

  .cube-scroll-wrapper
    height: 100%
A
AmyFoxFN 已提交
277
    background-color: $scroll-content-bgc
278

M
init  
miaodian 已提交
279 280 281 282 283 284 285 286 287
  .cube-pulldown-wrapper
    position: absolute
    width: 100%
    left: 0
    display: flex
    justify-content: center
    align-items: center
    transition: all
    .after-trigger
F
funanamy 已提交
288
      margin-top: 5px
M
init  
miaodian 已提交
289 290 291 292 293 294 295

  .cube-pullup-wrapper
    width: 100%
    display: flex
    justify-content: center
    align-items: center
    padding: 16px 0
U
ustbhuangyi 已提交
296 297 298 299

  .cube-scroll-content
    position: relative
    z-index: 1
300
    background-color: $scroll-content-bgc
301

302 303 304 305 306
  .cube-scroll-item
    height: 60px
    line-height: 60px
    font-size: $fontsize-large-x
    padding-left: 20px
M
init  
miaodian 已提交
307 308
</style>