【问题标题】:Change font color javascript更改字体颜色 javascript
【发布时间】:2015-01-02 05:00:02
【问题描述】:

我有这个代码。

$(window).bind("scroll", function() {
    if ($(this).scrollTop() > a.top -80) {
        $(".introHeader").fadeIn();
    } else {
        $(".introHeader").stop().fadeOut();
    }

现在我想更改名为:introHeader 的 div 中文本的字体颜色。 所以:

$(window).bind("scroll", function() {
    if ($(this).scrollTop() > a.top -80) {
        $(".introHeader").fadeIn();
        // Change font color.
    } else {
        $(".introHeader").stop().fadeOut();

我该如何解决这个问题,

    $(window).bind("scroll", function() {
    if ($(this).scrollTop() > a.top -80) {
        $(".introHeader").fadeIn();
        document.getElementById('introHeader').color = '#999999';
    } else {
        $(".introHeader").stop().fadeOut();

不工作..

【问题讨论】:

    标签: javascript fonts


    【解决方案1】:

    这应该可以工作

        if ($(this).scrollTop() > a.top -80) {
            $(".introHeader").fadeIn();
            // Change font color.
            $(".introHeader").attr('style', 'color: #999999;');
        } else {
           ...
        }
    

    应该注意有更好的方法来做到这一点。使用 CSS 类更好地细化子选择器,然后使用 .addClass('className')

    【讨论】:

    • 谢谢!修好了,有没有办法慢慢改变颜色,用这个方法?
    • 使用 CSS 过渡,结合 .addClass() 方法。这应该可以帮助您入门:css-tricks.com/almanac/properties/t/transition
    • 用这个修复它 if ($(this).scrollTop() > b.top -300) { $(".introMessage").attr('style', 'transition: color 1s ease ; 颜色:#D8D8D8;'); } else { $(".introMessage").attr('style', 'transition: color 1s ease; color: transparent;'); }
    【解决方案2】:
    $(".introHeader").css('color', '#999').fadeIn();
    

    您已经在使用 jquery。还不如利用css方法。

    您的示例无法正常工作,因为颜色不是属性。你必须使用style.color

    document.getElementById('introHeader').style.color = '#999999';
    

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 2013-11-14
      • 2014-01-19
      • 2014-05-03
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多