Ludwig
Ludwig

Reputation: 1781

Scroll down all the way to the point where div is visible

I have a function where I enable scrolling only to for the difference of the div height and window height, so that it doesn't scroll down from the point where div ends. But that is not what I want, because if I resize the screen to smaller size, user is not able to scroll all the way down to the end of that particular div. How can I modify this so that I enable scrolling to the point where the div is visible and only to that point and not over it?

scrollPoint = $(".magazine-section").offset().top - $(window).height();

$(window).scroll(function() {
    $(window).scrollTop() > scrollPoint ? $(window).scrollTop(scrollPoint) : '';
}).scroll();

Upvotes: 0

Views: 83

Answers (1)

Dan Wilson
Dan Wilson

Reputation: 4047

You need to set scrollPoint again when the window is resized using $(window).resize().

$(window).resize(function() {
   scrollPoint = $(".magazine-section").offset().top - $(window).height();
}

Upvotes: 2

Related Questions