1

I'm using overflow:hidden to prevent the body from scrolling when an overlay is shown. The problem is this removes the scrollbar so the whole page shifts a bit to the right. It shifts again when the overlay is removes and the overflow is set back to auto.

Is there a way to get around this? I would like to freeze the document from scrolling but would like to prevent the page from jumping around.

jsfiddle

2 Answers 2

1

You can add this:

html {
     overflow-y:scroll;
}

The body element will hide overflow (as you already have it), and the html element will just display an empty scroll bar. This works since you happen to have html and body set at height:100%.

Demo: http://jsfiddle.net/BCX64/3/

Alternatively, you can prevent scrolling like this when you show the overlay:

// Find current scroll positions
var top = $(window).scrollTop();
var left = $(window).scrollLeft()

$(window).scroll(function(){
    // Force scroll back to original positions
    $(this).scrollTop(top).scrollLeft(left); 
});

Then unbind the event when you hide the overlay:

$(window).unbind('scroll');

You don't need the noscroll class in this case.

Demo: http://jsfiddle.net/BCX64/6/

Sign up to request clarification or add additional context in comments.

2 Comments

This kinda works... jsfiddle.net/BCX64/5 If you scroll down a bit then click the button, the page body jumps back to the top.
Yes, because you have overflow:hidden on body. I get what you mean though, 1 minute...
0

What if you create a fake scrolling bar and place it in the right side.. you can customize the design too.

1 Comment

not all browsers/oses have the same width scroll bar - or any scroll bar at all for that matter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.