如何在滚动时固定 div?
请看下面的截图:
这张截图是固定在顶部的:
而这张截图则是在滚动时固定的:
我的意思是,如果固定到顶部后,div 恢复正常(没有 CSS 固定效果)。
请看这张截图,我想要像这样,如果 div 固定在顶部,则回到正常状态(无需 CSS 粘性):
这是我的粘性元素:
请看下面的截图:
这张截图是固定在顶部的:
![enter image description here](https://istack.dev59.com/sEptR.webp)
![enter image description here](https://istack.dev59.com/Fz1uI.webp)
请看这张截图,我想要像这样,如果 div 固定在顶部,则回到正常状态(无需 CSS 粘性):
![enter image description here](https://istack.dev59.com/Se8pw.webp)
@media screen and (min-width: 768px) {
.sticky {
position: fixed;
z-index: 9999;
display: block;
background-color: #2069e8;
width: 100%;
padding: 10px;
margin-top: -10px;
padding-top:10px
}
}
<div class="sticky">
<div class="col-sm-2">
<h2 style="margin:0px; width:250px;"><span class="smallnav menustater" onclick="openNav()"><i class="fa fa-th-list"></i></span> <a href="http://myweb.com" style="color:#ffffff; text-decoration:none; position:absoulute; display:block; margin-top:-33px; margin-left:38px; z-index:5; width:250px; font-weight:bold;">MY WEB</a></h2>
</div>
</div>
请帮忙
先谢谢你