美文网首页
滚动页面时DIV到达顶部时固定在顶部

滚动页面时DIV到达顶部时固定在顶部

作者: 汉木棉花 | 来源:发表于2020-03-23 09:08 被阅读0次

    var ie6 = document.all;

    var dv = $('#fixedMenu_keleyi_com'), 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' });

    });

    相关文章

      网友评论

          本文标题:滚动页面时DIV到达顶部时固定在顶部

          本文链接:https://www.haomeiwen.com/subject/ccsiyhtx.html