1110
1110

Reputation: 6839

How to add padding to scrollTop() function

I have a parent div with a list of div's.
In parrent div I also added fixed positioned div on the top of parent div.
Based on some action I call:

$('#parent').animate({       
                    scrollTop: $("#" + itemId).offset().top  - $('#parent').offset().top + $('#parent').scrollTop()
                     }, 500);

This successfully scroll to a div and put it on the top.
The issue is that when searched div is on the top I can't see it because it is bellow fixed div.

<div id="parent">
<div id="fixedDiv" style="position: fixed;
z-index: 999;
background: #000;
width: 300px;">
... some elements ...
</div>
<ul>
... list of divs...
</div> 

Is there some solution to scroll to div padded from the top of parent?

Upvotes: 7

Views: 21988

Answers (2)

kaleazy
kaleazy

Reputation: 6242

This is what I use, for a smooth scroll with 10px of upper padding:

$('body').animate({scrollTop: $('#div').offset().top-10},1000);

Upvotes: 25

Marcin Bobowski
Marcin Bobowski

Reputation: 1775

- $('#fixedDiv').height()

at end of scrollTop line will do :)

Upvotes: 4

Related Questions