利用jquery滚动到导航条时固定(fixed)在顶部

利用jquery
<div style="height:500px;background:#999"></div>
<div id="fixedMenu" style="background:#eee;width:100%;">菜单</div>
<div style="height:900px;background:#999"></div>
<script type="text/javascript" src="http://www.niwo8.com/statics/js/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        var ie6 = /msie 6/i.test(navigator.userAgent)
        , dv = $('#fixedMenu'), st;
        dv.attr('otop', dv.offset().top); //存储原来的距离顶部的距离
        $(window).scroll(function () {
            st = Math.max(document.body.scrollTop || document.documentElement.scrollTop);
            if (st >= parseInt(dv.attr('otop'))) {
                if (ie6) {//IE6不支持fixed属性,所以只能靠设置position为absolute和top实现此效果
                    dv.css({ position: 'absolute', top: st });
                }
                else if (dv.css('position') != 'fixed') dv.css({ 'position': 'fixed', top: 0 });
            } else if (dv.css('position') != 'static') dv.css({ 'position': 'static' });
        });
    });
</script>

posted @ 2013-04-22 16:51  白*开*水  阅读(1073)  评论(0编辑  收藏  举报