JavaScript Check if scrollbar reached a div with jquery

I'm trying to check when a div is reached from scrollbar with jquery. I read some similar question on stackoverflow, but all are on one div only. I have 4 div, with height: 100% and I want know when the scroll bar pass every div. I tried, but only works with the first div.

HTML:

<body>
<div id="main"></div>
<div id="service"></div>
<div id="clients"></div>
<div id="about"></div>
</body>

CSS:

body, html {
    height: 100%;
    padding: 0px;
    margin: 0px;
}
#main {
    background: red;
    height: 100%;
}
#service {
    background: green;
    height: 100%;
}
#clients {
    background: blue;
    height: 100%;
}
#about {
    background: yellow;
    height: 100%;
}

JS:

$(document).ready(function() {
    var passed_service = false;
    var passed_service = false;
    $('body,html').bind('scroll mousedown wheel DOMMouseScroll mousewheel keyup', function(event){
        if($(window).scrollTop() >= ($("#service").height())){
            if(!passed_service){
                alert("To #service");
                passed_service = true;
            }
        }
        if($(window).scrollTop() >= ($("#service").height() + $("#clients").height())){
            if(!passed_clients){
                alert("To #clients");
                passed_clients = true;
            }
        } 
    });
});

SORRY TO ALL, WAS A MY STUPID ERROR, I CAN'T DELETE THE QUESTION :(

Answer:1
var passed_service = false;
var passed_service = false; /* should be 'passed_clients' */

^ There's your problem


Also, instead of adding up the divs' heights, use the top offset instead.

DEMO

$(window).scrollTop() >= $("#service").offset().top
$(window).scrollTop() >= $("#clients").offset().top
...
Answer:2

You never declare the passed_clients variable.

Change this:

var passed_service = false;
var passed_service = false;

to this:

var passed_service = false;
var passed_clients = false;

Working fiddle: http://jsfiddle.net/XUEfD/8/

Answer:3

I'm trying to create a stripe customer using parse but can't seem to get the customer.id value from the response. var newCustomer; Stripe.Customers.create( card: request.params.cardToken, ...

I'm trying to create a stripe customer using parse but can't seem to get the customer.id value from the response. var newCustomer; Stripe.Customers.create( card: request.params.cardToken, ...

  1. creating stripe customer
  2. stripe creating customer object

I have a controller for uploading a file of any type. A condensed version of it is [HttpPost] public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string ...

I have a controller for uploading a file of any type. A condensed version of it is [HttpPost] public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string ...

I have two JSON objects that I want to merge in such a way that only the values of the properties in the first object that already exist are updated. I want to extend the first object with the second ...

I have two JSON objects that I want to merge in such a way that only the values of the properties in the first object that already exist are updated. I want to extend the first object with the second ...

  1. merge objects without overwriting
  2. javascript merge objects without overwriting
  3. merge array of objects without duplicates javascript
  4. javascript merge two objects without duplicates

I have a button right this: <button type="submit" class="btn blue pull-right" id="RequestNewPwd"> Submit <i class="m-icon-swapright m-icon-white"></i> <...

I have a button right this: <button type="submit" class="btn blue pull-right" id="RequestNewPwd"> Submit <i class="m-icon-swapright m-icon-white"></i> <...

  1. change jquery button text
  2. change jquery button color
  3. jquery change button text on click
  4. jquery change button value
  5. jquery change button background color
  6. jquery change button label
  7. jquery change button class
  8. jquery change button type
  9. jquery change button name
  10. jquery change button text onclick toggle
  11. jquery change button value onclick
  12. jquery change button onclick
  13. jquery change button title
  14. jquery change button disabled
  15. jquery change button
  16. jquery change button onclick function
  17. jquery change button id
  18. jquery change button color on click
  19. jquery change button text dynamically
  20. jquery change button type to submit