I have a requirement to scroll a selected element in a list to the top of a page. scrollTop() works when the selected element's distance from the top is less than the available scroll space. If it is more than the available space, it will only scroll part way up the screen (until it runs out of scroll space). See the JSFiddle here (for example, when selecting element 24, I want list item #24 to scroll to the top of the screen).
I have taken a look at several other examples such as here but this only scrolls to the bottom of the page. It does not take the bottom of the page and move it to the top.
My idea so far is to hardcode a div of white space at the bottom of the list, and have this grow/shrink to allow for more scrolling, but I want to know if there's a nicer way of doing this. I'm new to web development, and this honestly feels very hack-ish.
<html>
<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js></script>
<script type="text/javascript">
$(window).load(function(){
$('#scroll_navigation ul li').click(function() {
var distanceToBottom = $('#scroll_navigation').scrollHeight -$('#scroll_navigation').height() - $('#scroll_navigation').scrollTop();
$('html,body').animate({scrollTop: $(this).offset().top}, 500);
});
});
</script>
</head>
<body>
<div id="scroll_navigation">
<p>Catch any clicks in "#scroll_navigation" to scroll the list item to the top of the page</p>
<div class="container">
<ul>
<li><a href="#Portfolio" title="">1</a></li>
<li><a href="#Services" title="">2</a></li>
<li><a href="#About" title="">3</a></li>
<li><a href="#Contact" title="">4</a></li>
<li><a href="#Portfolio" title="">5</a></li>
<li><a href="#Services" title="">6</a></li>
<li><a href="#About" title="">7</a></li>
<li><a href="#Contact" title="">8</a></li>
<li><a href="#Portfolio" title="">9</a></li>
<li><a href="#Services" title="">10</a></li>
<li><a href="#About" title="">11</a></li>
<li><a href="#Contact" title="">12</a></li>
<li><a href="#Portfolio" title="">13</a></li>
<li><a href="#Services" title="">14</a></li>
<li><a href="#About" title="">15</a></li>
<li><a href="#Contact" title="">16</a></li>
<li><a href="#Portfolio" title="">17</a></li>
<li><a href="#Services" title="">18</a></li>
<li><a href="#About" title="">19</a></li>
<li><a href="#Contact" title="">20</a></li>
<li><a href="#Portfolio" title="">21</a></li>
<li><a href="#Services" title="">22</a></li>
<li><a href="#About" title="">23</a></li>
<li><a href="#Contact" title="">24</a></li>
<li><a href="#Portfolio" title="">25</a></li>
<li><a href="#Services" title="">26</a></li>
<li><a href="#About" title="">27</a></li>
<li><a href="#Contact" title="">28</a></li>
<li><a href="#Portfolio" title="">29</a></li>
<li><a href="#Services" title="">30</a></li>
<li><a href="#About" title="">31</a></li>
<li><a href="#Contact" title="">32</a></li>
<li><a href="#Portfolio" title="">33</a></li>
<li><a href="#Services" title="">34</a></li>
<li><a href="#About" title="">35</a></li>
<li><a href="#Contact" title="">36</a></li>
<li><a href="#Portfolio" title="">37</a></li>
<li><a href="#Services" title="">38</a></li>
<li><a href="#About" title="">39</a></li>
<li><a href="#Contact" title="">40</a></li>
<li><a href="#Portfolio" title="">41</a></li>
<li><a href="#Services" title="">42</a></li>
<li><a href="#About" title="">43</a></li>
<li><a href="#Contact" title="">44</a></li>
</ul>
</div>
<div id="white-space" style="height: 0; width:100%; clear:both; border-style:solid">
</div>