滚动时更改背景颜色 [英] Change background-color while scrolling

查看:611
本文介绍了滚动时更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望标题的背景颜色更改为它滚动经过的div的背景颜色.因此,当滚动到div #about(绿色)时,标题的背景颜色也将变为绿色.到目前为止,我已经知道了,但是没有用.任何帮助,我们将不胜感激.

I would like the background-color of my header to change to background-color of the div it scrolls past. So, when scrolled to the div #about (green), the background-color of my header will also change to green. I have this so far, but it's not working. Any help is greatly appreciated.

var t = $('#about').offset().top - 100;

$(document).scroll(function(){
    if($(this).scrollTop() > t)
    {   
        $('header').css({"background-color":"green"});
    } 
});

查看我的小提琴.

推荐答案

在向其中添加jQuery之后,您的代码就可以很好地发挥作用. (我使用的是1.9.1).

Your code works just fine in your fiddle AFTER you add jQuery to it. (I used 1.9.1).

这篇关于滚动时更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆