2019獨角獸企業重金招聘Python工程師標準>>>
// 點擊對應的nav里的li標簽,頁面就滾動到哪里 $('.title-list > li').click(function(event) {$(this).addClass('active').siblings().removeClass('active'); //li標簽里面有a標簽,可以阻止到a標簽的默認行為 event.preventDefault(); //這里找到的是target #后面的內容 var target = $(this).find('a').prop('hash'); //將頁面動畫滾動到指定位置 var scroll_top = $("#ueditor_0").contents().find("body").find(target).offset(); var s = scroll_top.top; $('html, body').animate({scrollTop: s}, 500); }); //頁面滾動到哪,對應的nav里的li就高亮 $(window).on('scroll',function(){ //獲取頁面滾動高度 var pageScrollTop = $('html,body').scrollTop(); $('.title-list > li ').each(function(index,ele){if(index == $('.title-list > li ').length - 1) { return; }var target = $(this).find('a').prop('hash'); if($(target).offset().top - pageScrollTop < 0) {$(this).parent('li').siblings().removeClass('active'); $(this).parent('li').addClass('active'); }; }); });