css 如何在JavaScript中触发父级和所有子级的动画?[已关闭]

v8wbuo2f  于 2023-08-09  发布在  Java
关注(0)|答案(1)|浏览(126)

已关闭。此问题需要details or clarity。它目前不接受回答。
**希望改进此问题?**通过editing this post添加详细信息并阐明问题。

4天前关闭。
此帖子是在3天前编辑并提交审查的。
Improve this question
我对HTML和CSS非常陌生。我的HTML中有一个块引号,其中包含不同span类中的单词,如下所示:

<div class="quote" id="quote">
  <blockquote> 
      <p>       
      <span class="left-quote">“</span>
      <span>This</span>
      <span>is</span>
      <span>my</span>
      <span>quote</span>
      <span class="right-quote">”</span>
      </p>
  </blockquote>
</div>

字符串
然后,我在CSS中对引用应用了动画,以便文本慢慢淡入。下面是我的CSS:

* {
  padding: 0;
  margin: 0;
}

body {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.quote p {
  font-family: "Montserrat Medium";
  max-width: 40ch;
  text-align: center;
  transform: scale(0.94);
  animation: scale 3s forwards cubic-bezier(0.5, 1, 0.89, 1);
}

@keyframes scale {
  100% {
    transform: scale(1);
  }
}

span {
  display: inline-block;
  opacity: 0;
  filter: blur(4px);
}

span:nth-child(1) {
  animation: fade-in 0.8s 0.1s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

span:nth-child(2) {
  animation: fade-in 0.8s 0.2s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

span:nth-child(3) {
  animation: fade-in 0.8s 0.3s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

span:nth-child(4) {
  animation: fade-in 0.8s 0.4s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

span:nth-child(5) {
  animation: fade-in 0.8s 0.5s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

span:nth-child(6) {
  animation: fade-in 0.8s 0.6s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

@keyframes fade-in {
  100% {
    opacity: 1;
    filter: blur(0);
  }
}


我现在要做的是,当用户滚动到页面中div处于活动状态的部分时,生成这个动画。
我使用以下方法来实现这一点:

const element = document.getElementById('quote');
const observer = new IntersectionObserver(entries => {
element.classList.toggle('quote', entries[0].isIntersecting );
});
observer.observe(element)


然而,这只会触发报价上的动画,我相信是这一部分:

.quote p {
  font-family: "Montserrat Medium";
  max-width: 40ch;
  text-align: center;
  transform: scale(0.94);
  animation: scale 3s forwards cubic-bezier(0.5, 1, 0.89, 1);
}


我怎样才能让动画在跨度和每个单词上工作?
我试过使用这些,但都没有成功:

const element = document.getElementById('quote');
const observer = new IntersectionObserver(entries => {
element.classList.toggle('quote span', entries[0].isIntersecting );
});
observer.observe(element)


还有这个

const element = document.getElementById('quote');
const observer = new IntersectionObserver(entries => {
element.classList.toggle('quote', 'span', entries[0].isIntersecting );
});
observer.observe(element)

* {
  padding: 0;
  margin: 0;
}

body {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.quote p {
  font-family: "Montserrat Medium";
  max-width: 40ch;
  text-align: center;
  transform: scale(0.94);
  animation: scale 3s forwards cubic-bezier(0.5, 1, 0.89, 1);
}
@keyframes scale {
  100% {
    transform: scale(1);
  }
}

span {
  display: inline-block;
  opacity: 0;
  filter: blur(4px);
}

span:nth-child(1) {
  animation: fade-in 0.8s 0.1s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

span:nth-child(2) {
  animation: fade-in 0.8s 0.2s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

span:nth-child(3) {
  animation: fade-in 0.8s 0.3s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

span:nth-child(4) {
  animation: fade-in 0.8s 0.4s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

span:nth-child(5) {
  animation: fade-in 0.8s 0.5s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

span:nth-child(6) {
  animation: fade-in 0.8s 0.6s forwards cubic-bezier(0.11, 0, 0.5, 0);
}

@keyframes fade-in {
  100% {
    opacity: 1;
    filter: blur(0);
  }
}
<!DOCTYPE html>
<html lang="en">

<!-- Head -->
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Cedarville+Cursive&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="blur.css">
</head>

<!-- Body -->
<body>

    <div class="quote" id="quote">
        <blockquote>
          <p>
            <span class="left-quote">“</span>
            <span>This</span>
            <span>is</span>
            <span>my</span>
            <span>quote</span>
            <span class="right-quote">”</span>      
          </p>
        </blockquote>
      </div>

</body>
</html>
2exbekwf

2exbekwf1#

因为我不知道你的HTML看起来怎么样。我必须对输出的外观进行有根据的猜测。

const quoteElements = document.querySelectorAll('.quote');
quoteElements.forEach((element) => {
  const childBlockquotes = element.querySelectorAll('blockquote');
  childBlockquotes.forEach((blockquote, index) => {
    const observer = new IntersectionObserver(entries => {
      if (entries[0].isIntersecting) {
        blockquote.classList.add('fade-in-quote');
      } else {
        blockquote.classList.remove('fade-in-quote');
      }
    }, {
      root: element,
      threshold: 0.1  // Adjust as needed
    });
    observer.observe(blockquote);
  });
});
.quote {
    height:100vh;
    overflow:auto;
  }
  
  .quote p {
    transform: scale(0.94);
    animation: scale 3s forwards cubic-bezier(0.5, 1, 0.89, 1);
  }
  
  @keyframes scale {
    100% {
      transform: scale(1);
    }
  }
  
  .quote span, .quote blockquote {
    display: inline-block;
    opacity: 0;
    filter: blur(4px);
    min-height:30vh;
    width:100%;
  }
  .fade-in-quote {
    animation: fade-in-quote 0.8s 0.1s forwards cubic-bezier(0.11, 0, 0.5, 0);
  }
  
  @keyframes fade-in-quote {
    100% {
      opacity: 1;
      filter: blur(0);
    }
  }
  
  blockquote {
    text-align: center;
    font-size: 16px;
    font-style: italic;
    padding: 2vmax;
    font-family: 'Poppins', sans-serif;  
    border-top-left-radius: 20px 20px;
    border-top-right-radius: 20px 20px;
    border-bottom-left-radius: 20px 20px;
    border-bottom-right-radius: 20px 20px;
    margin-top: 5px;
    margin-bottom: 5px;
  }
  
  .left-quote, .right-quote { 
    font-family: Georgia, Times, serif;
  }
<div class="quote">
    <blockquote>Quote 1</blockquote>
    <blockquote>Quote 2</blockquote>
    <blockquote>Quote 3</blockquote>
    <blockquote>Quote 4</blockquote>
    <blockquote>Quote 5</blockquote>
    <blockquote>Quote 6</blockquote>
    <blockquote>Quote 7</blockquote>
    <blockquote>Quote 8</blockquote>
    <blockquote>Quote 9</blockquote>
    <blockquote>Quote 10</blockquote>
    <blockquote>Quote 11</blockquote>
    <blockquote>Quote 12</blockquote>
    <blockquote>Quote 13</blockquote>
    <blockquote>Quote 14</blockquote>
    <blockquote>Quote 15</blockquote>
</div>

如果你有多个引用块,它会查找.quote并迭代这些引用块,然后将相同的引用块应用到页面上的每个.quote
你必须修改它以适合你的HTML,但我希望这个JS能在路上对你有所帮助。祝你今天愉快!

相关问题