Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock scrollbar and leave it visible

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!

like image 345
lukaleli Avatar asked Nov 29 '12 17:11

lukaleli


People also ask

How do I make my scroll bar always show?

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.

How do I stop scrolling without scrollbar hiding?

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.


1 Answers

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
like image 92
lostsource Avatar answered Oct 07 '22 18:10

lostsource