I need to have a footer in relation to it's parent and I need it to be fixed to the bottom of the window.
I made a calculation to get the right top
value but I am missing 8 pixels - this causes the scroll to scroll indefinitely.
What am I missing in my calculation?
$(function () {
changeFooter();
});
$(window).resize(function () {
changeFooter();
});
$(window).scroll(function () {
changeFooter();
});
function changeFooter() {
var footer = $("#footer");
footer.css({ top: getFooterTop(footer) + 'px' });
}
function getFooterTop(footer) {
return window.innerHeight + $(window).scrollTop() - footer.height();
}
function getFooterTopFixed(footer) {
return window.innerHeight + $(window).scrollTop() - footer.height() - 8;
}
#wrap {
position:absolute;
left:0px;
width: 100%;
margin-bottom: 50px;
}
#footer {
position: absolute;
width: 100%;
height: 50px;
background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrap">
<p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p>
<div id="footer"></div>
</div>
https://jsfiddle.net/rs4f1jt0/2/
You can see in the link that -8 pixels fixes the positioning, I just can't figure out where are those 8 pixels coming from to fix the calculation.
Thanks.