【问题标题】:How to layer animation behind another animation in CSS and HTML?如何在 CSS 和 HTML 中将动画分层放在另一个动画后面?
【发布时间】:2020-08-14 00:14:06
【问题描述】:

我正在努力弄清楚如何对 2 层不同的动画进行分层。我想在移动的月亮动画和网站瓷砖后面制作闪烁的星星动画(这样背景本质上就是闪烁的星星和移动的月亮)。现在,它只是叠加在月球动画和标题之上,完全覆盖它们,并且位置已关闭。我对 HTML 比较陌生,所以如果我的问题很明显,请原谅。 TIA

@keyframes moon {
  0% {
    box-shadow: -150px 0px 0px 0px white;
    transform: rotate(-10deg);
  }
  50% {
    box-shadow: 0px 0px 0px 0px white;
  }
  100% {
    box-shadow: 150px 0px 0px 0px white;
    transform: rotate(10deg);
  }
  z-index: -1;
  position: relative;
}

@keyframes move-twink-back {
  from {
    background-position: 0 0;
  }
  to {
    background-position: -10000px 5000px;
  }
}

@-webkit-keyframes move-twink-back {
  from {
    background-position: 0 0;
  }
  to {
    background-position: -10000px 5000px;
  }
}

@-moz-keyframes move-twink-back {
  from {
    background-position: 0 0;
  }
  to {
    background-position: -10000px 5000px;
  }
}

@-ms-keyframes move-twink-back {
  from {
    background-position: 0 0;
  }
  to {
    background-position: -10000px 5000px;
  }
}

.stars,
.twinkling {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  display: block;
}

.stars {
  background: #000 url(http://www.script-tutorials.com/demos/360/images/stars.png) repeat top center;
  z-index: -1;
}

.twinkling {
  background: transparent url(http://www.script-tutorials.com/demos/360/images/twinkling.png) repeat top center;
  z-index: 0;
  -moz-animation: move-twink-back 200s linear infinite;
  -ms-animation: move-twink-back 200s linear infinite;
  -o-animation: move-twink-back 200s linear infinite;
  -webkit-animation: move-twink-back 200s linear infinite;
  animation: move-twink-back 200s linear infinite;
}

html {
  z-index: -1;
  position: relative;
  background-image: linear-gradient(black 80%, #041931, #fff);
  background-repeat: no-repeat;
  height: 45%;
}

body {
  font-family: 'Manrope', sans-serif;
  background: none;
}

div {
  background: none;
  z-index: 1;
}

.moon {
  z-index: -1;
  position: relative;
  width: 10rem;
  height: 10rem;
  background: #0a0a0a;
  margin: 1rem auto;
  animation: moon 20s linear infinite alternate;
  border-radius: 50%;
  box-shadow: inset -10rem 0 whitesmoke;
  transform: rotate(-10deg);
}
<html>
<style>
  div {
    margin: 10%;
  }
</style>

<body>
  <h1 id="text">
    <center> Welcome to my Site! </center>
  </h1>
  <div class="moon"></div>
  <div class="stars"></div>
  <div class="twinkling"></div>
  <div class="maintext">
    <h2>This is text below the animations</h2>
  </div>
  <script type="text/javascript">
    setTimeout(function() {
      $('.moon').css("animation-play-state", "paused");
    }, 20000)
  </script>
</body>

</html>

【问题讨论】:

    标签: javascript html jquery css animation


    【解决方案1】:

    您不需要将z-index 添加到所有元素,因为定位看起来不规则。我刚刚删除了其他部分的 z-index 并将它们包含在相关部分中,即moonstarstwinkling

    我还必须删除 div 元素上设置的边距。以全屏模式查看演示。

    @keyframes moon {
      0% {
        box-shadow: -150px 0px 0px 0px white;
        transform: rotate(-10deg);
      }
      50% {
        box-shadow: 0px 0px 0px 0px white;
      }
      100% {
        box-shadow: 150px 0px 0px 0px white;
        transform: rotate(10deg);
      }
      position: relative;
    }
    
    @keyframes move-twink-back {
      from {
        background-position: 0 0;
      }
      to {
        background-position: -10000px 5000px;
      }
    }
    
    @-webkit-keyframes move-twink-back {
      from {
        background-position: 0 0;
      }
      to {
        background-position: -10000px 5000px;
      }
    }
    
    @-moz-keyframes move-twink-back {
      from {
        background-position: 0 0;
      }
      to {
        background-position: -10000px 5000px;
      }
    }
    
    @-ms-keyframes move-twink-back {
      from {
        background-position: 0 0;
      }
      to {
        background-position: -10000px 5000px;
      }
    }
    
    .stars,
    .twinkling {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      width: 100%;
      height: 100%;
      display: block;
    }
    
    .stars {
      background: #000 url(http://www.script-tutorials.com/demos/360/images/stars.png) repeat top center;
      z-index: -3;
    }
    
    .twinkling {
      background: transparent url(http://www.script-tutorials.com/demos/360/images/twinkling.png) repeat top center;
      -moz-animation: move-twink-back 200s linear infinite;
      -ms-animation: move-twink-back 200s linear infinite;
      -o-animation: move-twink-back 200s linear infinite;
      -webkit-animation: move-twink-back 200s linear infinite;
      animation: move-twink-back 200s linear infinite;
      z-index: -2;
    }
    
    html, body {
      font-family: 'Manrope', sans-serif;
      height: 100%;
    }
    
    .moon {
      position: relative;
      width: 10rem;
      height: 10rem;
      background: #0a0a0a;
      margin: 1rem auto;
      animation: moon 20s linear infinite alternate;
      border-radius: 50%;
      box-shadow: inset -10rem 0 whitesmoke;
      transform: rotate(-10deg);
      z-index: -1;
    }
    
    #text, .maintext { color: white; text-align: center; margin-top: 25px; }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <html>
    <style>
    
    </style>
    
    <body>
      <div class="header">
        <h1 id="text">
          <center> Welcome to my Site! </center>
        </h1>
      </div>
      <div class="animation-part">
        <div class="twinkling"></div>
        <div class="moon"></div>
        <div class="stars"></div>
      </div>
      <div class="maintext">
        <h2>This is text below the animations</h2>
      </div>
      <script type="text/javascript">
        setTimeout(function() {
          $('.moon').css("animation-play-state", "paused");
        }, 20000)
      </script>
    </body>
    
    </html>

    【讨论】:

    • 这太棒了! “欢迎来到我的网站!”有什么原因吗?不再可见?
    • 那是因为造型不完美。我不得不删除 45% 的高度。为文本添加颜色。
    • 谢谢!你认为有可能用闪烁的星星将渐变保持在底部吗?当我们把它放在月球后面时它就消失了
    • 检查其他答案。这会很有帮助。我以为你想要一个全屏的身体背景。但我建议您使用响应式框架semantic-ui.com 来开发您的网页。为每个部分编写代码非常耗时。
    【解决方案2】:

    此 z-index 和 css 调整将解决您的问题。

    @keyframes moon {
      0% {
        box-shadow: -150px 0px 0px 0px white;
        transform: rotate(-10deg);
      }
      50% {
        box-shadow: 0px 0px 0px 0px white;
      }
      100% {
        box-shadow: 150px 0px 0px 0px white;
        transform: rotate(10deg);
      }
      z-index: -1;
      position: relative;
    }
    
    @keyframes move-twink-back {
      from {
        background-position: 0 0;
      }
      to {
        background-position: -10000px 5000px;
      }
    }
    
    @-webkit-keyframes move-twink-back {
      from {
        background-position: 0 0;
      }
      to {
        background-position: -10000px 5000px;
      }
    }
    
    @-moz-keyframes move-twink-back {
      from {
        background-position: 0 0;
      }
      to {
        background-position: -10000px 5000px;
      }
    }
    
    @-ms-keyframes move-twink-back {
      from {
        background-position: 0 0;
      }
      to {
        background-position: -10000px 5000px;
      }
    }
    
    html {
      background-image: linear-gradient(black 80%, #041931, #fff);
      background-repeat: no-repeat;
      height: 45%;
    }
    
    body {
      font-family: 'Manrope', sans-serif;
      background: none;
      color: #fff;
      position: relative;
      margin:0;
      padding:0;
    }
    
    .stars,
    .twinkling {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      width: 100%;
      height: 100%;
      display: block;
    }
    
    .stars {
      background: #000 url(http://www.script-tutorials.com/demos/360/images/stars.png) top center;
      background-size: cover;
      z-index: -1;
      width: 100%;
    }
    
    .twinkling {
      background: transparent url(http://www.script-tutorials.com/demos/360/images/twinkling.png) repeat top center;
      z-index: -1;
      -moz-animation: move-twink-back 200s linear infinite;
      -ms-animation: move-twink-back 200s linear infinite;
      -o-animation: move-twink-back 200s linear infinite;
      -webkit-animation: move-twink-back 200s linear infinite;
      animation: move-twink-back 200s linear infinite;
    }
    
    .moon {
      z-index: 1;
      position: relative;
      width: 10rem;
      height: 10rem;
      background: #000000;
      margin: 1rem auto;
      animation: moon 20s linear infinite alternate;
      border-radius: 50%;
      box-shadow: inset -10rem 0 whitesmoke;
      transform: rotate(-10deg);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
    </head>
    <body>
      <h1 id="text">
        <center> Welcome to my Site! </center>
      </h1>
      <div class="moon"></div>
      <div class="stars"></div>
      <div class="twinkling"></div>
      <div class="maintext">
        <h2>This is text below the animations</h2>
      </div>
      <script type="text/javascript">
        setTimeout(function() {
          $('.moon').css("animation-play-state", "paused");
        }, 20000)
      </script>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2017-05-17
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 2014-07-19
      相关资源
      最近更新 更多