Skip to content Skip to sidebar Skip to footer

Detect When Body Scroll Hits Top | Bottom Of Element

I'm trying to detect using javascript and jquery when the window scroll hits the top of a selected element. I think I'm making progress but still no results: fiddle: https://jsfid

Solution 1:

$(window).scroll(function() {
    var targetScroll = $('.div').position().top;
    if($(window).scrollTop() > targetScroll) {
        alert('hey');
    }); // <=== Syntax Error: Closing Parenthese around an if block
});

Solution 2:

$(window).scroll(function() 
{
    var targetScroll = $('.div').offset().top - $('.div').outerHeight();
    if($(window).scrollTop() > targetScroll)
    {
        alert('hey');
    }
});

fbelanger was right about the syntax error, but the function would still fire at the bottom of the div. Just subtract the height of the div from its offset from the top. Hope this helps!


Post a Comment for "Detect When Body Scroll Hits Top | Bottom Of Element"