這兩天剛學了大圖滾動和Tween算法,感覺學到的不僅僅是效果,更多的是編程的思想。
大圖滾動實際上就是一個moveTo(s,e,callback)的函數來回使用,這個moveTo(s,e,callback)的函數主要作用告訴起點和終點還有scrollLeft++還是top++就可以讓圖片從起始位置運動到終點,根據這個函數就可以寫出:1.點擊第幾張運動到第幾張,點左左運動,自動滾動,都是通過利用這個函數實現的,感覺有點面向對象的味道了。
Tween算法是個很好的算法,有時間一定要好好的深入研究一下。寫運動的時候很多都要用到Tween算法。
大圖滾動還用到了回調函數,回調函數最大的好處就是解耦合了,好像也很有面向對象的思想哦!感覺特別棒!回調函數實際上就是把要執行的語句在單獨寫一個函數作爲參數傳給另一個使用該語句的函數。
回調函數的代碼
- function a(num) {
- //......
- num();
- }
- a(function(){
- alert('ok');
- });
大圖滾動代碼:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title> new document </title>
- <meta name="keywords" content="" />
- <meta name="description" content="" />
- <style>
- div,table,tr,td,li,ul,body{margin:0;padding:0;}
- .outer{height:500px;width:500px;margin:0 auto;position:relative;}
- #demo{height:300px;width:300px;margin:0 auto;overflow:hidden;}
- img{height:300px;width:300px;}
- ul{position:absolute;left:270px;top:250px;}
- li{height:16px;width:16px;background:#fff;margin:2px;float:left;color:#faa;list-style-type:none;text-align:center;line-height:16px;cursor:pointer;}
- #left{position:absolute;left:70px;top:150px;cursor:pointer;}
- #right{position:absolute;left:400px;top:150px;cursor:pointer;}
- .sp{background:red;}
- </style>
- </head>
- <body onload="load();">
- <div class="outer">
- <div id="demo">
- <table cellpadding=0 cellspacing=0>
- <tr>
- <td><img src="1.jpg" alt=""></td>
- <td><img src="2.jpg" alt=""></td>
- <td><img src="3.jpg" alt=""></td>
- <td><img src="4.jpg" alt=""></td>
- </tr>
- </table>
- </div>
- <ul id="demo1">
- <li>1</li>
- <li>2</li>
- <li>3</li>
- <li>4</li>
- </ul>
- <input type="button" value="<" id="left">
- <input type="button" value=">" id="right">
- </div>
- <script type="text/javascript">
- var t1,t2,t3;
- var d=5;
- var showIndex=0;//當前顯示第幾張圖片
- var demo=document.getElementById("demo");
- var demo1=document.getElementById("demo1");
- var lis=demo1.getElementsByTagName("li");
- var left=document.getElementById("left");
- var right=document.getElementById("right");
- //事件監聽器
- function addEventHaddler(target,type,func){
- if(target.addEventListener)
- target.addEventListener(type,func,false);
- else if(target.attachEvent)
- target.attachEvent("on"+type,func);
- else
- target["on"+type]=func;
- }
- function removeEventHaddler(target,type,func){
- if(target.removeEventListener)
- target.removeEventListenner(target,type,func);
- else if(target.detachEvent)
- target.detachEvent("on"+type,func);
- else
- delete target["on"+type];
- }
- var Tween = {
- Quart: {
- easeIn: function(t,b,c,d){
- return c*(t/=d)*t*t*t + b;
- },
- easeOut: function(t,b,c,d){
- return -c * ((t=t/d-1)*t*t*t - 1) + b;
- },
- easeInOut: function(t,b,c,d){
- if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
- return -c/2 * ((t-=2)*t*t*t - 2) + b;
- }
- }
- }
- //運動-終點函數
- function moveTo(s,e,callback1,callback2){
- if(t1){
- clearTimeout(t1);
- clearTimeout(t2);
- }
- var t=0;
- var b=s;
- var c=e-s;
- var x;
- function move(){
- if(t==d){
- callback1(e);
- clearTimeout(t1);
- t2=setTimeout(function(){Toright();},2000);
- }
- else{
- t++;
- x=Tween.Quart.easeOut(t,b,c,d);
- callback2(x);
- t1=setTimeout(move,17);
- }
- }
- move();
- }
- //通用賦值
- function move1(num){
- moveTo(demo.scrollLeft,num*demo.offsetWidth,function (e){demo.scrollLeft = e;},function (s){demo.scrollLeft=s;});
- }
- function bg(a){
- for (var i=0;i<lis.length;i++){
- if(i==a)
- lis[i].className="sp";
- else
- lis[i].className="";
- }
- }
- //加載後運行
- function load(){
- for(var i=0;i<lis.length;i++){
- lis[i].onmouseover=function (){
- if(t2)
- clearTimeout(t2);
- for(var i=0;i<lis.length;i++){
- if(this==lis[i]){
- move1(i);
- showIndex=i;
- bg(i);
- }
- }
- }
- }
- }
- //向右滾動
- function Toright1(){
- if(t3)
- clearTimeout(t3);
- if(t2)
- clearTimeout(t2);
- if(showIndex==lis.length-1)
- showIndex=0;
- else
- showIndex++;
- move1(showIndex);
- bg(showIndex);
- }
- function Toright(){
- t2=setTimeout(function(){Toright1();},2000);
- }
- addEventHaddler(right,"click",function (){
- if(t2)
- clearTimeout(t2);
- Toright1();
- });
- //向左運動
- function Toleft(){
- if(t2)
- clearTimeout(t2);
- if(showIndex==0)
- showIndex=lis.length-1;
- else
- showIndex--;
- move1(showIndex);
- bg(showIndex);
- }
- addEventHaddler(left,"click",function (){Toleft();});
- //自動滾動
- t3=setTimeout(function(){Toright();},2000);
- </script>
- </body>
- </html>