Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMontalvo Miguelo
24,789 PointsHow to animate DOM elements on scrolling? like http://teamtreehouse.com/features
2 Answers
Ashley Rich
6,522 PointsI have used jQuery Appear previously to achieve a similar effect. You can see it in action on Delightful WP and the code examples can be found here.
Ashley
Michael Collins
433 PointsI'm not sure what you mean by "animate", but here's some code to change the class of specific elements while scrolling. Then the elements change back when you are done.
I suspect that they key thing you are looking for is the setTimout and clearTimeout functions.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Animate on Scroll</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="js/test.js"></script>
</head>
<body>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
</body>
</html>
Here's the CSS
.test {
height: 20px;
width: 100px;
background-color: #0F0;
border-style: solid;
border-color: #000;
border-width: 1px;
}
.change {
background-color: #F00;
}
And the Javascript
alter_elements = function() {
$("div.test").removeClass("change");
}
var timer = null;
$(document).ready(function() {
$(window).on('scroll resize', function(e) {
$("div.test").addClass("change");
clearTimeout(timer)
timer = setTimeout(function(){alter_elements()}, 100);
});
});