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 trial

JavaScript

Adding new class for the div in view port

Hello Every one,

Now I'm working on playing with animation.i would like to know how to add a class to the div with is in view post .there are some js plugins but they didn't work for me (may be my mistake).currently i'm looking at a jQuery Waypoints plugin .here is the function i want to execute 1.first check a div with class .hand is in view port 2.if it is in view port then add additional classes .animated .slideInRight to the same div

Can any buddy let me know how to do this currently. really appropriate if any buddy can provide the required js code . Thanks in advance

2 Answers

Hi,

When you are saying view port, what are you referring to? Do you want to add a class if the device has a certain resolution, or do you want to add it based on orientation?

i mean to say adding classes to a div when it is in visible screen resolution when we scroll down ...

Fair enough - I've created a small jsfiddle for you that you can check out: http://jsfiddle.net/G5MdA/

Here's where all the "magic" happends:

var one = $('.one'),
      two = $('.two'),
      three = $('.three'),
      top = $('.top');
$(window).scroll(function(){
    var scrollPos = $('body').scrollTop();

  if (scrollPos >= one.position().top && scrollPos <= one.position().top + parseInt(one.height())) {
      top.html('We have reached the first element');
  } else if (scrollPos >= two.position().top && scrollPos <= two.position().top + parseInt(two.height())) {
      top.html('We have reached the second element');
  } else if (scrollPos >= three.position().top && scrollPos <= three.position().top + parseInt(one.height())) {
      top.html('We have reached the third element');
  }
});