0

Hey I was wondering if anyone could help me with some basic javascript. Is it possible to use the jquery .css to use asynchronous callbacks to stagger changes to css?

E.g.

 jQuery("#topbar").css({transition: "background-color 10s ease", background: "rgba(255, 255, 255, 0.6)"});
            //jQuery("#topbar").css({transition: "background-color 10s ease", background: "rgba(255, 255, 255, 0.0)"});

In this example can I alter the first function to become asysnchronous so that it runs the second commented function in a callback function?

Thanks

5
  • I understood what you want, but the way you put it is kind of confusing. You don't use the jquery .css, you apply styles/transitions at runtime with jQuery Commented Jun 6, 2014 at 16:39
  • That's not what asynchronous means. Asynchronous means that one thing does not have to wait for another -- that their run times are not synchronized. You want the opposite. Commented Jun 6, 2014 at 16:39
  • 3
    Use .animate() to queue css changes Commented Jun 6, 2014 at 16:41
  • Can you more clearly explain what you want to happen visually. Commented Jun 6, 2014 at 16:41
  • Just wanted to get a flash in flash out effect. My experience with asynchronous programming is that the only way to ensure sequential logic is through callbacks. Even loops must use callbacks. More like recursion I suppose. .animate() is probably the best option just have v limited front-end experience. Commented Jun 6, 2014 at 17:10

1 Answer 1

1

Option #1

Use pure CSS3 instead. Look into using CSS animations rather than queueing them up with Javascript. It is kind of pointless with modern browsers. See MDN and W3C.

Option #2

You'll have to set a timeout to delay the execution of the second jQuery line. Like so:

jQuery("#topbar").css({transition: "background-color 10s ease", background: "rgba(255, 255, 255, 0.6)"});

setTimeout(function() {
    jQuery("#topbar").css({
        background: "rgba(255, 255, 255, 0.0)"}
    );
}, 10000);

Now if you want them to loop you'll have to set up a recursive relation between the two functions. Hope this helps!

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

4 Comments

True! Oversight on my part
There are options three and four - use $.queue or $.animate so that it can be $.dequeued or $.stopped easily.
Worked a treat. I'll look into CSS animations.
You can also listen for transitionend (webkitTransitionEnd is the only prefixed one you'll need) events and chain them that way as well.

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.