Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Montalvo Miguelo
Montalvo Miguelo
24,789 Points

How to animate DOM elements on scrolling? like http://teamtreehouse.com/features

2 Answers

I 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
Michael Collins
433 Points

I'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);
    });


});