The .animate()
method has a progress function that can be called to do this:
$(function () {
$(".cair").animate({
top: "+=385px",
}, {
duration: 6000,
"complete": function () {
$('.cair').remove();
},
progress: function (a, p, r) {
//usar como quiser aqui
}
});
});
Example: link
According to the documentation, the progress function has 3 parameters. The second one passes the percentage where the animation is. The third is the number of milliseconds remaining until the animation is finished. This function was added in version 1.8
To get the top position there are two ways:
- read at each step the position / css
top
Example: link
progress: function (a, p, r) {
console.log(this.style.top);
}
- use the percentage to do the "where do you expect to be" accounts
Example: link
progress: function (a, p, r) {
var valorAtual = p * (fim - inicio);
if (Math.round(valorAtual) == 200) alert('chegámos aos 200px!');
}