參考圖
’
實現原理offset().top
html部分
直接上js
1,定義變量
var section=$(".section");//對應的區塊
var arr=[];//創建一個空數組
var movetop="0"; //做一個true和false操作
var $navBar=$(".navBar");
var navLi=$navBar.find("li"); //操作的元素
for(var i=0;i<section.length;i++){
arr[i]=section.eq(i).offset().top;//這個是距離上端的偏移量(與文檔的上端距離)
}
function scrollMove(elem,m){
elem.each(function(i){
$(this).on("click",function(){
if(movetop=="0"){
movetop=1;
$(this).addClass("active").siblings().removeClass('active');
$("body,html").animate({
scrollTop:section.eq(i).offset().top-82;
},300,function(){
movetop="0";
})
}
})
})
}
scrollMove(navLi,100);