css - 如何动态改变DIV样式

查看:244
本文介绍了css - 如何动态改变DIV样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在一些网站看到导航栏是透明的,当滚动条往下的时候 导航栏就变成了有背景颜色了。(如这个页面的效果:http://demos.creative-tim.com...),思路应该是jquery监听滚动条然后改变class。
但请问具体怎么做的呢?刚学习不太明白,希望有人能指点一下,最好是有例程参考!谢谢!

解决方案

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="jquery.js"></script>
    </head>
    <body>
           <div style="width:100%;height:100px;position:fixed;background: red"></div>
           <div style="width:100%;height:2000px;background:blue;";></div>      
    </div>

    </body>
<script type="text/javascript">
    $(document).scroll(function(){
        if($(document).scrollTop() > 300)
            $('div:first').css('background','white');
        else
            $('div:first').css('background','red');
    })
</script>
</html>

这篇关于css - 如何动态改变DIV样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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