0

Is there a way to call a function right before jQuery animation starts? I want to have a method that is fired before each animation in a potentially long queue.

I know .animate() function has start option, but it's being called when animation is added to queue not when it actually begins.

1 Answers1

1

Try utilizing jQuery.Deferred()

var beforeStart = function beforeStart() {
  $("body").append("<br>beforeStart", $.now());
  return $.Deferred(function(dfd) {
    setTimeout(dfd.resolve, 3000)
  }).promise()
};

$.when(beforeStart())
.then(function() {
  $("div").animate({
      top:48
    }, {
      duration:3000,
      start:function() {
        $("body").append("<br>animation start", $.now());
      }         
  });
})
div {
  position:relative;
  top:0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div>abc</div>
guest271314
  • 1
  • 15
  • 104
  • 177