简体   繁体   中英

How do I run animate() once the page has loaded?

I have a menu item that is animated in the following fiddle by clicking a link, but I actually want the background color fade animation to start once the page has loaded (without the need to click a link). How do I change the code in the following fiddle to animate once the page has loaded?

The code I have currently is:

$("a").click(function(e) { 
  e.preventDefault();
  for (var i = 0; i < 2; i++ ) {
    $("#menu-item-9032 a")
      .animate( { backgroundColor: "#00afee", color: "#363a47" }, 2000 )
      .animate( { backgroundColor: "transparent", color: "#363a47" }, 2000 );
  }
});

http://jsfiddle.net/Fe8Jy/500/

If I replace $("a").click(function(e) { with $(document).ready(function() { nothing happens, what am I doing wrong?

You must have created a syntax error. Code works fine with jQuery's ready:

http://jsfiddle.net/tks2sobo/

$(function() {

    for (var i = 0; i < 2; i++ ) {
        $("#menu-item-9032 a")
            .animate( { backgroundColor: "#00afee", color: "#363a47" }, 2000 )
            .animate( { backgroundColor: "transparent", color: "#363a47" }, 2000 );
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM