scroll.vue 8.1 KB
Newer Older
M
init  
miaodian 已提交
1 2
<template>
  <div ref="wrapper" class="cube-scroll-wrapper">
U
ustbhuangyi 已提交
3
    <div class="cube-scroll-content">
4
      <div ref="listWrapper" class="cube-scroll-list-wrapper">
A
AmyFoxFN 已提交
5 6 7 8 9 10
        <slot>
          <ul class="cube-scroll-list">
            <li @click="clickItem(item)" class="cube-scroll-item border-bottom-1px" v-for="item in data">{{item}}</li>
          </ul>
        </slot>
      </div>
M
init  
miaodian 已提交
11 12
      <slot name="pullup" :pullUpLoad="pullUpLoad" :isPullUpLoad="isPullUpLoad">
        <div class="cube-pullup-wrapper" v-if="pullUpLoad">
A
AmyFoxFN 已提交
13
          <div class="before-trigger" v-if="!isPullUpLoad">
M
init  
miaodian 已提交
14 15
            <span>{{ pullUpTxt }}</span>
          </div>
A
AmyFoxFN 已提交
16
          <div class="after-trigger" v-else>
M
init  
miaodian 已提交
17 18 19 20 21 22 23 24 25 26
            <loading></loading>
          </div>
        </div>
      </slot>
    </div>
    <slot
        name="pulldown"
        :pullDownRefresh="pullDownRefresh"
        :pullDownStyle="pullDownStyle"
        :beforePullDown="beforePullDown"
U
ustbhuangyi 已提交
27
        :isPullingDown="isPullingDown"
M
init  
miaodian 已提交
28 29 30 31 32 33
        :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 已提交
34
          <div v-if="isPullingDown" class="loading">
M
init  
miaodian 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
            <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 已提交
48
  import { getRect } from '../../common/helpers/dom'
M
init  
miaodian 已提交
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_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
  const DEFAULT_OPTIONS = {
D
dolymood 已提交
62
    observeDOM: true,
63 64 65 66 67 68 69
    click: true,
    probeType: 1,
    scrollbar: false,
    pullDownRefresh: false,
    pullUpLoad: false
  }

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

        return this.pullUpDirty ? moreTxt : noMoreTxt
      },
      refreshTxt() {
133 134
        const pullDownRefresh = this.pullDownRefresh
        return pullDownRefresh && pullDownRefresh.txt || DEFAULT_REFRESH_TXT
M
init  
miaodian 已提交
135 136 137 138 139 140
      }
    },
    created() {
      this.pullDownInitTop = -50
    },
    mounted() {
141
      this.$nextTick(() => {
F
funanamy 已提交
142
        this.initScroll()
143
      })
M
init  
miaodian 已提交
144 145
    },
    methods: {
F
funanamy 已提交
146
      initScroll() {
M
init  
miaodian 已提交
147 148 149
        if (!this.$refs.wrapper) {
          return
        }
150
        this._calculateMinHeight()
M
init  
miaodian 已提交
151

152
        let options = Object.assign({}, DEFAULT_OPTIONS, {
M
init  
miaodian 已提交
153
          scrollY: this.direction === DIRECTION_V,
154 155
          scrollX: this.direction === DIRECTION_H
        }, this.options)
M
init  
miaodian 已提交
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 184 185

        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() {
186
        this._calculateMinHeight()
M
init  
miaodian 已提交
187 188
        this.scroll && this.scroll.refresh()
      },
F
funanamy 已提交
189 190 191
      destroy() {
        this.scroll.destroy()
      },
M
init  
miaodian 已提交
192 193 194 195 196 197 198 199 200 201 202
      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 已提交
203
          this.isPullingDown = false
M
init  
miaodian 已提交
204 205 206 207 208 209 210 211 212 213 214 215
          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()
        }
      },
216 217 218 219 220
      _calculateMinHeight() {
        if (this.$refs.listWrapper && (this.pullDownRefresh || this.pullUpLoad)) {
          this.$refs.listWrapper.style.minHeight = `${getRect(this.$refs.wrapper).height + 1}px`
        }
      },
M
init  
miaodian 已提交
221 222 223 224
      _initPullDownRefresh() {
        this.scroll.on('pullingDown', () => {
          this.beforePullDown = false
          this.isPullingDown = true
225
          this.$emit(EVENT_PULLING_DOWN)
M
init  
miaodian 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        })

        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
            this.pullDownStyle = `top:${Math.min(pos.y - 30, 10)}px`
          }
        })
      },
      _initPullUpLoad() {
        this.scroll.on('pullingUp', () => {
          this.isPullUpLoad = true
241
          this.$emit(EVENT_PULLING_UP)
M
init  
miaodian 已提交
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 273 274 275 276 277 278 279
        })
      },
      _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">
280 281 282
  @require "../../common/stylus/variable.styl"

  .cube-scroll-wrapper
283
    position: relative
284
    height: 100%
285
    overflow: hidden
286

M
init  
miaodian 已提交
287 288 289 290 291 292 293 294 295
  .cube-pulldown-wrapper
    position: absolute
    width: 100%
    left: 0
    display: flex
    justify-content: center
    align-items: center
    transition: all
    .after-trigger
F
funanamy 已提交
296
      margin-top: 5px
M
init  
miaodian 已提交
297 298 299 300 301 302

  .cube-pullup-wrapper
    width: 100%
    display: flex
    justify-content: center
    align-items: center
303 304
    .before-trigger
      padding: 22px 0
A
AmyFoxFN 已提交
305
      min-height: 1em
306 307
    .after-trigger
      padding: 18px 0
U
ustbhuangyi 已提交
308 309 310 311

  .cube-scroll-content
    position: relative
    z-index: 1
312

313 314 315 316 317
  .cube-scroll-item
    height: 60px
    line-height: 60px
    font-size: $fontsize-large-x
    padding-left: 20px
M
init  
miaodian 已提交
318 319
</style>