/*
* @param _startElem:表示触发或点击的元素 jQuery的选择方式 以字符串传入
* @param _endElem:表示滚动到的位置 jQuery的选择方式 以字符串传入
* @param _speed:表示滚动的速度,毫秒单位
*
**/
function anchor(_startElem, _endElem, _speed) {
$(_startElem).click(function() {
jQuery(window).unbind("scroll");
console.log("to offset:"+$(_endElem).offset().top)
$('html,body').animate({
scrollTop: $(_endElem).offset().top
},
_speed);
});
}
使用方法如下:
function initAnchor(){
var initList = ["todays","hot","forenptice","new"];
for(var num in initList){
anchor("#anchor_mod_"+initList[num],"#mod_"+initList[num],1000);
}
}
在你需要锚点导航的地方和需要到达的地方对应加上ID,然后用anchor方法初始化即可