Skip to content

Instantly share code, notes, and snippets.

@colepacak
Last active August 29, 2015 14:22
Show Gist options
  • Save colepacak/e3d9de8a9f18a9441419 to your computer and use it in GitHub Desktop.
Save colepacak/e3d9de8a9f18a9441419 to your computer and use it in GitHub Desktop.
Using recursion to execute jQuery animation promises.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="scripts.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="promise-test">
<button>BOMBS AWAY</button>
<ul>
<li>3</li>
<li>2</li>
<li>1</li>
</ul>
</div>
</body>
</html>
$(function() {
var b = $('.promise-test button');
b.on('click', progressiveHide);
var items = $($('.promise-test li').get().reverse());
var counter = 0;
function progressiveHide() {
var p = items.eq(counter)
.animate({
bottom: 38
}, {
duration: 500,
complete: function() {
$(this).hide();
}
})
.promise();
p.then(function() {
counter++;
if (counter < items.length) {
return progressiveHide();
}
});
}
});
.promise-test {
position: relative;
margin-top: 100px;
}
ul {
padding: 0;
margin: 0;
overflow: hidden;
}
li {
display: block;
position: relative;
padding: 10px;
list-style: none;
}
li:nth-child(even) {
background: pink;
}
li:nth-child(odd) {
background: lightgray;
}
li:nth-child(1) {
z-index: 3;
}
li:nth-child(2) {
z-index: 2;
}
li:nth-child(3) {
z-index: 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment