【问题标题】:Position sticky combined with a z-index位置粘性与 z-index 结合
【发布时间】:2019-01-24 12:32:36
【问题描述】:

在下面的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; 属性,这是使动画工作所必需的。因此,我还在CSS 中添加了z-index,如答案here 中所述,以便在用户向下滚动页面后获得标题下方的动画。

但是,不知何故,我无法使 z-indexposition: sticky; 属性结合使用,因为当我向下滚动时,两个标题都消失了。

你知道我需要在我的代码中改变什么,所以一旦用户向下滚动:

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

【问题讨论】:

    标签: html css css-animations z-index


    【解决方案1】:

    只需删除使主体仅具有顶部标题的高度的浮动(不需要),因此粘性将无法按预期工作:

    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>

    【讨论】:

    • 谢谢伙计。正是我需要的。我也在jsfiddle.net/zwujpL5o这里更新了它,面板作为菜单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多