SO - detect position on a page

http://stackoverflow.com/questions/8369833/detect-users-position-on-web-page

by Paul Schmitt

HTML

<div class="scroll"></div>
<div class="content">HELLO</div>
<div class="scroll"></div>

CSS

div.scroll {
    background-color: #eee;
    width: 100px;
    height: 1000px;
}

div.content {
    background-color: #bada55;
    width: 100px;
    height: 200px;
}

JavaScript

var $window = $(window);
// # of pixels from the top of the document to the top of div.content
var contentTop = $("div.content").offset().top;
// content is visible when it is on the bottom of the window and not at the top
var contentStart = contentTop - $window.height();
// content is still visible if any part of his height is visible
var contentEnd = contentTop + $("div.content").height();

$window.scroll(function() {
    var scrollTop = $window.scrollTop();
    if(scrollTop > contentStart && scrollTop < contentEnd) {
        console.log('You can see "HELLO"!');
    } else {
        console.log('You cannot see "HELLO"!');
    }
});