# scrollbar

# Introduciton

The scrollbar plugin provides a nice scrollbar for BetterScroll.

TIP

For v2.2.0, users can provide custom scroll bars.

# Install

npm install @better-scroll/scroll-bar --save

// or

yarn add @better-scroll/scroll-bar

# Usage

First, install the plugin via the static method BScroll.use()

import BScroll from '@better-scroll/core'
import ScrollBar from '@better-scroll/scroll-bar'

BScroll.use(ScrollBar)

pass correct scrollbar options

new BScroll('.bs-wrapper', {
  scrollY: true,
  scrollbar: true
})

# Demo

  • Vertical Scrollbar

    <template>
      <div class="scrollbar">
        <div ref="wrapper" class="scrollbar-wrapper">
          <ul class="scrollbar-content">
            <li v-for="i of 40" :key="i" class="scrollbar-content-item">
              {{ `I am item ${i} `}}
            </li>
          </ul>
        </div>
      </div>
    </template>
  • Horizontal Scrollbar

    <template>
      <div class="horizontal-scrollbar-container">
        <div class="scroll-wrapper" ref="scroll">
          <div class="scroll-content">
            <div class="scroll-item" v-for="index in num" :key="index">{{index}}</div>
          </div>
        </div>
      </div>
    </template>
  • Custom Scrollbar

    <template>
      <div class="custom-scrollbar-container">
        <div ref="wrapper" class="custom-scrollbar-wrapper">
          <img @load="onload" class="custom-scrollbar-content" :src="girlImageLink" alt="">
          <!-- custom-vertical-scrollbar-->
          <div class="custom-vertical-scrollbar" ref="vertical">
            <div class="custom-vertical-indicator"></div>
          </div>
          <!-- custom-horizontal-scrollbar-->
          <div class="custom-horizontal-scrollbar" ref="horizontal">
            <div class="custom-horizontal-indicator"></div>
          </div>
        </div>
      </div>
    </template>
  • With Mousewheel

    <template>
      <div class="mousewheel-scrollbar-container">
        <div ref="wrapper" class="custom-scrollbar-wrapper">
          <div class="custom-scrollbar-content">
            <img @load="onload" :src="girlImageLink" alt="">
          </div>
          <!-- custom-horizontal-scrollbar-->
          <div class="custom-horizontal-scrollbar" ref="horizontal">
            <div class="custom-horizontal-indicator"></div>
          </div>
        </div>
        <div class="tip">please use your mouse-wheel</div>
      </div>
    </template>

# scrollbar options

# fade

  • Type: boolean
  • Default: true

When the scroll stops, the scrollbar fades out.

# interactive

  • Type: boolean
  • Default: false

Whether scrollbar can interacted with.

# customElements 2.2.0

  • Type: HTMLElement[]
  • Default: []

The user provides a custom scroll bar.

// horizontal
const horizontalEl = document.getElementById('User-defined scrollbar')
new BScroll('.bs-wrapper', {
  scrollY: true,
  scrollbar: {
    customElements: [horizontalEl]
  }
})
// vertical
const verticalEl = document.getElementById('User-defined scrollbar')
new BScroll('.bs-wrapper', {
  scrollY: false,
  scrollX: true,
  scrollbar: {
    customElements: [verticalEl]
  }
})
// freeScroll
const horizontalEl = document.getElementById('User-defined scrollbar')
const verticalEl = document.getElementById('User-defined scrollbar')
new BScroll('.bs-wrapper', {
  freeScroll: true,
  scrollbar: {
    // When there are two scrollbars
    // the first element of the array is the horizontal
    customElements: [horizontalEl, verticalEl]
  }
})

# minSize 2.2.0

  • Type: number
  • Default: 8

The minimum size of the scrollbar. When the user provides a custom scrollbar, this configuration is invalid.

# scrollbarTrackClickable 2.2.0

  • Type: boolean
  • Default: false

Whether the scrollbar track allows clicking.

Note:When enabling this configuration, please ensure that the click of BetterScroll Options is true, otherwise the click event cannot be triggered. The reason is here.

new BScroll('.bs-wrapper', {
  scrollY: true,
  click: true // essential
  scrollbar: {
    scrollbarTrackClickable: true
  }
})

# scrollbarTrackOffsetType 2.2.0

  • Type: string
  • Default: 'step'

After the scroll bar track is clicked, the calculation method of the scroll distance is the same as the browser's performance by default. It can be configured as 'clickedPoint', which means the scroll bar is scrolled to the clicked position.

# scrollbarTrackOffsetTime 2.2.0

  • Type: number
  • Default: 300

the scroll time after the scrollbar track is clicked.

# fadeInTime 2.4.0

  • Type: number
  • Default: 250

The duration of the animation when the scrollbar fades in.

# fadeOutTime 2.4.0

  • Type: number
  • Default: 500

The duration of the animation when the scrollbar fades out.

TIP

When scrollbar is configured as true, the plugin uses the default plugin option.

const bs = new BScroll('.wrapper', {
  scrollbar: true
})

// equals

const bs = new BScroll('.wrapper', {
  scrollbar: {
    fade: true,
    interactive: false,
    // supported in v2.2.0
    customElements: [],
    minSize: 8,
    scrollbarTrackClickable: false,
    scrollbarTrackOffsetType: 'step',
    scrollbarTrackOffsetTime: 300,
    // supported in v2.4.0
    fadeInTime: 250,
    fadeOutTime: 500
  }
})