滚动时改变标题颜色的JavaScript

时间:2016-11-18 00:04:03

标签: javascript html css

我在滚动时将标题的颜色从透明变为彩色时遇到了一些麻烦。 我不能使用jQuery,因为这是一个学校作业,需要常规的JS。

所以我有这个代码



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>
&#13;
&#13;
&#13;

我想知道我哪里出错了。请记住,我对JS没什么经验,所以我想我的脚本中至少有一个错误。

我也不确定如何在HTML文件中执行此操作。首先,我只有一个标题,但我认为我需要至少两个JS才能工作(透明和彩色)。

有什么输入? :)

编辑:要清楚,滚动时标题不会改变颜色。它只是黑色(有色的一个在透明的背后可见)。

2 个答案:

答案 0 :(得分:0)

&#13;
&#13;
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");
    }
};
&#13;
#header {
    position: fixed;
    height: 100px;
    width: 100%;
}

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

p {
  max-width: 10em;
  }
&#13;
<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>
&#13;
&#13;
&#13;

好吧,首先,你没有身份证header的任何内容 - 你有<header> id="header-transparent" - 所以要么getElementById('header')改为{{} 1}},或将第一个中的ID更改为getElementByTagName('header')

其次,您已经在HTML和CSS中分配了ID,而不是类,但是您正在尝试向类添加内容。这需要改变。

答案 1 :(得分:0)

基本上你没有犯过如下错误。

  1. 我将标头ID更改为<script>,其中引用了相同的标题 css并在标题&#39; header-transparent&#39;
  2. 中添加了默认类
  3. 我将选择器更改为类名header到css中 在
  4. 之前将id引用为.header-colored
  5. 我添加了一个透明的css #header-colored 背景颜色
  6. 最后,使用.header-transparent而不是header.className = 'header-colored'
  7. 设置课程

    我用一个标题做了它,并评论了另一个标题。

    工作示例:您应该有一个滚动条来测试这个

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

相关问题