$(document).ready(function () { var box0 = $(".one"),v0 = 1; //这里添加滚动的对象和其速率 var box1 = $(".two"),v1 = 1; rin(box0,v0); rin(box1,v1); function rin($box,v){//$box移动的对象,v对象移动的速率 var $box_ul = $box.find("ul"), $box_li = $box_ul.find("li"), $box_li_span = $box_li.find("span"), left = 0, s=0, timer;//定时器 $box_li.each(function(index){ $($box_li_span[index]).width($(this).width());//hover s += $(this).outerwidth(true); //即要滚动的长度 }) window.requestanimationframe = window.requestanimationframe||function(tmove){return settimeout(tmove,1000/60)}; window.cancelanimationframe = window.cancelanimationframe||cleartimeout; if( s>=$box.width()){//如果滚动长度超出box长度即开始滚动,没有的话就不执行滚动 $box_li.clone(true).appendto($box_ul); tmove(); function tmove(){ //运动是移动left 从0到-s;(个人习惯往左滚) left -= v; if(left <= -s){left = 0;$box_ul.css("left",left)}else{ $box_ul.css("left",left) } timer = requestanimationframe(tmove); } $box_ul.hover(function(){cancelanimationframe(timer)},function(){tmove()}) } } })