<script>
function getByClassName(obj,cls){
var els = obj.getElementsByTagName('*');
var result = [];
for(var i = 0 ; i<els.length;i++){
if(els[i].className == cls){
result.push(els[i])
}
}
return result;
}
function hasClass(obj,cls){
return obj.className.match(new RegExp("(\\s|^)"+cls+"(\\s|$)"));
}
function removeClass(obj,cls){
if(hasClass(obj,cls)){
var reg = new RegExp("(\\s|^)"+cls+"(\\s|$)");
obj.className = obj.className.replace(reg,'');
}
}
function addClass(obj,cls){
if(!hasClass(obj,cls)){
obj.className += ' '+ cls;
}
}
window.onload = function(){
window.onscroll = function(){
var top = document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop;
var menus = document.getElementById('menu').getElementsByTagName('a');
var items = getByClassName(document.getElementById('content'),'item');
// console.log(items);
var currentId = '';
for(var i =0;i<items.length;i++){
var _item = items[i];
var _top = _item.offsetTop;
console.log(_top);
if(top > _top - 200){
currentId = _item.id;
console.log(currentId)
}else{
break;
}
}
if(currentId ){
for(var j =0 ;j<menus.length;j++){
var _menu = menus[j];
// console.log(_menu);
var _href = _menu.href.split('#');
// console.log(_href);
if(_href[ _href.length - 1] != currentId){
removeClass(_menu,'current');
} else{
addClass(_menu,'current');
}
}
}
}
}
</script>