1

I'm trying to do some animation without Flash, I need a logo to load then shake before coming to a complete stop

I need it to happen on load (the shaking is a client request).

Right now, this works when you click it, but I need it to run automatically.

Here is my JavaScript code:

$(window).load(function() {
  jQuery.fn.shake = function() {
    this.each(function(i) {
      $(this).css({"position": "absolute"});
      for(var x = 1; x <= 3; x++) {
        $(this).animate({left: 43}, 10)
          .animate({left: 23}, 50)
          .animate({left: 23}, 10)
          .animate({left: 13}, 50)
          .animate({left: 43}, 50)
          .animate({left: 33}, 50)
          .animate({left: 43}, 50);
      }
    });
    return this;
  }

  $("#logo").click(function() {
    $(this).shake();
  });
});

The #logo element is the div that contains the image.

Any help would be greatly appreciated, thanks.

Malekai
  • 4,765
  • 5
  • 25
  • 60
  • 1
    Did you search at all?! http://stackoverflow.com/questions/773639/how-can-i-simulate-an-anchor-click-via-jquery http://stackoverflow.com/questions/1839363/simulating-a-click-in-jquery-javascript-on-a-link http://stackoverflow.com/questions/1786377/simulate-native-click http://stackoverflow.com/questions/4338161/jquery-simulate-a-a-href-link-click – ahren Aug 23 '12 at 00:01
  • Yes I did, but couldn't get it to work. Not a programmer, but got my question answered, thanks. – adrian collins Aug 23 '12 at 00:05
  • If your question was answered it's common courtesy on stack overflow to accept the answer by clicking the checkmark below the up/down vote arrows. – invertedSpear Aug 23 '12 at 00:10

5 Answers5

3
<script type='text/javascript'>
    jQuery.fn.shake = function() {
        this.each(function(i) {
            $(this).css({
                "position": "absolute"
            });
            for (var x = 1; x <= 3; x++) {
                $(this).animate({
                    left: 43
                }, 10).animate({
                    left: 23
                }, 50).animate({
                    left: 23
                }, 10).animate({
                    left: 13
                }, 50).animate({
                    left: 43
                }, 50).animate({
                    left: 33
                }, 50).animate({
                    left: 43
                }, 50);
            }
        });
        return this;
    }
    $(document).ready(function() {
        $("#logo").shake();
    });
</script>​​​
adeneo
  • 312,895
  • 29
  • 395
  • 388
1

If you need to simulate a click, you can add this to your code:

$("#logo").click();

Anyway, I recommend you to use $(document).ready instead of window, as it will allow you to let the scripts just execute after the document has been loaded.

<script type='text/javascript'>//<![CDATA[ $(document).ready(function(){ jQuery.fn.shake = function() { this.each(function(i) { $(this).css({ "position" : "absolute" }); for (var x = 1; x <= 3; x++) { $(this).animate({ left: 43 }, 10).animate({ left: 23 }, 50).animate({ left:23},10).animate({ left: 13 }, 50).animate({ left: 43 }, 50).animate({ left: 33 },50).animate({ left: 43 }, 50); } }); return this; } $("#logo").click(function() { $(this).shake(); }); });//]]> </script>
Breezer
  • 10,410
  • 6
  • 29
  • 50
Ivo Pereira
  • 3,410
  • 1
  • 19
  • 24
0

Doesn't $("#logo").shake() work at the end of your load function?

Instead of using:

$("#logo").click(function() {
  $(this).shake();
});

Use:

$("#logo").shake();

You don't have to add an onclick event listener and simulate a click, you can just fire that function directly instead.

Malekai
  • 4,765
  • 5
  • 25
  • 60
invertedSpear
  • 10,864
  • 5
  • 39
  • 77
0

You should actually call $('#logo').shake(); but you can also do $('#logo').trigger('click');

Check out the documentation for the trigger method.

Malekai
  • 4,765
  • 5
  • 25
  • 60
Pit Digger
  • 9,618
  • 23
  • 78
  • 122
0

If you want to do it without clicking on the div , why are you having that click handler. You can do it when the page loads like this:

<script type='text/javascript'>//<![CDATA[ 
    $(window).load(function(){

        jQuery.fn.shake = function() {
            this.each(function(i) {
            $(this).css({ "position" : "absolute" });
            for (var x = 1; x <= 3; x++) {
            $(this).animate({ left: 43 }, 10).animate({ left: 23 }, 50).animate({ left:23},10).animate({ left: 13 }, 50).animate({ left: 43 }, 50).animate({ left: 33 },50).animate({ left: 43 }, 50);
                }
            });
            return this;
            }

        $(this).shake();
});//]]>  

</script>
Bishnu Paudel
  • 2,083
  • 1
  • 21
  • 39