index.vue 12.3 KB
Newer Older
Nature's avatar
Nature committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
/**
* better-scroll vue封装
* @Author momoko
* @Date 2018/05
*/

<template>
  <div ref="scroll" :class="[c(),{'h-ios': isIos}]" class="content scroll-content">

    <div :class="c('__wrapper')">
      <div ref="scrollContent" class="scrollContent">
        <slot/>
      </div>

      <slot
        :pullUp="pullUp"
        :pullUpNow="pullUpNow"
        name="pullup"
      >
        <div v-if="pullUp" :class="c('__pullup')">
          <div v-if="!pullUpNow">
            <span>{{ pullUpTxt }}</span>
          </div>
          <div v-else>
            <Loading/>
          </div>
        </div>
      </slot>
    </div>

    <slot
      :pullDown="pullDown"
      :pullDownStyle="pullDownStyle"
      :pullDownBefore="pullDownBefore"
      :pullDownNow="pullDownNow"
      :bubbleY="bubbleY"
      name="pulldown"
    >
      <div v-if="pullDown" ref="pulldown" :style="pullDownStyle" :class="c('__pulldown')">
        <div v-if="pullDownBefore" :class="c('__pulldown__before')">
          <Bubble :y="bubbleY"/>
        </div>
        <div v-else :class="c('__pulldown__after')">
          <div v-if="pullDownNow">
            <Loading/>
          </div>
          <div v-else><span>{{ pullDownTxt }}</span></div>
        </div>
      </div>
    </slot>

  </div>
</template>

<script type="text/ecmascript-6">
import BScroll from 'better-scroll'
import Loading from './loading'
import Bubble from './bubble'
import mixin from './mixins'
import {timeout} from './utils'

import { detectOS } from '../../common/utils/index'

export default {
  name: 'Scroll',
  components: {
    Loading,
    Bubble,
  },
  mixins: [mixin],
  props: {
    probeType: {
      // 滚动事件监听类型
      type: Number,
      default: 1,
    },
    click: {
      // 开启点击事件代理
      type: Boolean,
      default: true,
    },
    listenScroll: {
      // 监听滚动
      type: Boolean,
      default: false,
    },
    listenBeforeScrollStart: {
      // 监听滚动开始前
      type: Boolean,
      default: false,
    },
    scrollX: {
      // 开启X轴滚动
      type: Boolean,
      default: false,
    },
    scrollY: {
      // 开启Y轴滚动
      type: Boolean,
      default: true,
    },
    scrollbar: {
      // 开启滚动条
      type: null,
      default: false,
    },
    pullDown: {
      // 启用下拉刷新
      type: Boolean,
      default: false,
    },
    pullDownConfig: {
      // 下拉刷新配置
      type: Object,
      default: () => ({
        threshold: 90, // 触发 pullingDown 的距离
        stop: 40, // pullingDown 正在刷新 hold 时的距离
        txt: '刷新成功',
      }),
    },
    pullUp: {
      // 启用上拉加载
      type: Boolean,
      default: false,
    },
    pullUpConfig: {
      // 上拉加载配置
      type: Object,
      default: () => ({
        threshold: 100, // 提前触发 pullingUp 的距离
        txt: {more: '上拉加载', noMore: '— 我是有底线的 —'},
      }),
    },
    startY: {
      // 起始Y位置
      type: Number,
      default: 0,
    },
    bounce: {
      // 回弹效果
      type: Boolean,
      default: true,
    },
    bounceTime: {
      // 回弹时间
      type: Number,
      default: 500,
    },
    preventDefaultException: {
      // 不阻止默认行为
      type: Object,
      default: () => ({
        tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/,
      }),
    },
    autoUpdate: {
      // 自动刷新高度:适用于简单场景,复杂场景请使用updateData/refreshData
      type: Boolean,
      default: false,
    },
    updateData: {
      // 引起更新上拉/下拉加载状态的数据(下拉刷新/上拉加载相关的数据)
      type: Array,
      default: null,
    },
    refreshData: {
      // 引起刷新高度的数据(不包含 updateData 内的数据)
      type: Array,
      default: null,
    },
    hasFoot: {
      // 底部按钮的配置
      type: Object,
      default: () => ({
        footFlag: false, // 提前触发 pullingUp 的距离
        height: 44,
      }),
    },
  },
  data () {
    return {
      pullDownBefore: true, // 下拉之前
      pullDownNow: false, // 正在下拉
      pullDownStyle: '', // 下拉样式
      pullUpNow: false, // 正在上拉
      pullUpFinally: false, // true表示到了上拉加载到了最底部
      isRebounding: false, // 正在回弹
      bubbleY: 0, // 气泡y坐标,
      isIos: false,
      fullScreen: true,
    }
  },
  computed: {
    // 下拉的文本
    pullDownTxt () {
      return this.pullDownConfig && this.pullDownConfig.txt
    },
    // 上拉的文本
    pullUpTxt () {
      const moreTxt = this.pullUpConfig && this.pullUpConfig.txt && this.pullUpConfig.txt.more

      const noMoreTxt = this.pullUpConfig && this.pullUpConfig.txt && this.pullUpConfig.txt.noMore

      return this.pullUpFinally ? noMoreTxt : moreTxt
    },
  },
  watch: {
    updateData () {
      this.update()
    },
    async refreshData () {
      if (this.updateState) return

      await this.$nextTick()
      this.refresh()
    },
  },

  created () {
    this.fullScreen && detectOS() === 'ios' && (this.isIos = true)
  },
  async mounted () {
    this.pullDownInitTop = parseInt(this.$refs.pulldown && getComputedStyle(this.$refs.pulldown).top) || -100
    await this.$nextTick()
    this.initScroll()

    // 自动刷新高度:深监视 $data,发生改变时更新高度
    this.autoUpdate && this.$parent && this.$parent.$data && this.$watch(() => this.$parent.$data, (val) => {
      this.update()
    }, {
      deep: true,
    })
  },
  methods: {
    // 初始化scroll
    initScroll () {
      let vm = this
      if (!this.$refs.scroll) return

      // 设置scrollContent的最小高,实现高度不足时也有回弹效果
      if (this.$refs.scrollContent) {
        this.$refs.scrollContent.style.minHeight = `${this.$refs.scroll.getBoundingClientRect().height + 1}px`
        if (vm.hasFoot.footFlag) {
          let height = vm.hasFoot.height || 88
          // this.$refs.scrollContent.style.minHeight = `${this.$refs.scroll.getBoundingClientRect().height - height}px`
          this.$refs.scrollContent.style.paddingBottom = height + 'px'
        }
      }

      const options = {
        probeType: this.probeType,
        click: this.click,
        scrollX: this.scrollX,
        scrollY: this.scrollY,
        scrollbar: this.scrollbar,
        pullDownRefresh: this.pullDown && this.pullDownConfig,
        pullUpLoad: this.pullUp && this.pullUpConfig,
        startY: this.startY,
        bounce: this.bounce,
        bounceTime: this.bounceTime,
        preventDefaultException: this.preventDefaultException,
      }

      this.scroll = new BScroll(this.$refs.scroll, options)
      this.listenScroll &&
        this.scroll.on('scroll', pos => {
          this.$emit('scroll', pos)
        })

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

      this.pullDown && this._initPullDown()

      this.pullUp && this._initPullUp()
    },
    // 初始化下拉刷新
    _initPullDown () {
      let vm = this
      this.scroll.on('pullingDown', () => {
        this.pullDownBefore = false
        this.pullDownNow = true
        setTimeout(function () {
          vm.$emit('pullingDown')
          vm.scroll.closePullDown() // 防止在 bounce 前二次触发
JingChao's avatar
JingChao committed
288
        }, 100)
Nature's avatar
Nature committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
      })

      this.scroll.on('scroll', pos => {
        if (!this.pullDown || pos.y < 0) return

        const posY = Math.floor(pos.y) // 滚动的y轴位置:Number

        if (this.pullDownBefore) {
          this.bubbleY = Math.max(0, posY + this.pullDownInitTop)
          this.pullDownStyle = `transform: translateY(${Math.min(posY, -this.pullDownInitTop)}px)`
        } else {
          this.bubbleY = 0
        }

        if (this.isRebounding) {
          this.pullDownStyle = `transform: translateY(${Math.min(posY, this.pullDownConfig.stop)}px)`
        }
      })
    },
    // 初始化上拉加载
    _initPullUp () {
      this.scroll.on('pullingUp', () => {
        let vm = this
        if (this.pullUpFinally) {
          this.scroll.finishPullUp()
        } else {
          vm.pullUpNow = true
          setTimeout(function () {
            vm.$emit('pullingUp')
JingChao's avatar
JingChao committed
318
          }, 100)
Nature's avatar
Nature committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
        }
      })
    },
    // 关闭滚动
    disable () {
      this.scroll && this.scroll.disable()
    },
    // 开启滚动
    enable () {
      this.scroll && this.scroll.enable()
    },
    // 销毁滚动示例
    destroy () {
      this.scroll && this.scroll.destroy()
    },
    // 刷新滚动高度
    refresh () {
      this.scroll && this.scroll.refresh()
    },
    // 更新加载状态,下拉/下拉成功后使用
    async update (final) {
      if (this.updateState) return

      this.updateState = true // 正在update状态

      if (this.pullDown && this.pullDownNow) {
        // 下拉刷新触发成功后
        this.pullDownNow = false
        await timeout(this.bounceTime / 2) // 刷新成功hold
        this.isRebounding = true
        this.scroll.finishPullDown() // 开始回弹
        await timeout(this.bounceTime)
        this.pullDownBefore = true
        this.isRebounding = false
        this.scroll.openPullDown(this.pullDownConfig)

        this.pullUpFinally = false
      } else if (this.pullUp && this.pullUpNow) {
        // 上拉加载触发成功后
        this.pullUpNow = false
        this.scroll.finishPullUp()
      }

      typeof final !== 'undefined' && (this.pullUpFinally = !!final)

      await this.$nextTick()
      this.refresh()

      this.updateState = false
    },
    /**
       * 每次滚动多少距离
       * @param  {Number} x    x轴位置
       * @param  {Number} y    y轴位置
       * @param  {Number} time 滚动时间
       * @return {Void}
       */
    scrollBy (x = 0, y = 0, time = this.bounceTime) {
      this.scroll && this.scroll.scrollTo((this.scroll.absStartX - x), (this.scroll.absStartY - y), time)
    },
    /**
       * 滚动到指定位置
       * @param  {Number} x    x轴位置
       * @param  {Number} y    y轴位置
       * @param  {Number} time 滚动时间
       * @return {Void}
       */
    scrollTo (x = 0, y = 0, time = this.bounceTime) {
      this.scroll && this.scroll.scrollTo(x, y, time)
    },
    // 滚动到元素
    scrollToElement () {
      this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
    },
    // 滚动到顶部
    scrollToTop () {
      this.scroll && this.scrollTo(0, 0)
    },
    // 滚动到底部
    scrollToBottom () {
      this.scroll && this.scrollTo(0, this.scroll.maxScrollY)
    },
  },
}
</script>

<style lang="stylus">
  //$ = vue-better-scroll
  .vue-better-scroll {
    width 100%
    overflow hidden
    box-sizing border-box
JingChao's avatar
JingChao committed
411 412
    position relative
    height 100%
Nature's avatar
Nature committed
413 414 415 416 417 418 419 420 421 422 423 424

    &__wrapper {
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
      -webkit-touch-callout: none;
      -webkit-text-size-adjust: none;
      -moz-text-size-adjust: none;
      text-size-adjust: none;
      -webkit-transform-origin: left top;
      transform-origin: left top;
JingChao's avatar
JingChao committed
425
      padding-bottom: 0.9rem;
Nature's avatar
Nature committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    }

    &__pullup {
      width 100%
      height 25px
      display flex
      justify-content center
      align-items center
      font-size 14px
      color rgb(153, 153, 153)
    }

    &__pulldown {
      position absolute
      left 0
JingChao's avatar
JingChao committed
441
      top -50px; /*no*/
Nature's avatar
Nature committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455
      width 100%
      display flex
      justify-content center
      align-items center
      transition all
      font-size 14px
      color rgb(153, 153, 153)

      &__before {
        display flex
      }

      &__after {
        width 100%
JingChao's avatar
JingChao committed
456
        height 40px; /*no*/
Nature's avatar
Nature committed
457 458 459 460 461 462 463
        display flex
        justify-content center
        align-items center
        color #666
      }
    }
  }
JingChao's avatar
JingChao committed
464 465 466 467

   .platform-ios{
     .vue-better-scroll {
       &__wrapper {
JingChao's avatar
JingChao committed
468
        //  padding-bottom: 2.16rem;
JingChao's avatar
JingChao committed
469 470 471
       }
     }
   }
Nature's avatar
Nature committed
472 473 474
  // iPhoneX适配
  @media (device-width: 375px) and (device-height: 812px) and (-webkit-min-device-pixel-ratio: 3) {
    .platform-ios {
JingChao's avatar
JingChao committed
475 476
      .vue-better-scroll {
        &__wrapper {
JingChao's avatar
JingChao committed
477
        //  padding-bottom: 2.84rem;
JingChao's avatar
JingChao committed
478
        }
Nature's avatar
Nature committed
479 480 481 482 483 484 485
      }
    }
  }

  // iPhoneX Max适配
  @media (device-width: 414px) and (device-height: 896px)  {
    .platform-ios {
JingChao's avatar
JingChao committed
486 487
      .vue-better-scroll {
        &__wrapper {
JingChao's avatar
JingChao committed
488
         // padding-bottom: 2.84rem;
JingChao's avatar
JingChao committed
489
        }
Nature's avatar
Nature committed
490 491 492 493
      }
    }
  }
</style>