("#gotop").hide(); var menutop = $("#menubar").position().top; var t = menutop + 20; if (scrollTop > t) { $("#fixtop").addClass('fixed'); } else if (scrollTop < t) { $("#fixtop").removeClass('fixed'); } }); $("#menubar div[href*=#],.mainkoubei a.more[href*=#],a.videobf[href*=#]").on("click", function () { var a = $(this).attr("href"); $("html,body").animate({ scrollTop: $(a).offset().top - 64 }, 800); return !1 }); $(window).scroll(function () { var top = $(document).scrollTop(); //console.log(top); var menu = $(".barmenu"); var items = $(document).find(".bg_white"); var curId = ""; //定义变量,当前所在的楼层item #id items.each(function () { var m = $(this); //定义变量,获取当前类 var itemsTop = m.position().top; //console.log(itemsTop); //定义变量,获取当前类的top偏移量 var topoff = itemsTop - top; if (topoff < 60) { curId = "#" + m.attr("id"); } else { return false; } }); //给相应的楼层设置cur,取消其他楼层的cur var curLink = menu.find(".selected"); if (curId && curLink.attr("href") != curId) { curLink.removeClass("selected"); menu.find("[href=" + curId + "]").addClass("selected"); } }); });