views:

91

answers:

1

In words, I want to say, If the body Id is not equal to a list item's class execute the following function, I have tried to get the code right but nothing seems to work.

I have 4 pages each with an ID to make the active state work in a css sprite that work great. On top of that I added between normal and hover a jquery opacity effect but the problem is that when I hover the active state It also changes to hover and I want the active sprite to stay put when hover, anyhelp would be appreciated, Saludos.

Html:

<ul id="nav">
    <li class="home"><a href="index.html" title="Home Page">Home</a></li>
    <li class="portfolio"><a href="portfolio.html" title="Portfolio Page">Portfolio</a></li>
    <li class="contact"><a href="contact.html" title="Contact Form Page">Contact</a></li>
    <li class="about"><a href="about.html" title="About me Page">About me</a></li>
    </ul>

Jquery:

  $(document).ready(function(){

    // Get the ID of the body
    var parentID = $("body").attr("id");

    // Loop through the nav list items
        $("#nav li").each(function() {

        // compare IDs of the body and class of list-items
           var myClass = $(this).attr("class");

        // only perform the change on hover if the IDs don't match (so the active link doesn't change on hover)
        if (myClass != "n-" + parentID) {

   // Opacity effect between states 
    $('ul#nav li a').removeClass('hover');  
    $("ul#nav li a").wrapInner("<span></span>");
    $("ul#nav li a span").css({"opacity" : 0});
    $("ul#nav li a").hover(function(){
        $(this).children("span").stop().animate({"opacity" : 1}, 500);
    }, function(){
        $(this).children("span").stop().animate({"opacity" : 0}, 500);
        });
      }

     });

});
+1  A: 

I'm not sure why you're checking for <li>s whose class is not 'n-parentId'. Don't you just want those whose classes are not the same as the body's ID?

Restrict the <li> selector to only exclude the class you don't want:

$(document).ready(function(){
    // Get the ID of the body
    var parentId = $("body").attr("id");

    // Loop through the nav list items
    $("#nav li[class!=" + parentId + "]").each(function(){
        // Opacity effect between states 
        $('ul#nav li a').removeClass('hover');  
        $("ul#nav li a").wrapInner("<span></span>");
        $("ul#nav li a span").css({"opacity" : 0});
        $("ul#nav li a").hover(function(){
            $(this).children("span").stop().animate({"opacity" : 1}, 500);
        }, function(){
            $(this).children("span").stop().animate({"opacity" : 0}, 500);
        });
    });
});
Josh Leitzel
Hi Josh, Thanks for giving me a hand with this. I'm a designer just starting with javascript so yes I see I was targeting the wrong way to the li. So you are now selecting all the li that has a class not equal to the id of the body and bind the function to each li, is that correct?I'm getting an error in that piece of code, maybe I did something wrong, I even copied the code to see if I miss typed some character but still shows an error: http://cl.ly/29Vj It says that the parent Id is not defined even though the variable is defined at start, that's strange.
Danilux
Whoops. I specified the variable as `parentID` but then called it using `parentId` (with the `d` lowercase). Fixed now.
Josh Leitzel
And yes, you are correct in your analysis of the code.
Josh Leitzel
Hi again josh, Well after struggling with the code since It wasn't working and the hover state was still being applied to the active home I found out with firebug that this line: $("ul#nav li a").wrapInner("<span></span>"); was the responsable for inserting 3 extra span to the html, so the effect was being still being applied to these extra span producing the hover effect to still appear. So then I modified it to $("#nav li a").wrapInner($("<span></span>")); and now only one correct span is added and the hover fade effect is not being applied to the active home tab, great.
Danilux
Unfortunatly this code is not working when you go to another page for example to portfolio or contact or about were the id that is in the body changes according to the page. Since the active switches to let's say portfolio the hover fade effect is added to the portfolio active state and the home tab which is on normal state doesn't react to hover, is like frozen. It's like this code $("#nav li[class!=" + parentId + "]") only caches the home id and is not being updated with the body id of portfolio or contact when you move to another page.
Danilux