0

I have a lightbox displaying various content:

$('.mehr').click(function() {
$('#lightbox').css({'visibility' : 'visible'}).animate({'background-color': 'rgba(0, 0, 0, 0.9)', 'opacity': '1'}, 500);
});

$('.close').click(function() {
$('#lightbox').stop().animate({'background-color': 'rgba(0, 0, 0, 0)', 'opacity': '0'}, 500, function(){$(this).css({'visibility' : 'hidden'});});
});

I need to prevent the user from scrolling when #lightbox is visible. Is there a way to deactivate the browser's scrollbars when #lightbox becomes visible and then reactivate them when #lightbox is closed?

1

2 Answers 2

4

this code hide the scroll using css

body {
   overflow: hidden;
}

In JQuery

$("body").css("overflow", "hidden");

For Internet Explorer 6, just overflow the html

$("html").css("overflow", "hidden");
Sign up to request clarification or add additional context in comments.

1 Comment

In this case it will be hidden and deactivated
0

While setting the overflow attribute to hidden just hides the scrollbar you can "lock" the current scrollposition with jQuery like this:

$('.mehr').click(function() {
$('#lightbox').css({'visibility' : 'visible'}).animate({'background-color': 'rgba(0, 0, 0, 0.9)', 'opacity': '1'}, 500);
lockPosition = $(document).scrollTop();
    $(window).on('scroll', function(e) {
        $(this).scrollTop(lockPosition);
    });
});

$('.close').click(function() {
$('#lightbox').stop().animate({'background-color': 'rgba(0, 0, 0, 0)', 'opacity': '0'}, 500, function(){$(this).css({'visibility' : 'hidden'});});
    $(window).unbind('scroll');
    });
});

FIddle: http://jsfiddle.net/3kjzr/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.