jQuery Reverse conflict

Asked

Viewed 108 times

3

I’m working on a website that I want it to change the order of the read when the window is < 800px, and I implemented the code below. The problem is that it seems to be failing at random and sometimes even when the window is going back to over 800px, and the menu is no longer clickable. I don’t know if it’s because I’m conflicting with the animation you get when the window > 800px. Any hints? the site is on http://www.iwanttobesanta.com/news.php for anyone who wants to take a look.

    function mouseEnterAnimation(allowAnimation) {
    if (allowAnimation) {
        $('#upBar, nav ul').stop(true).animate({
            "height" : "60px"
        }, 200);

        $('nav ul li').stop(true).animate({
            "padding-top"    : "20px",
            "padding-bottom" : "20px",
            "height"         : "60px"
        }, 200);

        $('#lang').stop(true).animate({
            "padding-top"    : "23px",
            "padding-bottom" : "23px",
            "height"         : "60px"
        }, 200);

        $('#logo').stop(true).animate({
            "margin-top"  : "15px",
            "margin-left" : "10px"
        }, 200);
    }
}


// Animação quando o rato sai de cima
function mouseLeaveAnimation(allowAnimation) {

    if (allowAnimation) {

        $('#upBar, nav ul').stop(true).animate({
            "height": "45px"
        }, 200, function() {
            $('#upBar, nav ul').removeAttr("style");
        });

        $('nav ul li').stop(true).animate({
            "padding-top"    : "13px",
            "padding-bottom" : "13px",
            "height"         : "45px"
        }, 200, function() {
            $('nav ul li').removeAttr('style');
        });

        $('#lang').stop(true).animate({
            "padding-top"    : "16px",
            "padding-bottom" : "16px", 
            "height"         : "45px"
        }, 200);

        $('#logo').stop(true).animate({
            "margin-top"  : "7px",
            "margin-left" : "10px"
        }, 200, function () {
            $('#logo').removeAttr('style');
        });
    }
}


$(document).ready(function () {

    var $topNav = $('#upBar, nav'),
        allowAnimation = ($(window).width() >= 800);

    $topNav.hover(
        function(){
            mouseEnterAnimation(allowAnimation);
        },
        function() {
            mouseLeaveAnimation(allowAnimation);
        }
    );

    if (!allowAnimation) {
      $("nav ul").append($("li").get().reverse());
    }
    else {

    }

    $(window).resize(function() {
        allowAnimation = ($(window).width() >= 800);

        if (allowAnimation) {

              $('nav ul').show();
        }

            else {

              $('nav ul').hide();
              $("nav ul").append($("li").get().reverse());
        }


    });

    $("#btnMobile, #menu").on("click", function(){
    $("nav ul").stop(true).slideToggle();

    });

});
  • The whole problem is the part where you mess with the li and forgets that he’s surrounded by links <a href>

  • Yes I’ve been told about this, but I tried with the <a> inside the li and the links were only in the text, it was not that I intended

  • the <a> should not be inside the <li>, the <a> must be surrounding the <li> in this way: <a href="#"> <li> Notícias </li> </a>

  • @Pauloroberto, this is not a valid construction. Markup out of compliance with the specifications can be rendered by the browser as it sees fit (and I have already witnessed incompatibilities between browsers exactly due to this construction). An element <ul> shall contain only elements <li>. Elements <li> on the other hand may contain links (which can be expanded to occupy the entire space of the <li> as my comments in the reply.

  • Got it, thank you very much

  • But how do I expand <a>? I’m trying and I can’t. http://jsfiddle.net/Dq6gM/

  • Miguel, that would be another question, but I’ll break your chain: http://jsfiddle.net/Dq6gM/1/. display: block; height: 100; worked for your fiddle.

  • Thank you so much @Anthony

Show 3 more comments

1 answer

0


See that your links are outside the element <li>:

<a href="noticias.php"><li>Notícias</li></a>
<a href="#"><li>Logística</li></a>

When you call:

$("nav ul").append($("li").get().reverse());

Is recovering the elements <li> without the links. Changing the Markup the problem goes away:

<li><a href="noticias.php">Notícias</a></li>
<li><a href="#">Logística</a></li>

That said, I think your Javascript is getting too complicated and doing various unnecessary manipulations in the DOM. Personally I would develop the functionality with two separate menus and a bit of media query to hide or display the reduced menu.

.menuReduzido {
    display: block;
}
.menu {
    display: none;
}
@media (min-width: 800px) {
    .menuReduzido {
        display: none;
    }
    .menu {
        display: block;
    }
}

With this you can make the two menus (and their respective events/animations) static and the CSS will display them according to the size of the window. While this kind of building with display: none; also not perfect (the ideal would be to have only a single menu "fluid") believe that it is the lesser of two evils in this case where there are animations involved.

  • Yes I tried that but it conflicted with animation > 800px. I have the links out of the read because I that the entire read is a link and not only the text

  • It doesn’t conflict if you link the animations to the class .menu instead of the physical elements / ids. If you want to keep the link out of the li (knowing that this construction does not pass the validation W3C - namely, your Markup is invalid) you need to modify your logic to reinsert the links within the <ul> rather than the elements <li>.

  • Another point, you can make your link a block display: block; occupying the entire space of the <li>, so that the user can click anywhere in the <li>. That way you produce Markup valid with the link inside the <li>, and, in any event, achieves the desired effect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.