I've done plenty of research into this and I can't seem to figure out why this isn't working for me.
I have two div elements. One of these divs has position: fixed
to keep it in the window at all times.
Once the div hits the footer, it should remove the fixed scrolling and stick to the top of the footer. I've tried using the code found in this example, but the div just snaps back up to the top of the page when it reaches the footer.
Here's a fiddle for an example:
$(document).scroll(function (){
if($('.userInfo').offset().top + $('.userInfo').height() >= $('footer').offset().top - 10){
$('.userInfo').css('position', 'absolute');
}
if($(document).scrollTop() + window.innerHeight < $('footer').offset().top){
$('.userInfo').css('position', 'fixed');
}
});
.profileMain{
display: flex;
}
.userInfoContainer{
height: 100%;
width: 50%;
display: inline-block;
}
.userInfo{
background: red;
width: 50%;
position: fixed;
}
.userContent{
background: blue;
width: 50%;
margin-bottom: 10em;
}
footer{
background: gray;
width: 100%;
height: 30em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = "profileMain">
<div class = "userInfoContainer">
<div class = "userInfo">f</div>
</div>
<div class = "userContent">f</div>
</div>
<footer></footer>
Can someone explain to me what I'm doing wrong here?