Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 300
1110 Avatar asked Jan 09 '13 22:01

1110


2 Answers

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

$('body').animate({scrollTop: $('#div').offset().top-10},1000);
like image 71
kaleazy Avatar answered Sep 19 '22 10:09

kaleazy


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

at end of scrollTop line will do :)

like image 44
Marcin Bobowski Avatar answered Sep 19 '22 10:09

Marcin Bobowski