在我的网站中,我必须创建一个政策横幅。
我的网站已经包含内容和页脚的 div。
我想要的是?
现在,我已经实现的大部分事情,但问题是将页脚保持在底部,横幅滚动应停止在页脚上方。
我的示例代码:
.main-area {
height: 1000vh;
background-color: #ccc;
width: 2000px;
position: relative;
}
.policy-banner {
height: 100px;
width: 2000px;
color: yellow;
background-color: blue;
position: fixed;
bottom: 0;
left: 0;
}
.footer {
background-color: black;
height: 50px;
color: white;
position: fixed;
bottom: 0;
left: 0;
}
Run Code Online (Sandbox Code Playgroud)
<html>
<head>
</head>
<body>
<div class="main-area">
<section>another sections coming dynamically</section>
<section>another sections coming dynamically</section>
<div class="policy-banner">
this is banner, it should stick to the user's view port.
when user scroll, it will also scroll but upto footer and should not overlap with footer.
</div>
</div>
<footer class="footer">
this is footer. it will stay at the end of the page.
</footer>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激。谢谢。
编辑: 这不是重复的问题,因为我想要的是 css 解决方案,这个标记的答案解决了我的粘性横幅问题。
你想要的是拥有position: sticky;
而不是fixed
. 无需过多赘述,这position: sticky;
意味着元素要么表现得像它是相对定位的,直到它满足条件,然后表现得像在满足条件的持续时间内固定。
在下面的示例中,条件是bottom: 0;
,因此它将开始固定,因为您从其父 div 开始.main-area
,但是一旦您在父 div 之外就停止。
.main-area {
background-color: #ccc;
}
.main-area .empty {
height: 500px;
background-color: #ddd;
}
.policy-banner {
height: 100px;
color: yellow;
background-color: blue;
position: -webkit-sticky;
position: sticky;
bottom: 0;
}
.footer {
background-color: black;
height: 50px;
width: 100%;
color: white;
}
Run Code Online (Sandbox Code Playgroud)
<html>
<head>
</head>
<body>
<div class="main-area">
<div class="empty">Section 1</div>
<div class="empty">Section 2</div>
<div class="policy-banner">
this is banner, it should stick to the user's view port.
when user scroll, it will also scroll but upto footer and should not overlap with footer.
</div>
</div>
<footer class="footer">
this is footer. it will stay at the end of the page.
</footer>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)