window.onload = function(){ //鎵€鏈夊潡绾у厓绱犵殑楂樺害 var arrHeight = [] //鎵€鏈夊潡绾у厓绱犺窛绂婚《閮ㄧ殑楂樺害 var arrTopHeight = [] //娴忚鍣ㄥ彲瑙侀珮搴 var domHeight = document.documentElement.clientHeight //褰撳墠鑿滃崟鏍忎綅缃 var menuIndex = 0; $(".main .mBox").each(function(index){ arrHeight.push($(".main .mBox").eq(index).height()) arrTopHeight.push($(".main .mBox").eq(index).offset().top) }) console.log(arrHeight,arrTopHeight) function scrollactive(){ var wScroll = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; //鑾峰彇绐楀彛璺濈缃戦〉椤堕儴鐨勯珮搴 var tw = wScroll + Math.abs(domHeight / 2.5) for(i = 0;i arrTopHeight[i] && tw < arrTopHeight[i] + arrHeight[i]) { var lpx = (i * 45) + ((i+1) * 80) - 60 menuIndex = i $("#menu_list i").stop(false).animate({left:lpx+'px'},500) } } } //閿氱偣骞虫粦婊氬姩 $('.menu_list li a').click(function() { $('html, body').animate({ scrollTop: ($($(this).attr('href')).offset().top - 80) + 'px' }, { duration: 400, easing: 'swing' }); return false; }); $('.menu_list li a').hover(function(){ var index = $(this).parent().prevAll().length var lpx = (index * 45) + ((index+1) * 80) - 60 $("#menu_list i").stop(false).animate({left:lpx+'px'},500) },function (){ var lpx = (menuIndex * 45) + ((menuIndex+1) * 80) - 60 $("#menu_list i").stop(false).animate({left:lpx+'px'},500) }) //婊氬姩娴忚鍣ㄨЕ鍙戜簨浠 $(window).scroll(function() { scrollactive(); }); }