0

I am trying to check the viewport size, and then apply different styles based on whether the viewport size is between 768px - 992px, using an if and else statement. However, what I've got so far isn't working.

Here is my code...

$(document).ready(function() {
    var windowSize = $(window).width();
    function checkwindowSize() {
        if(windowSize > 768 && windowSize < 992) {
            $('#show-menu').css('visibility', 'hidden', function() {
                $('#slide-menu-container').css('visibility', 'visible');
            });
        }
        else {
            $('#show-menu').css('visibility', 'visible', function() {
                $('#slide-menu-container').css('visibility', 'hidden');
            });
        }
    });
});

Thank you!

6
  • 3
    Looks like a job for media queries. Commented Jun 13, 2014 at 13:16
  • I don't know why I didn't think of using media queries! Just out of interest though, is there anything you can see that is wrong with my code? Thank you! Commented Jun 13, 2014 at 13:20
  • If i use show, can i do this... Commented Jun 13, 2014 at 13:21
  • if(windowSize > 768 && windowSize < 992) { $('#show-menu').show(, function() { Commented Jun 13, 2014 at 13:21
  • because i want to run another event as well as this one in the if statement! Commented Jun 13, 2014 at 13:22

4 Answers 4

2

Why not use css?? Like:

@media (min-width: 768px) and (max-width: 992px) { 
    #showmenu{

    }
 }

 @media (min-width: 993px) { 
    #showmenu{

    }
 }
  @media (max-width: 767px) { 
    #showmenu{

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

Comments

1

Try this.

Define your windowSize variable inside the checkwindowSize function.

@media query is preferred then javascript.

function checkwindowSize() {
    var windowSize = $(window).width();
    if(windowSize > 768 && windowSize < 992) {
        $('#show-menu').css('visibility', 'hidden', function() {
            $('#slide-menu-container').css('visibility', 'visible');
        });
    }
    else {
        $('#show-menu').css('visibility', 'visible', function() {
            $('#slide-menu-container').css('visibility', 'hidden');
        });
    }
}   

checkwindowSize();

$(window).resize(function(){checkwindowSize()});

Fiddle Demo.

Comments

0

Has Frédéric Hamidi said, CSS media query are the best way to do it.

To answer your question, in your code sample you never call the checkwindowSize() function. Also to improve your code you can call you function every time the browser is resized using the "resize" event. http://api.jquery.com/resize/

Comments

0

Here is the if you still want to use Jquery for this purpose Jsfiddle

JS

$( document ).ready(function() {

     if($( window ).width() <= 500)
     {
         $( ".smallerSize" ).css('display','block');
     }
     else
     {
        $( ".biggerSize" ).css('display','block');
     }
});

$( window ).resize(function() {
     if ($( window ).width() <= 500)
     {
         $( ".biggerSize" ).css('display','none');
         $( ".smallerSize" ).css('display','block');
     }
     else
     {
         $( ".smallerSize" ).css('display','none');
         $( ".biggerSize" ).css('display','block');
     }

});

CSS

.smallerSize
{
    display:none;
}

.biggerSize
{
    display:none;
}

HTML

<div class="smallerSize">
    <span>Small Size</span>
</div>

<div class="biggerSize">
    <span>Bigger Size</span>
</div>

Here you have 2 conditions, 1st one is when the page is loaded and 2nd condition is when the browser size is changing.You can see the demo for this approach

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.