Offset().top - (100% - 100px) of viewport?

I have this, a regular animate scrollTop:

jQuery('html, body').animate({ 
   scrollTop: jQuery('#element').offset().top 
}, 1000);

My question is if it is possible to make the offset().top stop at 100px from the bottom of the viewport?

offset().top-100

Something like that should work.
If not, please provide a fiddle.

hi, yeah but - 100 only stops the animation 100px from the top, I want it to stop 100px from the bottom of the viewport (think calc(100% - 100px)).

Here is a fiddle: http://jsfiddle.net/d0h4r73t/11/

As you see here the scroll-to element stops 50px from the top, but lets say I want it to stop 100px from the bottom of the viewport instead.

What about calc(100vh - 100px)? (Untested!)

But that is CSS

I tried:

$('a').click(function(e){
    e.preventDefault();
    $('body, html').animate({
        scrollTop: $('.scroll-to-this').offset().top - $(window).height() - 100
    }, 1000);
});

but that doesn’t work.

You were almost there:

$('a').click(function(e){
    e.preventDefault();
    var desiredHeight = $(window).height() - 100;
    $('body, html').animate({
        scrollTop: $('.scroll-to-this').offset().top - desiredHeight
    }, 1000);
});
2 Likes

Yeah, It worked when i wrapped $(window).height() - 100 in ( ) .

Question: Your solution with the variable, wouldn’t that output the same as my failed solution? Or does using a variable give the same result as wrapping it in ( ) like this: ($(window).height() - 100)

Yes.
It’s a question of operator precedence.
Glad you got it solved.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.