Skip to content

mercs600/vue3-perfect-scrollbar

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
November 2, 2022 20:45
April 17, 2021 18:40
src
October 28, 2020 18:58
October 28, 2020 18:29
September 19, 2018 19:37
October 17, 2022 17:52
September 23, 2018 15:11
October 28, 2020 18:29
October 28, 2020 18:29
November 2, 2022 20:45
October 2, 2018 20:48
November 2, 2022 20:45
October 28, 2020 18:29
November 2, 2022 20:45

vue3-perfect-scrollbar

Vue.js minimalistic but powerful wrapper for perfect scrollbar

Why I Created it ?

Because I ❀️ to use perfect-scrollbar in my projects (πŸ™Œ utatti). But also because the current solutions on github are outdated or overcomplicated.

Why would you use it ?

Because you want to load perfect-scrollbar to your Vue project in an easy way. But also because this plugin is updated, tested and build by rollup. So you will not find any unnecessary πŸ’© code in this repo. I hope πŸ™.

If you have any reasonable PR you are welcome 🀘

Install

npm

npm install vue3-perfect-scrollbar

yarn

yarn add vue3-perfect-scrollbar

How to use

Global Registration

import { createApp } from 'vue'
import App from './App.vue'
import PerfectScrollbar from 'vue3-perfect-scrollbar'
import 'vue3-perfect-scrollbar/dist/vue3-perfect-scrollbar.css'

const app = createApp(App)
app.use(PerfectScrollbar)
app.mount('#app')

So then you can use this plugin in each component as

<perfect-scrollbar>
    <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. </p>
</perfect-scrollbar>

This plugin will generate a container with ".ps" class name, you need to customize the height of the container

/* example */
.ps {
  height: 400px;
}

Edit Vue Template

Global options

Install method takes additional parameters:

app.use(PerfectScrollbar, {
  watchOptions: true,
  options: {
    suppressScrollX: true
  }
})

name {String}

Name of your global component.

Default: PerfectScrollbar

tag {String}

Tag which will be render as perfect scrollbar container

Default: div

watchOptions {Boolean}

Set true if you want to update perfect-scrollbar on options change

Default: false

options {Object}: Options

perfect-scrollbar options.

Default: {}

Local Registration

<template>
    <div>
        <perfect-scrollbar>
            <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. </p>
        </perfect-scrollbar>
    </div>
</template>
<script>
import { PerfectScrollbar } from 'vue3-perfect-scrollbar'
export default {
    components: {
        PerfectScrollbar
    }
}
</script>
<style src="vue3-perfect-scrollbar/dist/vue3-perfect-scrollbar.css"/>

Edit Vue Template

Props

tag {String}

Tag which will be render as perfect scrollbar container

Default: div

watchOptions {Boolean}

Set true if you want to update perfect-scrollbar on options change

Default: false

options {Object}: Options

perfect-scrollbar options.

Events

You can use Vue.js way to listen on the all perfect-scrollbar events. List of events you can find here

Simple example:

<template>
  <div id="app">
    <perfect-scrollbar @ps-scroll-y="onScroll" ref="scrollbar">
      <div>your content here</div>
    </perfect-scrollbar>
  </div>
</template>

<script>
export default {
  methods: {
    onScroll(event) {
      console.log(this.$refs.scrollbar.ps, event);
    }
  }
};
</script>

Edit Vue Perfect Scrollbar Event Listening

DEMO

https://mercs600.github.io/vue3-perfect-scrollbar/. You can also fork example from codesandbox

Cookbook

Custom scrollbar behavior with router.

One of simple solution to setup custom scrollbar to top when your route is changed.

  1. Add perfect scrollbar as wrapper for router-view and add simple ref
<perfect-scrollbar ref="scroll">
  <router-view></router-view>
</perfect-scrollbar>
  1. Add watch on $route to setup scroll container to 0, when route is changed.
watch: {
  $route() {
    this.$refs.scroll.$el.scrollTop = 0;
  }
}

Edit vue3-perfect-scrollbar with router