0

I am trying to build a function which checks whether a set of divs are within the visible viewport. I have the following code:

$(window).scroll(function(){
    var $w = $(window);
    var bottom_edge_y = $w.scrollTop()  + $w.height();
    var top_edge_y = $w.scrollTop();

    $('#itin_list').children('div').each(function () {
        var scrollTop = $(window).scrollTop(),
        divOffset = $('#'+ $(this).attr('id')).offset().top,
        if(top_edge_y < divOffset< bottom_edge_y){
            alert("caught");
        }
    });
)};

However this code alerts every time, even if the divOffset is not within the range specified in the if condition. What is the issue? Thanks in advance.

Shikkediel
  • 5,195
  • 16
  • 45
  • 77
toing_toing
  • 2,334
  • 1
  • 37
  • 79
  • 1
    Suggest you look at this: http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport if you haven't already. Lots of good info. – Richard Nov 12 '15 at 10:30

1 Answers1

1

There is a problem with your if condition. You cannot compare 3 numbers in a single comparison like this.

Instead of `if(top_edge_y < divOffset< bottom_edge_y), 

use this:

 if(top_edge_y < divOffset &&  divOffset<bottom_edge_y)`
burp_dude
  • 51
  • 3