导航栏滚动到顶部后固定
2014-01-08 10:53
260 查看
<div style="height:500px;background:#999"></div> <div id="fixedMenu" style="background:#eee;width:100%;">MENU</div> <div style="height:1900px;background:#999"></div> <script type="text/javascript" src="http://www.coding123.net/js/jquery.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>
注:http://www.coding123.net/js/jquery.js最好换成本地的js文件:jquery-1.4.2.js,不换的话,当www.coding.net打不开时,引用此域名的网站会出现内容无法加载的情况。
相关文章推荐
- 导航栏滚动到顶部时固定在顶部
- 页面滚动到指定位置导航栏固定顶部
- 导航栏滚动到顶部后固定
- 滚动页面, 顶部导航栏固定效果
- 当导航栏滚动到浏览器顶部时,固定导航栏
- 导航栏滚动到顶部后固定
- JS滚动到指定位置导航栏固定顶部
- jQuery 顶部导航尾随滚动,固定浮动在顶部
- 固定顶部导航栏
- javascript改变position值实现菜单滚动至顶部后固定
- 导航栏固定 和 回到顶部 防抖动兼容ie6
- jQuery 顶部导航跟随滚动条滚动固定浮动在顶部
- Jquery DIV滚动至浏览器顶部位置固定
- html bootstrap 表头固定在顶部,表列 可以自由滚动的效果
- Jquery DIV滚动至浏览器顶部位置固定
- 页面顶部固定漂浮导航栏,漂浮div,div背景透明
- jquery层居中,点击小图查看大图,弹出层居中代码,顶部层固定不动,滚动条滚动情况
- Bootstrap制作 精美渐变顶部固定自适应导航栏
- 滚动页面时DIV到达顶部时固定在顶部
- Jquery DIV滚动至浏览器顶部后固定不动代码