zoukankan      html  css  js  c++  java
  • better-scroll的基本使用

    封装组件:

    <template>
      <div ref="rootRef">
        <slot></slot>
      </div>
    </template>
    
    <script>
      import useScroll from './use-scroll'
      import { ref } from 'vue'
    
      export default {
        name: 'scroll',
        props: {
          click: {
            type: Boolean,
            default: true
          },
          probeType: {
            type: Number,
            default: 0
          }
        },
        emits: ['scroll'],
        setup(props, { emit }) {
          const rootRef = ref(null)
          const scroll = useScroll(rootRef, props, emit)
    
          return {
            rootRef,
            scroll
          }
        }
      }
    </script>

    封装组件用的js:

    import BScroll from '@better-scroll/core'
    import ObserveDOM from '@better-scroll/observe-dom'
    import { onMounted, onUnmounted, onActivated, onDeactivated, ref } from 'vue'
    
    BScroll.use(ObserveDOM)
    
    export default function useScroll(wrapperRef, options, emit) {
      const scroll = ref(null)
    
      onMounted(() => {
        const scrollVal = scroll.value = new BScroll(wrapperRef.value, {
          observeDOM: true,
          ...options
        })
    
        if (options.probeType > 0) {
          scrollVal.on('scroll', (pos) => {
            emit('scroll', pos)
          })
        }
      })
    
      onUnmounted(() => {
        scroll.value.destroy()
      })
    
      onActivated(() => {
        scroll.value.enable()
        scroll.value.refresh()
      })
    
      onDeactivated(() => {
        scroll.value.disable()
      })
    
      return scroll
    }

    组件使用:

        <scroll class="recommend-content">
          <div>
            <div class="slider-wrapper">
              <div class="slider-content">
                <slider v-if="sliders.length" :sliders="sliders"></slider>
              </div>
            </div>
            <div class="recommend-list">
              <h1 class="list-title" v-show="!loading">热门歌单推荐</h1>
              <ul>
                <li
                  v-for="item in albums"
                  class="item"
                  :key="item.id"
                  @click="selectItem(item)"
                >
                  <div class="icon">
                    <img width="60" height="60" v-lazy="item.pic">
                  </div>
                  <div class="text">
                    <h2 class="name">
                      {{ item.username }}
                    </h2>
                    <p class="title">
                      {{item.title}}
                    </p>
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </scroll>
    越努力越幸运
  • 相关阅读:
    iOS 自动续期订阅 恢复购买
    iOS内购恢复购买
    iOS内购自动续订
    iOS苹果内购服务端技术方案
    Spring4.x体系架构
    Linux下MySQL主从复制(Binlog)的部署过程
    Linux下MySQL多实例部署记录
    Linux下MySQL基础及操作语法
    Linux下Nginx基础应用
    Linux下Apache(HTTP)基础知识梳理-运维笔记
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/14860744.html
Copyright © 2011-2022 走看看