How can I lock scrolling of a webpage temporarily when a dialog box is displayed ? I have a dialog box within which I want to enable scrolling after d开发者_开发技巧eactivating scrolling from the overlayed webpage.
Is there a js command to temporarily disable scrolling ?
EDIT Try this:
On dialog open (remove scrollbar and prevent user from scrolling):
$('body').css({'overflow':'hidden'});
$(document).bind('scroll',function () {
window.scrollTo(0,0);
});
On Dialog Close(allow user to scroll again):
$(document).unbind('scroll');
$('body').css({'overflow':'visible'});
You could set a container element or maybe even the body to overflow: hidden
with a width and height of the browser window. This way any overflowing content will fall off the page and scroll bars are never displayed. This can be set in a css statement like body.dialog-open { overflow: hidden; }
. You can then add and remove the .dialog-open
classname when the dialog opens and closes.
The width and height might not be required if setting this on the body, but I'd have to check browser compatibility on that one. Might get some unexpected results there.
edit:
If you want scrolling inside your dialog you can set overflow: auto
there, with a height set on that element.
Older browsers (most notably IE) might show a horizontal scrollbar as well, you might have to set overflow-x: hidden
if that is the case.
Also see: CSS div element - how to show horizontal scroll bars only? for more information on scrollbars.
Here is vanilla JS version:
document.getElementsByTagName('body')[0].style.overflow = 'hidden';
...
document.getElementsByTagName('body')[0].style.overflow = 'visible' // the default for the css property
It's a copy of my answer that I posted here: https://stackoverflow.com/a/63221105/4336168
I use these two functions for this:
function enableBodyScroll() {
if (document.readyState === 'complete') {
document.body.style.position = '';
document.body.style.overflowY = '';
if (document.body.style.marginTop) {
const scrollTop = -parseInt(document.body.style.marginTop, 10);
document.body.style.marginTop = '';
window.scrollTo(window.pageXOffset, scrollTop);
}
} else {
window.addEventListener('load', enableBodyScroll);
}
}
function disableBodyScroll({ savePosition = false } = {}) {
if (document.readyState === 'complete') {
if (document.body.scrollHeight > window.innerHeight) {
if (savePosition) document.body.style.marginTop = `-${window.pageYOffset}px`;
document.body.style.position = 'fixed';
document.body.style.overflowY = 'scroll';
}
} else {
window.addEventListener('load', () => disableBodyScroll({ savePosition }));
}
}
How it works:
When you want to disable the scroll with saving the current position, you run
disableBodyScroll({ savePosition: true })
.The function check whether the page loaded or not (because user may trigger dialog opening during the loading).
If the page is loaded, it saves current scroll position by setting
margin-top
onbody
, then it setsposition: fixed; overflow-y: scroll
on it to remove scrollbar.If the page isn't loaded, it adds event listener to run (3.) when the page loads.
For enabling scroll everything is the same, but the function remove styles instead of setting them.
Source of the code, so they can be used like this:
npm install --save @funboxteam/diamonds
import { enableBodyScroll, disableBodyScroll } from '@funboxteam/diamonds';
精彩评论