All Projects → ramonvictor → body-scroll-freezer

ramonvictor / body-scroll-freezer

Licence: other
↕️ Dependency-free JS module to freeze body scroll when opening modal box

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to body-scroll-freezer

React Cool Portal
😎 🍒 React hook for Portals, which renders modals, dropdowns, tooltips etc. to <body> or else.
Stars: ✭ 458 (+1981.82%)
Mutual labels:  modal, lightbox
Lity
Lightweight, accessible and responsive lightbox.
Stars: ✭ 1,051 (+4677.27%)
Mutual labels:  modal, lightbox
Mediumlightbox
Nice and elegant way to add zooming functionality for images, inspired by medium.com
Stars: ✭ 671 (+2950%)
Mutual labels:  modal, lightbox
Basiclightbox
The lightest lightbox ever made.
Stars: ✭ 299 (+1259.09%)
Mutual labels:  modal, lightbox
Lightbox
A lightbox gallery plugin for Bootstrap
Stars: ✭ 1,866 (+8381.82%)
Mutual labels:  modal, lightbox
Lightcase
The smart and flexible Lightbox Plugin.
Stars: ✭ 413 (+1777.27%)
Mutual labels:  modal, lightbox
Simplelightbox
Touch-friendly image lightbox for mobile and desktop
Stars: ✭ 744 (+3281.82%)
Mutual labels:  modal, lightbox
MultiDialog
MultiDialog utilizes jQuery UI Dialog Widget for a full featured Modalbox / Lightbox application.
Stars: ✭ 23 (+4.55%)
Mutual labels:  modal, lightbox
React Layer Stack
Layering system for React. Useful for popover/modals/tooltip/dnd application
Stars: ✭ 152 (+590.91%)
Mutual labels:  modal, lightbox
React Modal Image
A tiny React component providing modal image Lightbox.
Stars: ✭ 97 (+340.91%)
Mutual labels:  modal, lightbox
Body Scroll Lock
Body scroll locking that just works with everything 😏
Stars: ✭ 3,357 (+15159.09%)
Mutual labels:  modal, lightbox
Baguettebox.js
⚡ Simple and easy to use lightbox script written in pure JavaScript
Stars: ✭ 2,252 (+10136.36%)
Mutual labels:  modal, lightbox
react-gallery-carousel
Mobile-friendly gallery carousel 🎠 with server side rendering, lazy loading, fullscreen, thumbnails, touch, mouse emulation, RTL, keyboard navigation and customisations.
Stars: ✭ 178 (+709.09%)
Mutual labels:  modal, lightbox
Ngx Gallery
Angular Gallery, Carousel and Lightbox
Stars: ✭ 417 (+1795.45%)
Mutual labels:  modal, lightbox
react-layer-stack
Layering system for React. Useful for popover/modals/tooltip/dnd application
Stars: ✭ 158 (+618.18%)
Mutual labels:  modal, lightbox
React Useportal
🌀 React hook for Portals
Stars: ✭ 698 (+3072.73%)
Mutual labels:  modal, lightbox
fancybox
jQuery lightbox script for displaying images, videos and more. Touch enabled, responsive and fully customizable.
Stars: ✭ 7,261 (+32904.55%)
Mutual labels:  modal, lightbox
Jbox
jBox is a jQuery plugin that makes it easy to create customizable tooltips, modal windows, image galleries and more.
Stars: ✭ 1,251 (+5586.36%)
Mutual labels:  modal, lightbox
Magnify
🖼 A jQuery plugin to view images just like in Windows. Browser support IE7+!
Stars: ✭ 177 (+704.55%)
Mutual labels:  modal, lightbox
Photoviewer
🌀 A JS plugin to view images just like in Windows.
Stars: ✭ 203 (+822.73%)
Mutual labels:  modal, lightbox

body-scroll-freezer

npm version

Dependency-free JS module to freeze body scroll when opening modal box.

Useful for modal, sliding-panel and lightbox interfaces.

<1kb minified.

body-scroll-freezer: online demo

How to install?

$ npm i body-scroll-freezer

A note on performance

Many other alternatives to this module listen to both mousewheel and DOMMouseScroll events in order to get some information from the DOM, which usually includes: Element.scrollTop, event.deltaY, Element.scrollHeight or Element.clientHeight. Check this StackOverflow answer as an example.

The problem is that most of those DOM operations (.scrollTop and .scrollHeight, for example) are expensive because they force layout/reflow. For more info on scrolling performance check out this article.

So, to avoid all that, body-scroll-freezer just assigns overflow: hidden; and padding-right: [scrollWidth]px; to the <body>. The overflow avoids vertical move on the background when users are scrolling within the modal box. The padding-right prevents horizontal jumps when hiding/showing the scrollbar.

Usage

// If no AMD/CommonJS: window.bodyScrollFreezer;
var bodyScroll = require('body-scroll-freezer'); 

1. Init to calculate scroll bar width.

// Note: declaring variable to store init() return is optional.
var scrollWidth = bodyScroll.init();

2. Turn scroll freeze ON when closing modal. Example:

document.querySelector('.modal-open').addEventListener('click', function() {
  // Logic to show modal goes here
  bodyScroll.freeze();
}, false);

3. Turn scroll freeze OFF when closing modal. Example:

document.querySelector('.modal-close').addEventListener('click', function() {
  // Logic to hide modal goes here
  bodyScroll.unfreeze();
}, false);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].