javascript
1 2 3 4import React from 'react' 5 6const Animation = () => { 7 8 const targetElement = document.querySelectorAll(".animationTarget"); 9 10 document.addEventListener("scroll", function(){ 11 for(let i = 0;i < targetElement.length;i++){ 12 const getElementDistance = targetElement[i].getBoundingClientRect().top + (targetElement[i].clientHeight * 0.6); 13 14 if(window.innerHeight > getElementDistance ){ 15 targetElement[i].classList.add("show"); 16 } 17 18 }}) 19 20 return ( 21 <div> 22 K.U 23 </div> 24 ) 25} 26 27export default Animation;
スクロールされるたびにWindowの高さと、<li>要素の高さを比較してshowクラスを付与し、cssでshowクラスに対し動きを設定しています。
コードとファイル https://github.com/K-U22/cat-cute2
github pages https://k-u22.github.io/cat-cute2/
プログラミングを初めて4か月程度でhtml、css、Javascript、Reactは3日程度で理解が浅いです。
YOUTUBEでjavascriptを使ってアニメーションを作る解説動画があったので、動画ではReactは使ってなかったようなのですが、それを参考にReactを使ってコードを書いていきました。
Animation.jsのコンポーネントで
const targetElement = document.querySelectorAll(".animationTarget")
とし(.animationTargetはArticle.jsコンポーネントのクラスネーム)、一時は要素を取得できていたようなのですが、色々やっていくうちに<li>タグのアニメーションが全く動かなくなってしまいました。
console.log(targetElement)としてコンソールを確認してみると、どうやら要素が取得できていないようです。
かなりの時間調べてみたのですが、一向に解決できる気配がないので質問しました。
知識が浅いのでどの部分を提示すればいいかわからないのでgithubのコードとgithub pagesのurlを貼って置きました。
質問の内容が適切ではなかったら申し訳ないです。
回答2件
あなたの回答
tips
プレビュー