2

Could you have a look through this code and let me know whats wrong? Basically it is a div that needs to fadeIn and fadeOut between 2 values and repeat this continuously. Also the div needs to be clickable during the animation.

Any help is appreciated!

What the code is supposed to do:

$("#p16-1-next").show(p16loop());
$(function(p16loop) {
    Fadeto 50%
    FadeTo 10%
    Repeat fade sequence    
});

The code i have written

$("#p16-1-next").show(p16loop());
$(function(p16loop) {
    $("#p16-1-next").fadeTo("slow", 0.5, function ()    {
        $("#p16-1-next").fadeTo("slow", 0.1, p16loop);  
    });
});
1
  • Thought i would just point out that you cant have a function inside an if statement, which may have caused my problem. Commented Dec 7, 2011 at 11:01

2 Answers 2

3

This should do it

$("#p16-1-next").show(p16loop);

function p16loop() {
    $(this).fadeTo("slow", 0.5, function ()    {
        $(this).fadeTo("slow", 0.1, p16loop);  
    });
};

Demo at http://jsfiddle.net/gaby/YeNG6/1/

Sign up to request clarification or add additional context in comments.

Comments

1

As I wrote in one of your early questions: To chain animations, there is no need to use the on-complete callback.

http://jsfiddle.net/HLjzz/1/

$("#p16-1-next").show(0, function p16loop () {
  $(this)
  .fadeTo("slow", 0.5)
  .fadeTo("slow", 0.1, p16loop);
});

7 Comments

+1 While passing a function as the first argument to show() isn't supported, it seems to be harmless. To make it a technically supported use of the API, you could do $("#p16-1-next").show( 0, function p16loop () {....
@RightSaidFred, passing a function is supported officially.. If you see the docs the third case is show( [duration] [, easing] [, callback] ). The [] mean optional, so it can very well be you opt to exclude the duration and easing but opt to include the callback.. which is what is happening in this case..
@GabyakaG.Petrioli: Yes, you're absolutely right. The docs show exactly that, and I followed the code through to verify it, and it is indeed covered here: github.com/jquery/jquery/blob/1.6.2/src/effects.js#L321-322
@GabyakaG.Petrioli: But they why do they show duration as required in with the second signature .show( duration [, callback] )? If all 3 are optional, then they seem to conflict.
@GabyakaG.Petrioli & @RightSaidFred I think, after all it's not really that important. As, with no actuall animation, the usage of the show callback is rather obsolete. One could achive the same result by just using show() and afterwards use a self-calling function expression. It's a matter of taste, I think.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.