Vue Perfect Scrollbar (Vue 2)
vue-perfect-scrollbar is a Vue.js 2 wrapper component for the Perfect Scrollbar library, designed to provide custom, cross-browser scrollbars that mimic native scrolling behavior but offer enhanced styling capabilities. The current version, 0.2.1, was last published 7 years ago, indicating that this specific package is abandoned and no longer actively maintained. While it aimed to simplify the integration of perfect-scrollbar into Vue 2 applications, users looking for modern Vue compatibility (Vue 3) or ongoing maintenance should seek alternative, actively developed wrappers like `vue3-perfect-scrollbar`. This package does not have a regular release cadence due to its abandoned status.
Common errors
-
[Vue warn]: Unknown custom element: <VuePerfectScrollbar>
cause The VuePerfectScrollbar component was not correctly registered with Vue, or there's a typo in the component name.fixEnsure `import VuePerfectScrollbar from 'vue-perfect-scrollbar'` is present in your script, and the component is registered in `components: { VuePerfectScrollbar }` within your Vue component definition. -
Scrollbars are not visible or do not function correctly.
cause Missing `perfect-scrollbar` CSS import or the container element does not have `position: relative`.fix1. Add `import 'perfect-scrollbar/css/perfect-scrollbar.css'` to your project. 2. Ensure the CSS for the scrollable area includes `position: relative;`. -
VuePerfectScrollbar does not update after content changes (e.g., new items added to a list).
cause Perfect Scrollbar often requires manual `update()` calls when its content or container dimensions change, and this wrapper might not automatically detect all changes.fixAfter content changes that affect scrollable size, you may need to manually trigger an update on the Perfect Scrollbar instance. This typically involves accessing the underlying Perfect Scrollbar instance via a Vue ref and calling its `update()` method, though the exact access path can vary (e.g., `this.$refs.myScrollbar.$children[0].update()`).
Warnings
- breaking This `vue-perfect-scrollbar` package is abandoned and has not been updated in over 7 years. It is designed for Vue 2 and is incompatible with Vue 3. Using it in modern Vue projects will lead to significant compatibility issues and a lack of ongoing support or security patches.
- gotcha The underlying `perfect-scrollbar` library and its CSS must be installed and imported separately. This wrapper component does not bundle the core perfect-scrollbar styles, leading to non-functional scrollbars if the CSS is missing.
- gotcha For `perfect-scrollbar` to function correctly, the container element on which `vue-perfect-scrollbar` is applied (or its direct parent) must have `position: relative` CSS property. Failing to set this will result in scrollbars not appearing or behaving erratically.
- deprecated Older versions (prior to 0.0.14) had 'wrong method names for init and uninit', which could cause issues with programmatic control or component lifecycle management. While fixed, this indicates potential instability in early releases.
Install
-
npm install vue-perfect-scrollbar -
yarn add vue-perfect-scrollbar -
pnpm add vue-perfect-scrollbar
Imports
- VuePerfectScrollbar
const VuePerfectScrollbar = require('vue-perfect-scrollbar')import VuePerfectScrollbar from 'vue-perfect-scrollbar'
- VuePerfectScrollbar (Webpack/Older Bundlers)
import { VuePerfectScrollbar } from 'vue-perfect-scrollbar'import VuePerfectScrollbar from 'vue-perfect-scrollbar/index.vue'
- CSS Styles
import 'vue-perfect-scrollbar/dist/perfect-scrollbar.css'
import 'perfect-scrollbar/css/perfect-scrollbar.css'
Quickstart
<template>
<VuePerfectScrollbar class="scroll-area" :settings="settings" @ps-scroll-y="scrollHandle">
<div style="height: 1000px; width: 100%; background: #f0f0f0; padding: 20px;">
<h1>Scrollable Content</h1>
<p>This is some long content to demonstrate the scrollbar functionality.</p>
<p>Scroll down to see more...</p>
<p v-for="n in 50" :key="n">Line {{ n }} of many.</p>
</div>
</VuePerfectScrollbar>
</template>
<script>
import VuePerfectScrollbar from 'vue-perfect-scrollbar'
import 'perfect-scrollbar/css/perfect-scrollbar.css' // Don't forget to import the CSS
export default {
components: {
VuePerfectScrollbar
},
name: 'App',
data() {
return {
settings: {
maxScrollbarLength: 60,
wheelSpeed: 0.5
}
}
},
methods: {
scrollHandle(evt) {
console.log('Scroll Y event:', evt)
},
// Example of manually updating the scrollbar
updateScrollbar() {
if (this.$refs.scrollbar) {
// Assuming you add a ref='scrollbar' to VuePerfectScrollbar component
// this.$refs.scrollbar.$children[0].update(); // Accessing underlying PerfectScrollbar instance
console.warn('Manual update method might vary based on the Perfect Scrollbar version and wrapper implementation.')
}
}
},
mounted() {
// Simulate content change after a delay to show update necessity
// setTimeout(() => {
// // Add more content dynamically
// // this.$nextTick(() => this.updateScrollbar());
// }, 2000);
}
}
</script>
<style lang="scss">
.scroll-area {
position: relative; /* Crucial for perfect-scrollbar to work correctly */
margin: auto;
width: 400px;
height: 300px;
border: 1px solid #ccc;
}
</style>