防止元素落后于固定div

时间:2015-09-28 06:59:52

标签: jquery html css

考虑以下示例

HTML

<div class="top">
    Hello
    <div>
        <a href="content">Click Here</a>
        <div id="content">Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here
        </div>

CSS

.top{
     position: fixed;
}  

#content{
     margin-top: 100px;
}

当点击链接点击这里时,课程content的内容落后于top课程,有什么办法可以阻止这个吗?就像id content的内容应该始终在类top之后显示这可以使用CSS或jQuery实现吗?

1 个答案:

答案 0 :(得分:2)

这是z-index css属性。仅当元素position不是静态时才会生效:fixedrelativeabsolute

.top a {
  z-index: 10;
  position: absolute;
  left: 20px;
  top: 10px;
  background-color: #ddd;
}
#content {
  border: 1px solid #898989;
  padding-top: 30px;
}
<div class="top">
  Hello
  <div>
    <a href="content">Click Here</a>
    <div id="content">
      Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here Content Here
    </div>
  </div>
</div>