2016-11-18 5 views
0

スクロールするときに、ヘッダーの色を透明から色に変更する際に問題があります。 これはjQueryを使用することはできません。これは学校の割り当てであり、正規のJSが必要なためです。スクロールする際のヘッダーの色を変更するJavaScript

だから私はこのコード

var header = document.getElementById('header'); 
 
window.onscroll = function() { 
 
    "use strict"; 
 
    if (document.body.scrollTop >= 200) { 
 
     header.classList.add("header-colored"); 
 
     header.classList.remove("header-transparent"); 
 
    } 
 
    else { 
 
     header.classList.add("header-transparent"); 
 
     header.classList.remove("header-colored"); 
 
    } 
 
};
#header { 
 
    position: fixed; 
 
    height: 100px; 
 
    width: 100%; 
 
} 
 

 
#header-colored { 
 
    background-color: black; 
 
    position: fixed; 
 
    height: 100px; 
 
    width: 100%; 
 
}
<header id="header-transparent"> 
 
    <ul id="navbar"> 
 
    <li>home</li> 
 
    <li>business</li> 
 
    <li>technical</li> 
 
    <li>about us</li> 
 
    </ul> 
 
    </header> 
 

 
<header id="header-colored"> 
 
    <ul id="navbar"> 
 
    <li>home</li> 
 
    <li>business</li> 
 
    <li>technical</li> 
 
    <li>about us</li> 
 
    </ul> 
 
    </header>

そして、私は間違っているつもりだところ、私は思ったんだけどを持っています。私はJSの経験がほとんどないので、スクリプトに少なくとも1つのエラーがあると思います。

また、HTMLファイルでこれを行う方法もわかりません。まず、ヘッダーは1つしかありませんでしたが、私はJSが機能するためには少なくとも2つが必要であると考えました(透明で色付き)。

入力がありますか? :)

編集:明確にするには、私がスクロールするとヘッダーの色が変わらないことがうまくいかない。それはちょうど黒です(色付きのものは透明なものの裏に見えます)。

+1

言うためにテストするためのスクロールバーを持っている必要があり、間違った起こっているか、何を働いていない内容を正確に把握するために私たちを必要と? 「* [ask] *」のガイドラインを読んでから、あなたの質問を編集してください。 –

+0

また、あなたの例はちょっとhosh-poshです。ちょうど2つの '

' - タグが追加されています。実際のスクロール可能なページは表示されません。 – junkfoodjunkie

+0

申し訳ありません、これは初めてですここに投稿してください。 私は2つの 'header'タグが悪い考えだと思いました。それを変更します。私は今もスクロール可能なページを持っていません。他にも私が把握しようとしている問題があります。ありがとうtho! – r4tchet

答えて

0

基本的に、以下のように間違いはほとんどありません。

  1. 私はどのCSS にクラス名.header-coloredとしてセレクタを変更
  2. 「ヘッダ透明」 CSSでそれを参照するheaderにヘッダIDを変更し、ヘッダにデフォルトクラスを追加私は透明 背景色
  3. との1つの以上のcss .header-transparentを追加そして最後に、あなたの代わりにのheader.className = 'header-colored'を持つクラスを設定し
  4. #header-coloredとしてIDを指し、

私は1つのヘッダーでそれを行い、もう一方はコメントしました。

ワーキングサンプル:あなたは間違っているかもしれない、これは

var header = document.getElementById('header'); 
 
window.onscroll = function() { 
 
    "use strict"; 
 
    if (document.body.scrollTop >= 200) { 
 
     header.className = 'header-colored'; 
 
    } 
 
    else { 
 
     header.className = 'header-transparent'; 
 
    } 
 
};
#header { 
 
    position: fixed; 
 
    height: 100px; 
 
    width: 100%; 
 
} 
 

 
.header-transparent { 
 
    background-color: transparent; 
 
} 
 

 
.header-colored { 
 
    background-color: black; 
 
}
<header id="header" class="header-transparent"> 
 
    <ul id="navbar"> 
 
    <li>home</li> 
 
    <li>business</li> 
 
    <li>technical</li> 
 
    <li>about us</li> 
 
    </ul> 
 
    </header> 
 

 
<!--<header id="header-colored"> 
 
    <ul id="navbar"> 
 
    <li>home</li> 
 
    <li>business</li> 
 
    <li>technical</li> 
 
    <li>about us</li> 
 
    </ul> 
 
    </header>--> 
 
<div> 
 
    More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content More content 
 
</div>

+0

ありがとう、一人の男、これは不思議に働いた。私はまた、私が間違っていたことに気付きました。 – r4tchet

+0

はい、私はこれを実際にこれを見るためのコンテンツで更新しました – Aruna

+0

私も別の質問がありますが、私はここで少し質問するのは複雑すぎると思います。私はあなたに午後を撃つ方法を見つけることを試みているが、私はそれが可能であるように思わない? – r4tchet

0

var header = document.getElementById('header'); 
 
window.onscroll = function() { 
 
    "use strict"; 
 
    if (document.body.scrollTop >= 200) { 
 
     header.classList.add("header-colored"); 
 
    } 
 
    else { 
 
     header.classList.add("header-transparent"); 
 
    } 
 
};
#header { 
 
    position: fixed; 
 
    height: 100px; 
 
    width: 100%; 
 
} 
 

 
.header-colored { 
 
    background-color: black; 
 
    position: fixed; 
 
    height: 100px; 
 
    width: 100%; 
 
} 
 

 
p { 
 
    max-width: 10em; 
 
    }
<header id="header"> 
 
    <ul id="navbar"> 
 
    <li>home</li> 
 
    <li>business</li> 
 
    <li>technical</li> 
 
    <li>about us</li> 
 
    </ul> 
 
    </header> 
 
<p>1914 translation by H. Rackham 
 

 
"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" 
 
Section 1.10.33 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC 
 

 
"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat." 
 
1914 translation by H. Rackham 
 

 
"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."</p>

まあ、最初のオフ、あなたはID headerでは何も持っていない - あなたは<header>id="header-transparent"を持っている - ので、どちらかgetElementByTagName('header')getElementById('header')を変更、または最初の1でIDを変更します〜header-transparent

第2に、HTMLとCSSの両方にクラスではなくIDを割り当てていますが、クラスにアイテムを追加しようとしています。それは変化する必要があります。

+0

ありがとうございました。 私は 'getElementByTagName'に変更し、IDもクラスに変更しました。私は今、 "window.onscrollで未定義のプロパティ 'add'を読み取ることができないというエラーを受け取ります。あなたはこれを引き起こしていることを知っていますか? – r4tchet

+0

私の更新された回答を参照してください。完全には完了していませんが、クラスを追加します。私はIDを使用して戻った、btw。 – junkfoodjunkie

関連する問題