1

My website has had nav dropdowns that I made with CSS but I've recently changed it to jQuery so it's animated. When it was CSS I was able to disable the dropdowns on the smallest breakpoints for mobile. But with jQuery I don't know how to do that. This is my dropdown code. What can I do to it to make it disable when the viewport gets small enough?

$(document).ready(function() {
    $('.nav > li').mouseenter(function() {
        $(this).children('.nav-content').slideDown(200);
    });

    $('.nav > li').mouseleave(function() {
        $(this).children('.nav-content').slideUp(200);
    });
});

This is the website as it is now:

http://mattboy115.github.io/scarymonkeyshow/index.html

Omar
  • 32,160
  • 9
  • 67
  • 108
Matt Lee
  • 321
  • 2
  • 3
  • 16

3 Answers3

3

You can check the size of the screen with $(window).width() and $(window).height()

So something like

$(document).ready(function() {
    if($(window).width() > 800){
        $('.nav > li').mouseenter(function() {
            $(this).children('.nav-content').slideDown(200);
        });

        $('.nav > li').mouseleave(function() {
            $(this).children('.nav-content').slideUp(200);
        });
    }
}); 
Eric Guan
  • 13,320
  • 7
  • 34
  • 53
  • You were the only one who knew what I was specifically looking for and gave me a clear answer. Thank you. This works. It doesn't let me switch between working and non working unless I reset the page first. But it works and that's what matters. People aren't going to stretch their phone screens out anyway lol – Matt Lee Feb 29 '16 at 04:44
  • 1
    Thanks. You could potentially put my answer into a `resize` event listener, then you could switch between working and non working without a reset. It 's more complicated though, and you're right ppl won't stretch their phones. But for testing wise it's comforting to see the code working as expected. – Eric Guan Feb 29 '16 at 04:55
1

The answers given will work, but I'd recommend making a second nav for mobile and using media queries to make the right one show.

Just real simple HTML:

<div class="desktop-nav">
    [nav code]
</div>
<div class="mobile-nav">
    [mobile nav code]
</div>

then CSS:

@media screen and (min-width: [X]px) {
    .mobile-nav {
        display: none;
    }
}
@media screen and (max-width: [X]px) {
    .desktop-nav {
        display: none;
    }
}

then your jquery is solved by just applying the mouseenter to .desktop-nav:

$(document).ready(function() {
    $('.desktop-nav > li').mouseenter(function() {
        $(this).children('.nav-content').slideDown(200);
    });

    $('.desktop-nav > li').mouseleave(function() {
        $(this).children('.nav-content').slideUp(200);
    });
});
bryan60
  • 22,247
  • 3
  • 31
  • 47
1

I am uncertain if you are looking to do this at a specific size or if you want it for mobile. For example you can use modernizer to detect if touch is enabled on the device:

Using Modernizr to test for tablet and mobile - Opinions wanted

you can also check user agents to match device types and apply your code that way:

What is the best way to detect a mobile device in jQuery?

You can then apply if it does or doesn't match your conditions based on your detection..

Community
  • 1
  • 1
D3TXER
  • 158
  • 2
  • 13