0

I'm using this code to trigger stuff on window resize.

css

@media only screen and (min-width : 320px) and (max-width : 768px) {
    .placeholder {
        width:100%;
    }

js

if ($(window).width() <= 768){ 
$('#middle').hide();
$('.left').removeClass('hidden');

} else {
$('#middle').show();
$('.left').addClass('hidden');
}
});

However, these is an inconsistency between the CSS media query at 768 and the jquery window width 768 because they do not appear to trigger at the same time. This is something explained on this great article, http://www.fourfront.us/blog/jquery-window-width-and-media-queries

However, when I try to apply this like

if ($(".placeholder").css("width") === "100%") {
         $('.left').removeClass('hidden');
         $('#middle').hide();
    } else {
        $('#middle').show();
        $('.left').addClass('hidden');
    }

This doesn't work. I've tried applying other css values, like a 2px margin, but this also doesn't work. What am I doing wrong? Can anyone help? Many thanks.

3
  • I understand the question, but why not use CSS to do your show/hide? Commented Dec 5, 2013 at 14:58
  • What browser are you testing in? Commented Dec 5, 2013 at 15:01
  • isherwood- You are absolutely right. I didn't want to bore you with too much code so I didn't put the whole thing. Please see the updated version, I have add and remove classes too. Commented Dec 5, 2013 at 15:10

2 Answers 2

2

As mehdi said, $(".placeholder").css("width") will return you a px value. To resolve this you can add another style to your class that won't influence in your design like border-color: rgb(255, 0, 0); and use it to your if..

See this example

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

1 Comment

Thanks André! That worked beautifully. I was trying to do that before but for some reason it didn't work. It works now!
1

even if you set the width:100%, $(".placeholder").css("width") give you width in px

DEMO

if ($(".placeholder").css("width") == $(window).width()+"px")//or $(container).width()

means .placeholder width is 100% if margin=0

2 Comments

no, sorry. That was a bad choice of div name. Forget about #this, let's call it #blue.
I don't think so because that would be targeting window.

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.