ウインドウをスクロールした時にjQueryでheaderをfadeinさせたいのですが、どのようにすればいいでしょうか?
###参考サイト
Olta 参考サイト
###考え方
・予めfadeinさせたいheader-cloneを用意し、そのheader-cloneを既存のheaderの上に乗せる。
・header-cloneにopadity: 0;を指定し見えないようにする。
・CSSでheader-cloneに.activeをつけ、opacity: 1;を指定する。
・スクロールした際addClassでactiveをつけfadeinさせる。
html
html
1<header id="header"> 2 <!-- hrader開始 --> 3 <div class="header-first"> 4 <div class="header-call "> 5 <!-- <img class="logo-clone" src="../images/logo.svg" alt=""> --> 6 <img class="call" src="../images/icon-tel.svg" alt=""> 7 <p>050-3205-1008</p><sapn id="time">(10:00~18:00 土日祝日除く)</span> 8 </div> 9 <div class="global-nav"> 10 <ul> 11 <li>運営会社</li> 12 <li>よくある質問</li> 13 <li>ログイン</li> 14 </ul> 15 </div> 16 </div> 17 </header> 18 <!-- header終了 --> 19 20 <!-- header-clone開始 --> 21 <div class="header-clone"> 22 <div class="header-call-clone"> 23 <img class="logo-clone" src="../images/logo.svg" alt=""> 24 <img class="call-clone" src="../images/icon-tel.svg" alt=""> 25 <p>050-3205-1008</p><sapn id="time">(10:00~18:00 土日祝日除く)</span> 26 </div> 27 28 <div class="global-nav-clone"> 29 <div class="list-4"><a href="">無料登録して申し込む</a></div> 30 <ul> 31 <li>運営会社</li> 32 <li>よくある質問</li> 33 <li>ログイン</li> 34 </ul> 35 </div> 36 </div> 37 <!-- header-clone終了 -->
###CSS
css
1.header-clone { 2 height: 90px; 3 opacity: 0; 4 background-color: red; 5 position: fixed; 6 top: 0px; 7 left: 0px; 8 width: 100%; 9} 10 11.header-clone.active { 12 opacity: 1; 13}
###jQuery
js
1$(function() { 2 3 $('#header').each(function(){ 4 var $window = $('winodow'), 5 $header = $(this) 6 threshold = $header.offset().top, //+ $header.outerHeight(), 7 $clone = $('<div class="header-clone"></div>'); 8 9 $window.on('scroll',$.throttle(1000 / 15, function(){ 10 if($window.scrollTop() > threshold) { 11 $clone.addClass('active').fadeIn(); 12 }else{ 13 $clone.removeClass('active').fadeOut(); 14 } 15 })); 16 $window.trigger('scroll'); 17 }); 18}); 19
試したこと
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/07 10:21