代码之家  ›  专栏  ›  技术社区  ›  Michi

位置粘性结合Z索引

  •  0
  • Michi  · 技术社区  · 6 年前

    在下面 HTML CSS 我创建了一个标题和一个图像动画,您也可以在 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; 使动画工作所必需的属性。因此,我还添加了 z-index 对我 CSS 如答案所述 here 当用户向下滚动页面时,获取标题下的动画。

    然而,不知何故我不能 Z指数 结合工作 position: sticky; 属性,因为当向下滚动时,两个标题都会消失。

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

    a)第一 Healer-01 消失和消失
    b)第二个 头标02 保持不变,并且
    c) .图像动画 在收割台后面。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Temani Afif    6 年前

    只需移除浮球(不需要),使主体只有顶部收割台具有相同高度,因此粘性不会按预期工作:

    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>