views:

517

answers:

3

Hi there. I apologise for any stupid questions/coding, I'm very new to jquery!

I'm trying to create a menu for a one-page site that has rollovers and an active state. HTML:

<ul id="menu">
<li><a class="rollover" href="#"><img class="folio" src="images/folio3.png" /></a></li>
<li><a class="rollover" href="#"><img class="services" src="images/services3.png" /></a></li>
<li><a class="rollover" href="#"><img class="about" src="images/about3.png" /></a></li>
<li><a class="rollover" href="#"><img class="contact" src="images/contact3.png" /></a></li>
</ul>

jquery:

$(document).ready(function(){   
$("a.rollover").fadeTo(1,0.5);
$("a.rollover").hover(
        function() {$(this).fadeTo("fast",1);},
        function() {$(this).fadeTo("fast",0.5);});
$("a.rollover").click(function(){
if($(".active").length) {
    if($(this).hasClass("active")) {
    $(this).removeClass("active");
    $(this).fadeTo("fast",0.5);
    } else {
    $(".active").fadeTo("fast",0.5);
    $(".active").removeClass("active");
    $(this).addClass("active"); 
    $(this).fadeTo("fast",1);
    }
} else {    
    $(this).addClass("active");
    $(this).fadeTo("fast",1);
    }});
});

So there are two problems here:

  1. Even though the active class is added and in Chrome's developer tools I can see that the opacity on an active class is "1", it doesn't seem to work in the browser, ie. the opacity still appears in the browser to be "0.5".

  2. If $(this) is active, even after clicking $(this) thus removing the active class, the opacity remains at "1". If I click $(this) several times, eventually the opacity changes back to "0.5".

I'd really appreciate the help. I've been struggling with this for oh... 3 days now heh :-/

Many thanks in advance...

A: 

Try this, crunched down a bit

$(function(){   
  $("a.rollover").fadeTo(1,0.5);
  $("a.rollover").hover(
    function() {$(this).stop().fadeIn("fast");},
    function() {$(this).stop().fadeTo("fast",0.5);}
  ).click(function(){
    var ia = $(this).hasClass("active"); //Was it active before?
    $(".active").fadeTo("fast",0.5).removeClass("active"); //Remove actives
    $(this).toggleClass("active", !ia); //Switch active to reverse of previous
    $(".active").stop().fadeIn("fast");//Fade in anything active (this if it is)
  }});
});
Nick Craver
Thanks Nick for your very quick answer! Unfortunately, while the hover and add/remove "active" class both work, I still have the same problems:1. Opacity of active class in the developer tools is "1", but is still *appears* as 0.5 in the browser.2. Click on an active img removes the active class, but the opacity remains at "1". Clicking several time on the same img eventually changes the opacity to "0.5" but this can also occur while the img toggles back to the "active" class.
circey
@circey - For #1, Attributes of a css **class** and in-line attributes on a particular **element** can always be different. For #2, I would remove all opacity declarations from css in your case, I think it'd greatly simplify what you're trying to do.
Nick Craver
Thanks, but the css does not contain any opacity declarations whatsoever and the css also does not contain any attributes for the ".active" class.
circey
A: 

Try this, I think it should work:

$(function() {
    $("a.rollover img").fadeTo(1, 0.5);
    $(".active").fadeTo(0.5, 1);

    $("a.rollover img").hover(
        function() {
            if ( ! $(this).hasClass("active")) {
                $(this).stop().fadeTo("fast", 1);
            }
        },
        function() {
            if ( ! $(this).hasClass("active")) {
                $(this).stop().fadeTo("fast", 0.5);
            }
        }
    ).click(function() {
        var ia = $(this).hasClass("active"); //Was it active before?
        $(".active").fadeTo("fast", 0.5).removeClass("active"); //Remove actives
        $(this).toggleClass("active", !ia); //Switch active to reverse of previous
        $(".active").stop().fadeTo("fast", 1); //Fade in anything active (this if it is)
    });
});
davgothic
No, this didn't work either. Thank you for your effort though!
circey
+2  A: 

I think this should do what you are trying to do

$(document).ready(function(){   
    $("a.rollover").fadeTo(1,0.5);
    $("a.rollover").hover(function(){
        $(this).fadeTo("fast",1);
    },function(){
        if(!$(this).hasClass('active'))
        {
            $(this).fadeTo("fast",0.5);
        }
    });
    $("a.rollover").click(function(){
        if($('.active').length > 0)
        {                
            if($(this).hasClass('active'))
            {
                $(this).removeClass("active");
                $(this).fadeTo("fast",0.5);
            }
            else
            {
                $(".active").fadeTo("fast",0.5);
                $(".active").removeClass("active");
                $(this).addClass("active");
                $(this).fadeTo("fast",1);
            }
        }
        else
        {
            $(this).addClass("active");
            $(this).fadeTo("fast",1);
        }
        return false;
    });
});
Nalum
Yes, it certainly is! Thanks so much :-)
circey