2016-11-18 147 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沒有什麼經驗,所以我想我至少在腳本中有一個錯誤。

我也不知道如何在HTML文件中做到這一點。首先,我只有一個標題,但我想我至少需要兩個JS來工作(透明和彩色)。

任何輸入? :)

編輯:要明確,什麼行不通是,當我滾動頭不變色。它只是黑色(在透明的背後可以看到有色的)。

+1

說來,你可能會錯誤需要我們知道到底是怎麼回事錯的,什麼是不工作?請閱讀「* [問] *」準則,然後編輯您的問題。 –

+0

另外,你的例子有點hosh-posh,因爲它只增加了兩個'

'-tags,並沒有提供給我們一個實際的可滾動頁面,所以我們可以看到發生了什麼,等等。 – junkfoodjunkie

+0

對不起,這是我的第一次張貼在這裏。 我覺得這兩個'header'標籤是一個壞主意。會改變它。我現在沒有可滾動的頁面,因爲我還有其他問題,我也想弄清楚。感謝壽! – r4tchet

回答

0

基本上你已經犯了如下錯誤。

  1. 我改變報頭ID成header其是指在 CSS相同並且添加了一個默認類的報頭「首部的透明」
  2. 我改變選擇作爲類名.header-colored到CSS ,其指的id作爲#header-colored之前
  3. 我增加了一個CSS .header-transparent透明 背景顏色
  4. 最後,用header.className = 'header-colored'而不是你設置類

我做了一個標題,並評論其他。

工作樣本:你應該有一個滾動條來測試這個

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" - 所以要麼改變getElementById('header')getElementByTagName('header'),或改變ID中的第一個到header-transparent

其次,你必須分配的ID,而不是類,無論是在HTML和CSS的,但你要的東西添加到類。這需要改變。

+0

非常感謝。 我更改爲'getElementByTagName',並且我還將ID更改爲類。我現在得到一個錯誤,說「無法讀取屬性'添加'未定義在window.onscroll」。你知道這會造成什麼嗎? – r4tchet

+0

查看我更新的答案。它不完全完整,但增加了類。我回到使用ID,順便說一句。 – junkfoodjunkie