4

For example: I want to change the background of div.world to blue when the div.hello is no longer in view after scrolling down the page.

Here is the code I have so far http://jsfiddle.net/cJWAr/140/

<div id="cat">
<div class="hello">hello</div><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <div class="world">world</div><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>

$( document ).ready(function() {
   if ($('.hello').is(":visible") ){
    $('.world').addClass('red');       
    }
    else if  ($('.hello').is(":hidden") ){
    $('.world').addClass('blue');  
     }
});
Display name
  • 177
  • 1
  • 3
  • 15

1 Answers1

0

The element is not hidden until you explicitly set its visibity : hidden. It's just scrolled down. For your task, you might want to use the jquery appear plugin. Works like a charm. Once set up, just do -

$('.hello').appear(function() { //if the element is visible, change the background to something else
  $('.world').css({'background':'your-background'});
});

You can also use the disappear function, if you'd like.

$('someselector').on('disappear', function(event, $all_disappeared_elements) {
  // this element is now outside browser viewport
});
aandis
  • 4,084
  • 4
  • 29
  • 40