tags:

views:

50

answers:

3

Hi. I am making a site that requires the content area of the site be faded out when the user clicks a btn. The btn is a simple anchor tag. I have had some help from other S.O users on this and nearly got it working just fine. The problem I have is that it will not fade out. Here is the jquery code:

$("#show-background").click(function () {
if ($("#content-area").hasClass("bg_hidden")){
    $("#content-area")
    .removeClass("bg_hidden")
    .stop()
    .fadeIn("slow");
    $(this).text("Show Background");
          }
else{
    $("#content-area")
    .addClass("bg_hidden")
    .stop()
    .fadeOut("slow");
     $(this).text("Show Text");
          }
});

A sample can be found here www.nicklansdell.com/sample/services.html I wonder if any one can help? Many thanks in advance.

A: 

Why not just use toggle?

$("#show-background").click(function () {
    $("#content-area").animate({opacity: 'toggle'}, 'slow'); 
});
RJD22
`fadeToggle` is not in the jquery API.
Alex Bagnolini
The reason why I was given this method was to break it down so I could understand how it works, which of course now I do now :) When I try your method it simply doesn't work. I am wondering if the Jquery tools library is somehow conflicting with this code?
mtwallet
I edited to animate that is a libary in the core this should work.
RJD22
Just tried the above and what happens now is when the btn is clicked it just fades in repeatedly
mtwallet
I just tested the code myself and it works perfectly. Make sure the #showbackground id is used on 1 element and that there is not other library interfering with it. Here a link to the working script:http://dreu.info/tests/fadetoggle.html
RJD22
Will check this out thanks again for all your help!
mtwallet
please vote for what answer was helpfull by upvoting it. That makes us feel warm inside. :P
RJD22
I will still trying all answers none are working for me! Its driving me crazy I've obviously screwed up somewhere but god only knows! Anyways I can the answer you gave does definitely work so....
mtwallet
Try to disable that jquery tools and see if it then works. If it does there is a similar library made by jquery themselfes named jquery-ui. This library makes sure it doesn't interfere with jqueries core features
RJD22
Yeah I very briefly looked at it but decided on Tools because of the scrollable feature with UI doesn't seem to have. Will look again though.
mtwallet
The problem is you are using the jQuery Tools which includes jQuery, but it's not version 1.4 which is the only version that supports the opacity 'toggle'.
fudgey
A: 

Try to fade out separately from addClass:

$("#content-area").fadeOut("slow");

BTW, you can check the button's text in the if instead of class existence...

Michael Kessler
Hi I tried doing this but again it did not work. Could there be a conflict between my code here and the jquery tools library I am using?
mtwallet
A: 

I did some tries on your page with firebug and couldn't come up with what is breaking the fadeOut call.

If you need a workaround, you can do:

if ($("#content-area").hasClass("bg_hidden")){
    $("#content-area")
    .removeClass("bg_hidden")
    .stop()
    .children().fadeIn("slow");
    $(this).text("Show Background");
}else{
    $("#content-area")
    .addClass("bg_hidden")
    .stop()
    .children().fadeOut("slow");
     $(this).text("Show Text");
}

Fading in/out all the children worked on your page.

This is a work-around, not a solution.

Alex Bagnolini
Thanks for the Alex. When I try this and preview it the div#content-area is hidden from the start and the btn does not work. Any ideas what i'm doing wrong?
mtwallet