var stage = 0 $(document).ready(function () { var height1 = $("#eins").height (); $(window).scroll(function () { var pos = $(this).scrollTop (); console.log (pos); if (pos+600 >= height1 && stage == 0){ moveBlobs (1000, 1000, 1)} else if (stage == 1 && pos < 400){ moveBlobs (0,0,0) } }) } ) function moveBlobs (t, r, to) { var dur = 0.7 if (stage < to) { $("#blobs").css ({ 'animation': 'move-down-'+ to + ' ease-in-out '+dur+ 's' }) } else { $("#blobs").css ({ 'animation': 'move-down-'+(to+1)+' ease-in-out '+dur+'s reverse' }) } setTimeout (function () {$("#blobs").css ({ 'top': t + 'px', 'right': r + 'px', 'animation': '0',}); stage = to; }, (dur*1000)-1) }