位置粘性结合z索引

时间:2019-01-24 12:32:37

标签: html css css-animations z-index

在下面的HTMLCSS中,我创建了标题和图像动画,您也可以在JSfiddle here中找到这些内容:

body {
  margin: 0;
}


/* 01.00 HEADER: Items in header */

.header_01 {
  width: 80%;
  height: 10vh;
  
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  z-index:99;
  
  
  text-align: center;
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: orange;    
}

.header_02 {
  width: 80%;
  height: 10vh;
  
  margin: 10vh auto 0;
  position: sticky;
  z-index:99;
  
  top:0;
  display: flex;
  justify-content: space-between;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: yellow;
}

.image {
  width: 30%;
  height: 100%;
  display: flex;
  justify-content: center;
  text-align: center;
  align-items: center;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: green;
}

.navigation {
  width: 70%;
  height: 100%;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: aqua;
}


/* 02.00 NAVIGATION */

.navigation>ul {
  height: 100%;
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: blue;
}

.navigation>ul>li {
  width: 100%;
  display: flex;
  justify-content: center;
  text-align: center;
  align-items: center;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
}



/* 03.00 CONTENT */

.image_animation {
 width: 80%;
 margin-left: 10%;
 margin-top: 15%;
 float: left;
 display: flex;
 justify-content: space-between;

 background-color: green;
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
}
 
.image_list {
 width: 100%;
 position: relative;
	
 background-color: red;
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
}
	
.image_list img {
 width: 100%;
 height: 100%;
}
 
.image1 {
 height: 100%;
 width: 100%;
 float: left;
 position: absolute;
}
 
.image2 {
 height: 100%;
 width: 100%;
 float: left;
 animation-delay: 2s;
}
 
.image_list div {
 animation-name: animation_home_images;
 animation-duration:4s;
 animation-iteration-count:infinite;
 animation-fill-mode: forwards;
 opacity:0;
 }

@keyframes animation_home_images {
  50.0% {
    opacity: 1
  }
  0%, 100%{
    opacity: 0
  }
}
<div class="header_01">
This is our webpage.
</div>


<div class="header_02">	

      <div class="image">
      Image
      </div>
  
      <nav class="navigation"> 
      
        <ul>
        
          <li class="button_01"> 1.0 Main Menu </li>
          <li class="button_01"> 2.0 Main Menu </li>
          <li class="button_01"> 3.0 Main Menu </li>
          
        </ul>
        
      </nav>
      
</div>	


<div class="image_animation">
    
  <div class="image_list">
    <div class="image1"><img src="http://placehold.it/101x101"></div>
		<div class="image2"><img src="http://placehold.it/201x201"></div>
	</div>
        
</div>

如您所见,我有一个header,它由两部分组成。一旦用户向下滚动页面,第一个.header_01应该消失,而第二个.header_02应该保持固定。我最初是通过问题here的答案实现的。

到目前为止,所有这些都工作正常。


现在,我在标头下方添加了一个.image-animation属性,该属性是使动画正常运行所必需的postion: absolute;。因此,我还按照答案here的说明在z-index上添加了CSS,一旦用户向下滚动页面,动画就会显示在标题下方。

但是,由于某种原因,我无法使z-indexposition: sticky;属性结合使用,因为当我向下滚动时,两个标题都消失了。

您知道我需要在代码中进行哪些更改,以便一旦用户向下滚动即可:

a)第一个.header_01消失了,并且
b)第二个.header_02保持不变,并且
c).image-animation放在标题后面。

1 个答案:

答案 0 :(得分:0)

简单地移除浮子(不需要),该浮子使主体的高度仅为顶部标头,因此粘性无法按预期工作:

body {
  margin: 0;
}


/* 01.00 HEADER: Items in header */

.header_01 {
  width: 80%;
  height: 10vh;
  
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  z-index:99;
  
  
  text-align: center;
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: orange;    
}

.header_02 {
  width: 80%;
  height: 10vh;
  
  margin: 10vh auto 0;
  position: sticky;
  z-index:99;
  
  top:0;
  display: flex;
  justify-content: space-between;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: yellow;
}

.image {
  width: 30%;
  height: 100%;
  display: flex;
  justify-content: center;
  text-align: center;
  align-items: center;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: green;
}

.navigation {
  width: 70%;
  height: 100%;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: aqua;
}


/* 02.00 NAVIGATION */

.navigation>ul {
  height: 100%;
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: blue;
}

.navigation>ul>li {
  width: 100%;
  display: flex;
  justify-content: center;
  text-align: center;
  align-items: center;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
}



/* 03.00 CONTENT */

.image_animation {
 width: 80%;
 margin-left: 10%;
 margin-top: 15%;
 display: flex;
 justify-content: space-between;

 background-color: green;
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
}
 
.image_list {
 width: 100%;
 position: relative;
 overflow:hidden;
	
 background-color: red;
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
}
	
.image_list img {
 width: 100%;
 height: 100%;
}
 
.image1 {
 height: 100%;
 width: 100%;
 position: absolute;
}
 
.image2 {
 height: 100%;
 width: 100%;
 display:block;
 animation-delay: 2s;
}
 
.image_list div {
 animation-name: animation_home_images;
 animation-duration:4s;
 animation-iteration-count:infinite;
 animation-fill-mode: forwards;
 opacity:0;
 }

@keyframes animation_home_images {
  50.0% {
    opacity: 1
  }
  0%, 100%{
    opacity: 0
  }
}
<div class="header_01">
This is our webpage.
</div>


<div class="header_02">	

      <div class="image">
      Image
      </div>
  
      <nav class="navigation"> 
      
        <ul>
        
          <li class="button_01"> 1.0 Main Menu </li>
          <li class="button_01"> 2.0 Main Menu </li>
          <li class="button_01"> 3.0 Main Menu </li>
          
        </ul>
        
      </nav>
      
</div>	


<div class="image_animation">
    
  <div class="image_list">
    <div class="image1"><img src="http://placehold.it/101x101"></div>
		<div class="image2"><img src="http://placehold.it/201x201"></div>
	</div>
        
</div>