2

I want make a screen width judge, when screen width is bigger than 1024px, the body scroll bar will hidden, else when screen width is smaller than 1024px the body scroll bar will show its scroll.

See code in

http://jsfiddle.net/xmJzU/ (overflow-x)

http://jsfiddle.net/xmJzU/1/ (overflowX)

And test in

http://jsfiddle.net/xmJzU/show

http://jsfiddle.net/xmJzU/1/show

However when I dragged my browser edge, adjuce screen width smaller than 1024px, there have no scroll bar appear. Thanks.

2 Answers 2

2

It works, you just need to also declare the width variable inside your resize handler as the global variable is not in its' scope.

jQuery(document).ready(function() {
    var width = $(window).width();
    if (width < 1025) {
        $('body').css('overflow-x', 'scroll');
    } else {
        $('body').css('overflow-x', 'hidden');
    }

    $(window).bind('resize', function() {
        var width = $(window).width();
        if (width < 1025) {
            $('body').css('overflow-x', 'scroll');
        } else {
            $('body').css('overflow-x', 'hidden');
        }
    });
});

Example fiddle

An alternative method to using javascript for this is to use CSS3 Media Queries, but obviously this is dependant on the min-browser spec requirements you have.

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

Comments

0

Try using this css:

body {
width:100%;
min-width:200px;
overflow-x:hidden;
}

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.