前提・実現したいこと
スクロールした時にテキストと画像が指定した方向からふわっと出てくるアニメーションの練習をしていたのですが、画像だけその場でふわっと出てきてしまいます。理想としては右に寄っている画像は右から、左に寄っている画像は左からふわっと出したいのですが、なぜtransformが効いていないかのような動きになるんでしょうか?
該当のソースコード
HTML
1<main> 2 <div class="container"> 3 <ul> 4 <li class="image-list"><img src="../images/works1.jpg" alt=""> 5 <p class="image-text">テキストテキストテキストテキスト<br>テキストテキストテキストテキスト</p></li> 6 <li class="while"></li> 7 <li class="image-list"><img src="../images/works2.jpg" alt=""> 8 <p class="image-text">テキストテキストテキストテキスト<br>テキストテキストテキストテキスト</p></li></li> 9 <li class="while"></li> 10 <li class="image-list"><img src="../images/works3.jpg" alt=""> 11 <p class="image-text">テキストテキストテキストテキスト<br>テキストテキストテキストテキスト</p></li></li> 12 <li class="while"></li> 13 <li class="image-list"><img src="../images/works4.jpg" alt=""> 14 <p class="image-text">テキストテキストテキストテキスト<br>テキストテキストテキストテキスト</p></li></li> 15 </ul> 16 </div><!-- /container --> 17 </main>
CSS
1main { 2 padding-top: 100vh; 3} 4 5main ul { 6 display: flex; 7 flex-flow: column; 8} 9 10.image-list { 11 position: relative; 12} 13 14.while { 15 height: 300px; 16} 17 18.image-text { 19 position: absolute; 20 top: 20px; 21 right: 0; 22 background: rgb(126, 0, 0, 0.7); 23 padding: 10px; 24 color: white; 25 transform: translateY(20px); 26 transition: 1s; 27 opacity: 0; 28} 29 30.container li:nth-of-type(3) .image-text, .container li:nth-of-type(7) .image-text { 31 left: 0; 32 right: auto; 33} 34 35.container img { 36 display: block; 37 opacity: 0; 38 transition: 1s 1s; 39} 40 41.container li:nth-of-type(1) img, .container li:nth-of-type(5) img { 42 transform: translateX(-20px); 43} 44 45.container li:nth-of-type(3) img, .container li:nth-of-type(7) img { 46 margin-left: auto; 47 transform: translateX(20px); 48} 49 50 51.container .show img, .container .show .image-text { 52 opacity: 1; 53 transform: none; 54}
js
1const imageAnimation = document.querySelectorAll('.image-list'); 2 3document.addEventListener('scroll', function(){ 4 for(let i = 0; i < imageAnimation.length; i++) { 5 const imageDistance = imageAnimation[i].getBoundingClientRect().top + imageAnimation[i].clientHeight *.5 6 7 if(window.innerHeight > imageDistance) { 8 imageAnimation[i].classList.add('show'); 9 } 10} 11});
試したこと
見にくいコードで申し訳ないです。
検証ツールで該当の画像を調べたところ、transformは反映されていました。タイプエラーの表示もありませんでした。
補足情報(FW/ツールのバージョンなど)
dynabook V72/JLE
型番:PV82JLECNRDQE
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/15 11:21