admin管理员组

文章数量:1277895

First of all, I know this has been answered a lot of times, but all the things I try to do, they're not working. So here's my problem:

I have a non-displayed div that I want to show when the users clicks a div, so I have this javascript code:

$(document).ready(function() {
    $(".hamburger-menu").on("click", function(){
        var display = $(".menu-mobile").css("display");
        if (display == "none") {
            $(".menu-mobile").css("display", "block");
        } else {
            $(".menu-mobile").css("display", "none");
        }
    });
});

This works perfectly on my desktop but doesn't work in my mobile.
What I found in the other answers was to change the .on("click") value to .on("click touchstart") or .on("tap"), but they both don't work.

So what's the problem? I have another animation that doesn't require any click event and it works perfectly and smoothly on my mobile. Could anyone help me?

Thanks.

First of all, I know this has been answered a lot of times, but all the things I try to do, they're not working. So here's my problem:

I have a non-displayed div that I want to show when the users clicks a div, so I have this javascript code:

$(document).ready(function() {
    $(".hamburger-menu").on("click", function(){
        var display = $(".menu-mobile").css("display");
        if (display == "none") {
            $(".menu-mobile").css("display", "block");
        } else {
            $(".menu-mobile").css("display", "none");
        }
    });
});

This works perfectly on my desktop but doesn't work in my mobile.
What I found in the other answers was to change the .on("click") value to .on("click touchstart") or .on("tap"), but they both don't work.

So what's the problem? I have another animation that doesn't require any click event and it works perfectly and smoothly on my mobile. Could anyone help me?

Thanks.

Share Improve this question edited Nov 16, 2014 at 19:59 Omar 31.7k9 gold badges72 silver badges116 bronze badges asked Nov 16, 2014 at 19:30 peregraumperegraum 5371 gold badge8 silver badges19 bronze badges 4
  • 2 One suggestion, instead of $(document).ready(function() { $(".hamburger-menu").on("click", function(){ var display = $(".menu-mobile").css("display"); if (display == "none") { $(".menu-mobile").css("display", "block"); } else { $(".menu-mobile").css("display", "none"); } }); }); could you not do $(".hamburger-menu").on("click", function(){ $(".menu-mobile").toggle(); – Billy Commented Nov 16, 2014 at 19:37
  • @Billy Oh, yes! Sorry, I'm not really into javascript so I didn't really know this function existed.... Hahahahaha thanks! ;) – peregraum Commented Nov 16, 2014 at 19:45
  • Sorry I couldn't help with the actual problem, Don't know too much about web designing for mobile, Good luck though.. – Billy Commented Nov 16, 2014 at 19:54
  • @Billy Don't worry! Thanks for the advice though! – peregraum Commented Nov 16, 2014 at 20:00
Add a ment  | 

1 Answer 1

Reset to default 6

there are some issues with jquery and detecting touches. i find it best to set event listeners with javascript for this sort of thing. I've had luck with something like this:

this.addEventListener('touchend', function(e){happens(e)}, false);

touchend is a good mouseup equivalent for mobile. touchstart should work for mousedown like you were trying.

本文标签: javascriptOn click event for mobileStack Overflow