Prevent Body Scroll In Vue.js – v-scroll-lock

v-scroll-lock is a small yet useful Vue.js directive that prevents the body scroll when other components (e.g. modal, nav, etc) are activated.

How to use it:

1. Install & Import the v-scroll-lock module.

import VScrollLock from 'v-scroll-lock'

Vue.use(VScrollLock)

2. Use the direction in your app.

<template>
  <div class="modal" v-if="open">
    <button @click="closeModal">X</button>
    <div class="modal-content" v-scroll-lock="open">
      <p>Modal Content Here</p>
    </div>
  </div>
</template>
export default {
  name: 'Modal',
  data() {
    return {
      open: false,
    }
  },
  methods: {
    openModal() {
      this.open = true
    },
    closeModal() {
      this.open = false
    },
  },
}

Preview:

Prevent Body Scroll In Vue.js - v-scroll-lock

Changelog:

09/14/2020

  • v1.3.1

05/19/2020

  • v1.2.0

Download Details:

Author: phegman

Live Demo: https://v-scroll-lock.peterhegman.com/

Download Link: https://github.com/phegman/v-scroll-lock/archive/master.zip

Official Website: https://github.com/phegman/v-scroll-lock#demo

Install & Download:

# Yarn
$ yarn add v-scroll-lock

# NPM
$ npm install v-scroll-lock --save

Add Comment