3 回答
TA贡献1872条经验 获得超3个赞
使用 JQuery,您可以添加样式为position:fixedand的类,top:0并使用滚动功能触发,以获得更好的侧面元素视图/效果。
$(window).scroll(function(){
var header_height = $('#header').height();
if ($(this).scrollTop() > header_height) {
$('#left').addClass('fixed');
} else {
$('#left').removeClass('fixed');
}
});
#header,#footer{
width:100%;
height:80px;
background:blue;
}
#content{
width:1000px;
margin:0 auto;
overflow:hidden;
}
#left{
width:200px;
float:left;
background:green;
margin:20px 0;
height:100vh;
}
#right{
float:right;
width:calc(100% - 220px);
}
.item{
height:80px;
margin:20px 0;
background:red;
}
.fixed{
position:fixed;
top:0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="header">
header
</div>
<div id="content">
<div id="left">
left
</div>
<div id="right">
<!-- this content is scrolling-->
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
</div>
</div>
<div id="footer">
footer
</div>
TA贡献1821条经验 获得超4个赞
position: fixed在CSS 中使用属性
#header,#footer{
width:100%;
height:80px;
background:blue;
}
#content{
width:1000px;
margin:0 auto;
overflow:hidden;
}
#left{
width:200px;
float:left;
background:green;
/* Enter The Position Fixed Property Here! */
position:fixed;
margin:20px 0;
height:100vh;
}
#right{
float:right;
width:calc(100% - 220px);
}
.item{
height:80px;
margin:20px 0;
background:red;
}
<div id="header">
header
</div>
<div id="content">
<div id="left">
left
</div>
<div id="right">
<!-- this content is scrolling-->
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
</div>
</div>
<div id="footer">
footer
</div>
有一些位置错位,因为你有你的头。将此属性用于要滚动的 div。根据需要进行放置
添加回答
举报