0

I'm trying to use the following code to improve a couple of layout elements on an adaptive design. It doesn't seem to work though, it calls the first property, but that's it. Here it is in testing state using color.

$(document).ready(function() {
    if ((screen.width>940)) {
        // if screen size is 940px wide or larger
        $("body").css('color', '#222'); // you can also use $(".yourClass").hide();
    } else {
        // if screen size width is less than 940px
        $("body").css('color', 'red'); // here you can also use show();
    }
});

Any help is sincerely appreciated!

1
  • You might want to wrap your if statement with $(window).resize(function() { /* code here */ }).resize();. Commented Sep 13, 2011 at 23:45

3 Answers 3

2

You can use $(window).width(); on your code, but I strongly encourage you use CSS3 Media Queries for this purpose.

body{
    color: #222;
}
@media screen and (max-width: 940px) {
    body{
        color: red;
    }
}

Here's a good arcticle about it:

http://www.alistapart.com/articles/responsive-web-design/

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

2 Comments

Yea I refer to that article all the time.
Thanks for the recommendation but the adaptivity I need is only minor and media queries aren't supported by ie7 which is what most of our users are on.
1

do you by any chance mean $(window).width()? screen.width for me is always 1920 on my computer. On a smart phone it should be smaller, but on any given device it should be pretty constant. In any case using $(window).width() proves your syntax is correct.

http://jsfiddle.net/VvFkK/

3 Comments

Thanks, I believe you're right, that's what I'm looking for! Though I tried your code and it broke the other Javascript on my page.
idk why it doesn't work, but this one better illustrates this type of approach.
Thank you so much Joseph, you have solved my problem and made my day! :D
1

Here is What I would do...or something close to it... If I were using JS

$(function(){
                        // on window resize fire this 
                        $(window).resize(function(){
                            //get window width
                            var wide = $(window).width();

                            //log width
                            console.log(wide);

                            if (wide > 940) 
                                {
                                    console.log('wider');
                                }                             
                            else (wide < 940)
                                {
                                    console.log('not so wide');
                                }
                        });


            });//END Doc Ready

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.