Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

javascript - textMessage.getBoundingClientRect is not a function

I'm creating some text animation using css and javascript. I have two sections that share the same classes. I'm selecting the classes in javascript using querySelectorAll and then using forEach to apply my if statement to each section of the two sections that share the same classes. However I'm getting the console error textMessage.getBoundingClientRect is not a function.

I'm not sure what I'm doing wrong. Can anyone shed any light?

function scrollAppear() {
  const textMessage = document.querySelectorAll('.home-message-text');
  let textPosition = textMessage.getBoundingClientRect().top;
  let screenPosition = window.innerHeight;
  textMessage.forEach(function(textMessage) {
    if (textPosition < screenPosition) {
      textMessage.classList.add('.home-message-text-appear');
    }
  });

}
.homepage-message {
  display: flex;
  flex-direction: row;
  padding-top: 7rem;
  padding-right: 6rem;
  border-radius: .5rem;
}

.homepage-message-text {
  align-items: center;
  justify-content: center;
  max-width: 70rem;
  padding-top: 10rem;
  opacity: 0;
  transform: translateY(20px);
  transition: 1.5s all ease-in-out;
}

.home-message-text-appear {
  opacity: 1;
  transform: translateY(0px);
}
<html>

<body>
  <section class="homepage-message container">
    <div class="homepage-message-text">
      <h2 class="title">Some Type Of Title One</h2>
      <p class="message">Lorem ipsum dolor sit amet consectetur adipisicing elit. A eligendi ipsum, veniam aliquid assumenda incidunt? Rem perspiciatis eveniet exercitationem velit repellat eaque est ad, vitae animi saepe, dolorem quos officia. Corrupti, accusamus! Maxime
        exercitationem accusamus quibusdam similique minus, recusandae veniam veritatis sequi aliquid labore, ad sit minima! Porro ut officia repudiandae facilis, similique officiis sapiente. Dolorum vitae sint quaerat facere.
      </p>

    </div>


  </section>

  <section class="homepage-message container">
    <div class="homepage-message-text">
      <h2 class="title">Some Type Of Title Two</h2>
      <p class="message">Lorem ipsum dolor sit amet consectetur adipisicing elit. A eligendi ipsum, veniam aliquid assumenda incidunt? Rem perspiciatis eveniet exercitationem velit repellat eaque est ad, vitae animi saepe, dolorem quos officia. Corrupti, accusamus! Maxime
        exercitationem accusamus quibusdam similique minus, recusandae veniam veritatis sequi aliquid labore, ad sit minima! Porro ut officia repudiandae facilis, similique officiis sapiente. Dolorum vitae sint quaerat facere.
      </p>

    </div>


  </section>
</body>

</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

querySelectorAll returns NodeList?which doesn't have getBoundingClientRect method. You should move it inside forEach and call it on each node.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...