$(function() { //新闻滚动 function news_scroll(){ var nb = $("#rollnews"), ul = nb.find('.other'), up = $(".news_btn .up"), down = $(".news_btn .down"), idx = 1, delay = 3000, speed = 1000, len = nb.find("li").length, maxtop = - (len-1)*40; nb.hover(function(){ clearinterval(autoplay); },function(){ autoplay = setinterval(ap,delay); }).trigger('mouseleave'); up.on('click',function(){ clearinterval(autoplay); if(idx == 1){ idx = len; ul.animate({margintop:'+=40px'},speed,function(){ ul.css({margintop:maxtop}); }); }else{ idx --; ul.stop(false,true).animate({margintop:'+=40px'},speed); } autoplay = setinterval(ap,delay); return false; }); down.on('click',function(){ clearinterval(autoplay); if(idx == len){ idx = 1; ul.animate({margintop:'-=40px'},speed,function(){ ul.css({margintop:'0'}); }); }else{ idx++; ul.stop(false,true).animate({margintop:'-=40px'},speed); } autoplay = setinterval(ap,delay); return false; }); function ap(){ idx++; if(idx<=len){ ul.stop().animate({margintop:'-=40px'},speed); }else{ idx = 1; ul.animate({margintop:'-=40px'},speed,function(){ ul.css({margintop:'0'}); }); } }; }; news_scroll();//新闻滚动 });