JQuery绝对定位导航栏

js代码:

$(function () {
    //获取.title的上偏移量
    var startoffset = $(".title").offset().top;
    //滚动时执行
    window.onscroll = function () {
        //获取当前滚动条的纵坐标位置||被卷走的高度
        var stop = document.documentElement.scrollTop || document.body.scrollTop;
        if (stop >= startoffset) {
            $(".title").addClass("title_fixed");
        }
        else {
            $(".title").removeClass("title_fixed");
        }
    }
})

css代码:

.title_fixed {
    position: fixed;
    top: 0;
    z-index: 100;
}

End!

posted @ 2020-01-06 16:56  Wahaha、  阅读(543)  评论(0)    收藏  举报