実現したいこと
スクロールすると、H2の線が左から右にのびる
前提
HTML・CSS・JSで実装
発生している問題
「線が左から右にのびる」動きが一斉に行われてしまう。
スクロールで各エリアに画面に入ったときに動くようにしたい。
該当のソースコード
HTML=============================
<section class="p-top__vision"> <h2 class="p-top__vision-title js-scrollAnimation">VISION</h2> <p class="p-top__vision-description">テキストテキストテキスト</p> </section> <section class="p-top__service"> <h2 class="p-top__service-title js-scrollAnimation">SERVICE</h2> <p class="p-top__service-description">テキストテキストテキスト</p> </section>
SCSS=============================
.border { position: relative; width: 100%; } .border:before { content: ''; position: absolute; left: 0; bottom: 0; border-bottom: solid 2px #000; animation: border_anim 2s linear forwards; } @keyframes border_anim { 0% { width: 0%; } 100% { width: 100%; } }
JS=============================
var $target = $('.js-scrollAnimation'); var offset = 100; $(window).on('scroll', function () { var scroll = $(window).scrollTop(); var h = $(window).height(); $target.each(function () { var pos = $(this).offset().top; if (scroll > pos - h + offset) { $(this).addClass('border'); } }) }).trigger('scroll');

回答1件
あなたの回答
tips
プレビュー