{"id":13656740,"url":"https://github.com/ramonvictor/body-scroll-freezer","last_synced_at":"2025-04-01T15:34:52.581Z","repository":{"id":58219102,"uuid":"79034212","full_name":"ramonvictor/body-scroll-freezer","owner":"ramonvictor","description":"↕️ Dependency-free JS module to freeze body scroll when opening modal box","archived":false,"fork":false,"pushed_at":"2018-03-26T18:57:57.000Z","size":33,"stargazers_count":24,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-08T11:23:53.967Z","etag":null,"topics":["lightbox","microlib","modal","scrolling"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ramonvictor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-15T13:26:00.000Z","updated_at":"2024-05-21T10:47:54.000Z","dependencies_parsed_at":"2022-08-31T03:01:03.124Z","dependency_job_id":null,"html_url":"https://github.com/ramonvictor/body-scroll-freezer","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramonvictor%2Fbody-scroll-freezer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramonvictor%2Fbody-scroll-freezer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramonvictor%2Fbody-scroll-freezer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramonvictor%2Fbody-scroll-freezer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ramonvictor","download_url":"https://codeload.github.com/ramonvictor/body-scroll-freezer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246663085,"owners_count":20813823,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lightbox","microlib","modal","scrolling"],"created_at":"2024-08-02T05:00:31.648Z","updated_at":"2025-04-01T15:34:52.547Z","avatar_url":"https://github.com/ramonvictor.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# body-scroll-freezer\n\n[![npm version](https://badge.fury.io/js/body-scroll-freezer.svg)](https://badge.fury.io/js/body-scroll-freezer)\n\nDependency-free JS module to freeze body scroll when opening modal box.\n\nUseful for modal, sliding-panel and lightbox interfaces.\n\n\u003e \u003c1kb [minified](https://raw.githubusercontent.com/ramonvictor/body-scroll-freezer/master/docs/js/body-scroll-freezer.min.js).\n\n\u003ca href=\"https://ramonvictor.github.io/body-scroll-freezer/\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/ramonvictor/body-scroll-freezer/master/docs/assets/online-demo.png\" alt=\"body-scroll-freezer: online demo\" /\u003e\u003c/a\u003e\n\n## How to install?\n\n```\n$ npm i body-scroll-freezer\n```\n\n## A note on performance\n\nMany 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](http://stackoverflow.com/questions/5802467/prevent-scrolling-of-parent-element#answer-16324762) as an example.\n\nThe problem is that most of those DOM operations (`.scrollTop` and `.scrollHeight`, for example) are expensive because they [force layout/reflow](https://gist.github.com/paulirish/5d52fb081b3570c81e3a). For more info on scrolling performance [check out this article](https://www.html5rocks.com/en/tutorials/speed/scrolling/).\n\nSo, to avoid all that, **body-scroll-freezer** just assigns `overflow: hidden;` and `padding-right: [scrollWidth]px;` to the `\u003cbody\u003e`.\nThe `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.\n\n## Usage\n\n```js\n// If no AMD/CommonJS: window.bodyScrollFreezer;\nvar bodyScroll = require('body-scroll-freezer'); \n```\n\n1\\.  Init to calculate scroll bar width.\n\n```js\n// Note: declaring variable to store init() return is optional.\nvar scrollWidth = bodyScroll.init();\n```\n\n2\\. Turn scroll freeze **ON** when closing modal. Example:\n\n```js\ndocument.querySelector('.modal-open').addEventListener('click', function() {\n  // Logic to show modal goes here\n  bodyScroll.freeze();\n}, false);\n```\n\n3\\. Turn scroll freeze **OFF** when closing modal. Example:\n\n```js\ndocument.querySelector('.modal-close').addEventListener('click', function() {\n  // Logic to hide modal goes here\n  bodyScroll.unfreeze();\n}, false);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Framonvictor%2Fbody-scroll-freezer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Framonvictor%2Fbody-scroll-freezer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Framonvictor%2Fbody-scroll-freezer/lists"}