我已经进行了大量的研究,但似乎无法弄清楚为什么这对我不起作用。
我有两个div元素。其中一个div具有“position: fixed”以使其始终保持在窗口中。
一旦div碰到页脚,它应该删除固定滚动并粘附到页脚的顶部。我尝试使用此示例中找到的代码,但当div到达页脚时,它只是回弹到页面顶部。
以下是一个示例fiddle:
我有两个div元素。其中一个div具有“position: fixed”以使其始终保持在窗口中。
一旦div碰到页脚,它应该删除固定滚动并粘附到页脚的顶部。我尝试使用此示例中找到的代码,但当div到达页脚时,它只是回弹到页面顶部。
以下是一个示例fiddle:
$(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>
有人能解释一下我在这里做错了什么吗?
bottom: 0
,但也没有任何效果。 - Jaxon Crosmas