I have a dialog box appearing when user clicks on any of the flat.
What I want to do is to lock scrollbar if viewport height is bigger than 550px. Now I apply overflow:hidden
to body, but this causes site jumping when scrollbar is hiding. I want to disable scrolling, but still show a scrollbar. Is it possible?
Thanks in advance!
To show the scrollbars always on the webpage, use overflow: scroll Property. It will add both horizontal and vertical scrollbars to the webpage. To add only horizontal scrollbar use overflow-x: scroll property and for vertical scrollbar use overflow-y: scroll property.
2. Disabling scroll with only CSS. There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.
You can simulate a scrollbar lock by detecting the scroll, and scrolling back to the previous position.. (this might appear jerky on some browsers especially if you drag the scroll bar itself)
function lockScroll() {
var lockX = window.scrollX;
var lockY = window.scrollY;
function lockIt() {
window.scrollTo(lockX,lockY);
return false;
}
window.addEventListener("scroll",lockIt,false)
return {
stop: function(){
window.removeEventListener("scroll",lockIt,false)
}
}
}
Usage:
var locker = lockScroll(); // locks scrolling
And when you're done you can re-enable scrolling
locker.stop(); // unlocks scrolling
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With