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

在包含元素可见之前开始CSS动画

  •  1
  • user13286  · 技术社区  · 7 年前

    我正在制作一个动画,我想让4个元素看起来彼此独立地浮动,以实现我对所有4个元素使用相同的动画,但使用 animation-delay 以确保它们独立浮动。问题是这些动画发生在页面加载时隐藏的元素中,当该元素可见时,可以看到动画延迟发生。是否可以在包含的元素仍然隐藏的情况下开始动画,以便看不到延迟?否则,在不使CSS过于复杂化的情况下,使它们彼此独立浮动的最佳方法是什么?

    $(function() {
      $('button').click(function(e) {
        e.preventDefault();
        $('.scene.one').hide();
        $('.scene.two').fadeIn(500);
      });
    });
    .scenes {
      height:150px;
      width:300px;
      border:1px solid black;
    }
    h1 {
      margin:0;
    }
    p {
      margin-bottom:0;
      font-size:12px;
    }
    .scene {
      position:relative;
      height:100%;
      width:100%;
      text-align:center;
      display:none;
    }
      .scene-container {
        position:absolute;
        top:50%;
        left:50%;
        transform:translateX(-50%) translateY(-50%);
      }
      .scene.one {
        display:block;
      }
      
    .float {
      display:inline-block;
      width:25px;
      height:25px;
      background:black;
      animation-duration: 3s;
      animation-iteration-count: infinite;
      animation-timing-function: ease-in-out;
    }
      .animated .float {
        animation-name:floating;
      }
      .float:nth-child(2) {
        animation-delay:.5s;
      }
      .float:nth-child(3) {
        animation-delay:1.5s;
      }
      .float:nth-child(4) {
        animation-delay:1s;
      }
    
    @keyframes floating {
        from { transform: translate(0,  0px); }
        65%  { transform: translate(0, 15px); }
        to   { transform: translate(0, -0px); }    
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="scenes">
      <div class="scene one">
        <div class="scene-container">
          <h1>WELCOME</h1>
          <button>Next scene</button>
          <p>(Animation delay is still visible even if you wait 2 seconds before clicking)</p>
        </div>
      </div>
      <div class="scene two">
        <div class="scene-container">
          <div class="float-container animated">
            <div class="float"></div>
            <div class="float"></div>
            <div class="float"></div>
            <div class="float"></div>
          </div>
        </div>
      </div>
    </div>
    2 回复  |  直到 7 年前
        1
  •  1
  •   vals    7 年前

    您可以使用负延迟来实现这一点。

    我已经使用calc设置了它们,以使逻辑清晰,但是您也可以使用计算值。

    $(function() {
      $('button').click(function(e) {
        e.preventDefault();
        $('.scene.one').hide();
        $('.scene.two').fadeIn(500);
      });
    });
    .scenes {
      height:150px;
      width:300px;
      border:1px solid black;
    }
    h1 {
      margin:0;
    }
    p {
      margin-bottom:0;
      font-size:12px;
    }
    .scene {
      position:relative;
      height:100%;
      width:100%;
      text-align:center;
      display:none;
    }
      .scene-container {
        position:absolute;
        top:50%;
        left:50%;
        transform:translateX(-50%) translateY(-50%);
      }
      .scene.one {
        display:block;
      }
      
    .float {
      display:inline-block;
      width:25px;
      height:25px;
      background:black;
      animation-duration: 3s;
      animation-iteration-count: infinite;
      animation-timing-function: ease-in-out;
    }
      .animated .float {
        animation-name:floating;
      }
      .float:nth-child(2) {
        animation-delay:calc(.5s - 3s);
      }
      .float:nth-child(3) {
        animation-delay:calc(1.5s - 3s);
      }
      .float:nth-child(4) {
        animation-delay:calc(1s - 3s);
      }
    
    @keyframes floating {
        from { transform: translate(0,  0px); }
        65%  { transform: translate(0, 15px); }
        to   { transform: translate(0, -0px); }    
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="scenes">
      <div class="scene one">
        <div class="scene-container">
          <h1>WELCOME</h1>
          <button>Next scene</button>
          <p>(Animation delay is still visible even if you wait 2 seconds before clicking)</p>
        </div>
      </div>
      <div class="scene two">
        <div class="scene-container">
          <div class="float-container animated">
            <div class="float"></div>
            <div class="float"></div>
            <div class="float"></div>
            <div class="float"></div>
          </div>
        </div>
      </div>
    </div>
        2
  •  1
  •   Obsidian Age    7 年前

    如果你选择利用 visibility: visible visibility: hidden 而不是 display 然后可以在用户看到元素之前设置它们的动画。不幸的是,这意味着你将不能有淡入淡出的效果(因为这是特别的工作关闭的) 显示

    $(function() {
      $('button').click(function(e) {
        e.preventDefault();
        $('.scene.one').css('display', 'none');
        $('.scene.two').css('visibility', 'visible');
      });
    });
    .scenes {
      height: 150px;
      width: 300px;
      border: 1px solid black;
    }
    
    h1 {
      margin: 0;
    }
    
    p {
      margin-bottom: 0;
      font-size: 12px;
    }
    
    .scene {
      position: relative;
      height: 100%;
      width: 100%;
      text-align: center;
      visibility: hidden;
    }
    
    .scene-container {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translateX(-50%) translateY(-50%);
    }
    
    .scene.one {
      visibility: visible;
    }
    
    .float {
      display: inline-block;
      width: 25px;
      height: 25px;
      background: black;
      animation-duration: 3s;
      animation-iteration-count: infinite;
      animation-timing-function: ease-in-out;
    }
    
    .animated .float {
      animation-name: floating;
    }
    
    .float:nth-child(2) {
      animation-delay: .5s;
    }
    
    .float:nth-child(3) {
      animation-delay: 1.5s;
    }
    
    .float:nth-child(4) {
      animation-delay: 1s;
    }
    
    @keyframes floating {
      from {
        transform: translate(0, 0px);
      }
      65% {
        transform: translate(0, 15px);
      }
      to {
        transform: translate(0, -0px);
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="scenes">
      <div class="scene one">
        <div class="scene-container">
          <h1>WELCOME</h1>
          <button>Next scene</button>
          <p>(Wait 2 seconds before clicking)</p>
        </div>
      </div>
      <div class="scene two">
        <div class="scene-container">
          <div class="float-container animated">
            <div class="float"></div>
            <div class="float"></div>
            <div class="float"></div>
            <div class="float"></div>
          </div>
        </div>
      </div>
    </div>
    推荐文章