Jq에서 만든 디지털 스크롤 플러그인은 숫자가 변경될 때 동적으로 스크롤하고 전환할 수 있으며 효과가 매우 좋습니다.
먼저 예를 살펴보겠습니다.
CSS
.textC { position:absolute; width:500px; overflow:hidden; margin-top: 100px; line-height:30px; margin-left: 300px; height:30px; } .textC span { color: #13BEEC; font-size: 28px; font-weight: bold; font-family: Georgia, "Times New Roman", Times, serif; position: absolute; }
HTML
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(function(){ NumbersAnimate.Target=$(".textC"); NumbersAnimate.Numbers=12389623; NumbersAnimate.Duration=1500; NumbersAnimate.Animate(); }); var NumbersAnimate={ Target:null, Numbers:0, Duration:500, Animate:function(){ var array=NumbersAnimate.Numbers.toString().split(""); //遍历数组 for(var i=0;i<array.length;i++){ var currentN=array[i]; //数字append进容器 var t=$("<span></span>"); $(t).append("<span class=\"childNumber\">"+array[i]+"</span>"); $(t).css("margin-left",18*i+"px"); $(NumbersAnimate.Target).append(t); //生成滚动数字,根据当前数字大小来定 for(var j=0;j<=currentN;j++){ var tt; if(j==currentN){ tt=$("<span class=\"main\"><span>"+j+"</span></span>"); }else{ tt=$("<span class=\"childNumber\">"+j+"</span>"); } $(t).append(tt); $(tt).css("margin-top",(j+1)*25+"px"); } $(t).animate({marginTop:-((parseInt(currentN)+1)*25)+"px"},NumbersAnimate.Duration,function(){ $(this).find(".childNumber").remove(); }); } }, ChangeNumber:function(numbers){ var oldArray=NumbersAnimate.Numbers.toString().split(""); var newArray=numbers.toString().split(""); for(var i=0;i<oldArray.length;i++){ var o=oldArray[i]; var n=newArray[i]; if(o!=n){ var c=$($(".main")[i]); var num=parseInt($(c).html()); var top=parseInt($($(c).find("span")[0]).css("marginTop").replace('px', '')); for(var j=0;j<=n;j++){ var nn=$("<span>"+j+"</span>"); if(j==n){ nn=$("<span>"+j+"</span>"); }else{ nn=$("<span class=\"yy\">"+j+"</span>"); } $(c).append(nn); $(nn).css("margin-top",(j+1)*25+top+"px"); } var margintop=parseInt($(c).css("marginTop").replace('px', '')); $(c).animate({marginTop:-((parseInt(n)+1)*25)+margintop+"px"},NumbersAnimate.Duration,function(){ $($(this).find("span")[0]).remove(); $(".yy").remove(); }); } } NumbersAnimate.Numbers=numbers; }, RandomNum:function(m,a){ var Range = a - m; var Rand = Math.random(); return(m + Math.round(Rand * Range)); } } </script>
1. 처음 전화할 때
NumbersAnimate.Target=$(".textC"); NumbersAnimate.Numbers=12389623; NumbersAnimate.Duration=1500; NumbersAnimate.Animate();
NumbersAnimate.ChangeNumber();
대상: 숫자의 상위 컨테이너
숫자: 표시되는 숫자
기간: 스크롤 속도(밀리초)