百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> jquery滚动到导航后悬浮停止页面顶部 兼容ie6
分享文章到:

jquery滚动到导航后悬浮停止页面顶部 兼容ie6

发布时间:01/15 来源: 浏览: 关键词:
本文章来给各位同学详细介绍一款jquery滚动到导航后悬浮停止页面顶部 兼容ie6实例代码,有需要了解的朋友可参考。

jquery实现方法

 代码如下

<script type="text/javascript">
$(function(){

if ($.browser.msie && ($.browser.version == "6.0") && !$.support.style) {
var handler,sTop,dTop;
    dTop = $("#fixedNav").offset().top
    $(window).bind('scroll',function(){
        window.clearTimeout(handler);
        handler = window.setTimeout(function(){
            sTop = $(document).scrollTop();
            sTop > dTop ? $("#fixedNav").css("top",sTop - dTop) : $("#fixedNav").css("top",0);
     $("#fixedNav").css({"position":"relative","z-index":"1200"});
        },60);
    });
}else{

 var topMain= $("#fixedNav").offset().top
 $(window).scroll(function(){
  if ($(window).scrollTop()>topMain){
   $("#fixedNav").addClass("nav_scroll");
  }else{
   $("#fixedNav").removeClass("nav_scroll");
  }
 });

 }
});
</script>

css代码

 代码如下

.nav_scroll{ position:fixed;top:0;left:0;z-index:1200; width:100%;}

html

 代码如下

<div id=fixedNav>jquery实现滚动到导航后悬浮停止页面顶部 兼容ie6,这里面放内容</div>


下面附一段js代码

 

 代码如下

<script type="text/javascript">
function htmlScroll()
{
 var top = document.body.scrollTop ||  document.documentElement.scrollTop;
 if(elFix.data_top < top)
 {
  elFix.style.position = 'fixed';
  elFix.style.top = 0;
  elFix.style.left = elFix.data_left;
 }
 else
 {
  elFix.style.position = 'static';
 }
}

function htmlPosition(obj)
{
 var o = obj;
 var t = o.offsetTop;
 var l = o.offsetLeft;
 while(o = o.offsetParent)
 {
  t += o.offsetTop;
  l += o.offsetLeft;
 }
 obj.data_top = t;
 obj.data_left = l;
}

var oldHtmlWidth = document.documentElement.offsetWidth;
window.onresize = function(){
 var newHtmlWidth = document.documentElement.offsetWidth;
 if(oldHtmlWidth == newHtmlWidth)
 {
  return;
 }
 oldHtmlWidth = newHtmlWidth;
 elFix.style.position = 'static';
 htmlPosition(elFix);
 htmlScroll();
}
window.onscroll = htmlScroll;

var elFix = document.getElementById('fixedRight');
htmlPosition(elFix);

</script>

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有4人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板