前提・実現したいこと
キービジュアル内の真ん中に配置しているテキストが、下スクロールしたらそのまま次のセクションに降りてきて、次のセクションでピタッと配置される、そしてそこから上スクロールで戻るとテキストも一緒に戻り再度キービジュアル内の真ん中に配置されていく…という動きを実装したいです。
下記サイトの様な動きです(ただし下記サイトだと、上スクロールしても要素は元の位置にはこないです)。
https://www.corporate.lasana.co.jp/
PCだとうまくいくのですが、スマホだと上スクロールで戻った時に、テキストがうまく真ん中に配置されず、下にずれて配置されてしまいます。
色々調べて試しているのですが、うまくできずどう書いたらいいか分かりません。
どなたか教えていただけませんでしょうか。
よろしくお願いいたします。
該当のソースコード
HTML
1<div id="kv"></div> 2<section id="section-01"> 3 <div class="section-inner"> 4 <h2>テキスト</h2> 5 </div> 6</section>
CSS
1#section-01 h2{ 2 position: absolute; 3 top: 50%; 4 left: 50%; 5 width: 100%; 6 transform: translate(-50%,-50%); 7} 8 9#section-01 .section-inner{ 10 padding: 345px 0; 11} 12 13#section-01 h2.fixed{ 14 position: fixed; 15 top: 320px; 16} 17 18#section-01 .section-inner.fixed2{ 19 position: relative; 20} 21 22#section-01 h2.fixed2{ 23 position: absolute; 24 top: 220px; 25}
jQuery
1$(function() { 2 var scrollStart = $('#section-01 h2').offset().top-250; 3 var scrollEnd = $('#section-01 .section-inner').offset().top-100; 4 var distance = 0; 5 $(window).scroll(function(){ 6 distance = $(this).scrollTop(); 7 if (scrollStart <= distance) { 8 $('#section-01 h2').addClass('fixed'); 9 } else{ 10 $('#section-01 h2').removeClass('fixed'); 11 } 12 if (scrollEnd <= distance) { 13 $('#section-01 h2').addClass('fixed2'); 14 $('#section-01 .section-inner').addClass('fixed2'); 15 }else{ 16 $('#section-01 h2').removeClass('fixed2'); 17 $('#section-01 .section-inner').removeClass('fixed2'); 18 } 19 }); 20});