0

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?

Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
Metzed
  • 470
  • 1
  • 8
  • 27

1 Answers1

1

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 );
    }
});
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • Any ideas why the background color it fades to seems to be white, when it should be transparent? – Metzed Aug 10 '16 at 22:49
  • @Metzed Not sure... I guess jQuery animate doesn't properly support `transparent`? You can try with `rgba` or you can put the text into a different element and fade with `opacity`. – mpen Aug 10 '16 at 23:13
  • thanks. The original answer worked well thank you and thanks so much for posting that CSS3 solution. I've now used that instead. – Metzed Aug 11 '16 at 14:16