0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【HTML,CSS,JQuery】スクロール時にヘッダーの背景色を変える

Last updated at Posted at 2020-03-19

割とよく使うので忘備録として・・。

##スクロール時にヘッダーの背景色を変える

  1. タブレット画面幅以下の時のみ
  2. ヘッダーが固定
  3. ヘッダー透明でトップビジュアルの上にあるが、スクロールしたら透明は見難いから色をつけたい。
<header>
  <p><a href="/"><img src="pathToLogo" alt="ロゴ"></a></p>
  <button><img src="pathToImg" alt="メニューボタンなど"></button>
</header>

$(function(){
   var breakPoint = 768;
   if ($(window).innerWidth() <= breakPoint) {
     var mainVisualHeight = 420;
     var header = $('header');
     $(window).scroll(function () {
       $(this).scrollTop() > mainVisualHeight ?  header.css('background', '#fff'): header.css('background', 'inherit');
      });
     }
});
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?