测试一下博客的代码功能

js 代码
 
  1. var GDnew=function(){}//1  
  2. GDnews.Effect=function(){};//2  
  3. //上面两个方法是为了扩大命名空间  
  4. GDnews.Effect.MoveSpeed=Class.create();  
  5. GDnews.Effect.MoveSpeed.prototype={  
  6.     initialize:function(element) {  
  7.         this.element = $(element);  
  8.         this.options = Object.extend({  
  9.             x:    0,  
  10.             y:    0,  
  11.             mode: 'relative',  
  12.             delay:85,  
  13.             amountX:0,  
  14.             amountY:0  
  15.             }, arguments[1] || {});  
  16.         this.event("beforeSetup");    
  17.         this.options.amountX=Math.abs(this.options.amountX);  
  18.         this.options.amountY=Math.abs(this.options.amountY);  
  19.         this.scroX=0;//目前已经移动的X数目  
  20.         this.scroY=0;//目前已经移动的Y数目  
  21.         // Bug in Opera: Opera returns the "real" position of a static element or  
  22.         // relative element that does not have top/left explicitly set.  
  23.         // ==> Always set top and left for position relative elements in your stylesheets   
  24.         // (to 0 if you do not need them)   
  25.         this.element.makePositioned();  
  26.         this.originalLeft = parseFloat(this.element.getStyle('left') || '0');  
  27.         this.originalTop  = parseFloat(this.element.getStyle('top')  || '0');  
  28.         if(this.options.mode == 'absolute') {  
  29.             // absolute movement, so we need to calc deltaX and deltaY  
  30.             this.options.x = this.options.x - this.originalLeft;  
  31.             this.options.y = this.options.y - this.originalTop;  
  32.         }  
  33.     this.event("afterSetup");  
  34.     this.scroll();    
  35.     },  
  36.     update: function() {  
  37.         this.event("beforeUpdate");   
  38.         var _x=Math.abs(this.options.x);  
  39.         var _y=Math.abs(this.options.y)  
  40.         var amountX=(this.scroX+this.options.amountX)>=_x ?   
  41.                     (_x-this.scroX/*归零*/) : this.options.amountX;  
  42.         var amountY=(this.scroY+this.options.amountX)>=_y ?   
  43.                     (_y-this.scroY/*归零*/) : this.options.amountY;  
  44.         this.scroX+=amountX;//执行X总数累加  
  45.         this.scroY+=amountY;//执行Y总数累加  
  46.         this.element.setStyle({  
  47.             left:Math.round(this.originalLeft+(this.options.x<0 ? -this.scroX : this.scroX)) + 'px',  
  48.             top:Math.round(this.originalTop+(this.options.y<0 ? -this.scroY : this.scroY))  + 'px'  
  49.             });  
  50.         this.event("beforeFinish");  
  51.         if (amountX==0 && amountY==0){//结束  
  52.             this.scroY=0;  
  53.             this.scroX=0;  
  54.             this.cancel();  
  55.             this.event("afterFinish");  
  56.         }  
  57.     },  
  58.     scroll:function(){  
  59.         this.cancel();  
  60.         this.setTimeInt=window.setInterval(this.update.bind(this),this.options.delay);  
  61.     },  
  62.     cancel:function(){  
  63.         if(this.setTimeInt) window.clearInterval(this.setTimeInt);  
  64.         this.setTimeInt=null;  
  65.     },  
  66.     event: function(eventName) {  
  67.         if(this.options[eventName + 'Internal']) this.options[eventName + 'Internal'](this);  
  68.         if(this.options[eventName]) this.options[eventName](this);  
  69.     }  
  70. }  
  71.   
  72. 在这个类的基础上,再写一个实现marquee的类:  
  73. GDnews.w3cMarquee=Class.create();  
  74. /** 
  75. *@param element 目标的marquee标签 
  76. *@ nomouseEvent 默认为false,类似传统marquee上的onMouseover=this.stop()系列行为 
  77. *此方法在IE中不执行。思路是把marquee删除,用一个div代替它,然后设置此div的left和top来实现滚动。 
  78. */  
  79.   
  80. GDnews.w3cMarquee.prototype={//目前仅实现behavior=scroll  
  81.     initialize:function(element,nomouseEvent){  
  82.         if(/MSIE/.test(navigator.userAgent)){//如果是IE,留着真marquee  
  83.             if(nomouseEvent==truereturn;  
  84.             element=$(element);  
  85.             Event.observe(element,'mouseover',function(){element.stop()});  
  86.             Event.observe(element,'mouseout',function(){element.start()});  
  87.             return;  
  88.         }  
  89.         this.marquee=$(element);  
  90.         if("marquee"!=this.marquee.tagName.toLowerCase()) return;  
  91.           
  92.         this._createDiv();//创建DIV  
  93.         this.makePosition();//设置位置  
  94.           
  95.         var _x=this.scrollWidth;  
  96.         var _y=this.scrollHeight;  
  97.         var newElementId=this.newElementId;  
  98.         var options={x:_x,y:_y,delay:this.scrollDelay,  
  99.             amountX:this.scrollAmount,amountY:this.scrollAmount,  
  100.             beforeSetup:function(effect){  
  101.                 if(nomouseEvent==truereturn;  
  102.                 var cancel=effect.cancel.bind(effect);  
  103.                 var scroll=effect.scroll.bind(effect);  
  104.                 Event.stopObserving(newElementId,'mouseover',cancel);  
  105.                 Event.stopObserving(newElementId,'mouseout',scroll);  
  106.                 Event.observe(newElementId,'mouseover',cancel);  
  107.                 Event.observe(newElementId,'mouseout',scroll);  
  108.             },  
  109.             beforeUpdate:function(effect){  
  110. //              if(behavior=='alternate'){//本想实现'alternate'行为,但由于时间关系,我就把它先留着。  
  111. //                  var left=parseInt(effect.element.getStyle('left'));  
  112. //                  var top=parseInt(effect.element.getStyle('top'));  
  113. //                  see(left+","+top);  
  114. //                  if(effect.scroX>=_parentWidth || effect.scroY>_parentHeight){  
  115. //                      effect.options.x=-1*effect.options.x;  
  116. //                      effect.options.y=-1*effect.options.y;  
  117. //                      effect.cancel();  
  118. //                      window.setTimeout(effect.scroll.bind(effect),100);  
  119. //                  }  
  120. //              }  
  121.             },  
  122.             afterFinish:function(effect){  
  123.                 /*if(behavior=='scroll'){*/  
  124.                     effect.element.setStyle({left:effect.originalLeft,top:effect.originalTop});  
  125.                     window.setTimeout(effect.scroll.bind(effect),100);  
  126.                 /*}*/  
  127.             }  
  128.         };  
  129.         new GDnews.Effect.MoveSpeed(this.scrollId,options);  
  130.     },  
  131.     _createDiv:function(){  
  132.         this.newElementId=(this.marquee.id || "_marquee")+"_"+(new Date().getTime());  
  133.         var _attribute=$A(this.marquee.attributes);  
  134.         this.divEl=document.createElement('div');  
  135.         this.divEl.id=this.newElementId;  
  136.           
  137.         this.scrollId=(this.marquee.id || "_marquee")+"_child_"+(new Date().getTime());  
  138.         this.scrollEl=document.createElement('div');  
  139.         this.scrollEl.id=this.scrollId;  
  140.         try{  
  141.             this.marquee.parentNode.insertBefore(this.divEl,this.marquee);//新建一个node  
  142.             this.divEl.appendChild(this.scrollEl);//一个用于滚动的node  
  143.               
  144.             _attribute.each(function(d,i){//把其它属性放回去  
  145.                     var key=d.nodeName.toLowerCase();  
  146.                     var nodeValue=d.nodeValue;  
  147.                       
  148.                     if(typeof nodeValue=='object' || typeof nodeValue=='function'  
  149.                         || nodeValue=='' || nodeValue==null  || key=='truespeed' || key=='id') return;  
  150.   
  151.                     switch (key){  
  152.                         case 'direction':  
  153.                             this.direction=nodeValue;  
  154.                             return;  
  155.                             break;  
  156.                         case 'scrolldelay':  
  157.                             this.scrollDelay=parseInt(nodeValue);  
  158.                             return;  
  159.                             break;  
  160.                         case 'scrollamount':  
  161.                             this.scrollAmount=parseInt(nodeValue);  
  162.                             return;  
  163.                             break;  
  164.                         case 'behavior':  
  165.                             this.behavior=nodeValue;  
  166.                             return;  
  167.                             break;  
  168.                         case 'loop':  
  169.                             this.loop=parseInt(nodeValue);  
  170.                             return;  
  171.                             break;  
  172.                     }  
  173.                                               
  174.                     this.divEl.setAttribute(key,nodeValue);//IE中只有className而没有class  
  175.                 }.bind(this));  
  176.               
  177.             $A(this.marquee.childNodes).each(function(d){//把子元素放回去  
  178.                                     if(d.length!=1/*这是个长为1的textNode*/this.scrollEl.appendChild(d);  
  179.                                 }.bind(this));  
  180.             this.selfWidth=parseInt($(this.scrollEl).offsetWidth);  
  181.             this.selfHeight=parseInt($(this.scrollEl).offsetHeight);  
  182.               
  183.         }catch(e){  
  184.             throw e;  
  185.         }  
  186.         Element.remove(this.marquee);//结束真marquee的使命  
  187.     },  
  188.     makePosition:function(){  
  189.         $(this.newElementId).makeClipping();  
  190.         var scrollEl=$(this.scrollEl);  
  191.         scrollEl.makePositioned();  
  192.           
  193.         this.originalLeft = parseFloat(this.scrollEl.getStyle('left') || '0');  
  194.         this.originalTop  = parseFloat(this.scrollEl.getStyle('top')  || '0');  
  195.           
  196.           
  197.         this.loop = this.loop || -1;  
  198.         this.scrollDelay  = this.scrollDelay || 85;  
  199.         this.scrollAmount = this.scrollAmount || 6;  
  200.         this.direction    = this.direction || 'left';  
  201.         this.behavior     = this.behavior  || 'scroll';  
  202.         if(window.opera) {//fix opera。Opera对marquee标签有不同的解释  
  203.             switch (this.direction){  
  204.                 case '128':  
  205.                     this.direction='left';  
  206.                     break;  
  207.                 case '129':  
  208.                     this.direction='right';  
  209.                     break;  
  210.                 case '164':  
  211.                     this.direction='up';  
  212.                     break;  
  213.                 case '165':  
  214.                     this.direction='down';  
  215.                     break;  
  216.             }  
  217.             if(this.behavior='137') this.behavior='scroll';  
  218.             if(this.behavior='138') this.behavior='alternate';  
  219.             else this.behavior='slide';  
  220.         }  
  221.         this.behavior     = this.behavior || 'scroll';  
  222.         this.parentWidth  = parseInt(this.divEl.offsetWidth);  
  223.         this.parentHeight = parseInt(this.divEl.offsetHeight)  
  224.         this.scrollWidth  = this.parentWidth+this.selfWidth;  
  225.         this.scrollHeight = this.parentHeight+this.selfHeight;  
  226.           
  227.         var topLeft={};  
  228.         switch (this.direction){  
  229.             case 'up':  
  230.                 this.scrollWidth=0;  
  231.                 this.scrollHeight=-this.scrollHeight;  
  232.                 topLeft={top:this.parentHeight+"px",left:this.originalLeft+"px"};  
  233.                 break;  
  234.             case 'down':  
  235.                 this.scrollWidth=0;  
  236.                 topLeft={top:(-this.selfHeight-this.originalTop)+"px",left:this.originalLeft+"px"};  
  237.                 break;  
  238.             case 'right':  
  239.                 this.scrollHeight=0;  
  240.                 topLeft={top:this.originalTop+"px" ,left:-this.selfWidth+"px"};  
  241.                 break;  
  242.             default:  
  243.                 this.scrollHeight=0;  
  244.                 this.scrollWidth=-this.scrollWidth;  
  245.                 topLeft={top:this.originalTop+"px",left:this.parentWidth+"px"};   
  246.         }  
  247.         scrollEl.setStyle(topLeft);  
  248.         scrollEl.show();  
  249.     }  
  250. }  
  251.   
  252. GDnews.w3cMarquee.initAll=function(){  
  253.     var marquees=$A(document.getElementsByTagName('marquee'));  
  254.     marquees.each(function(d){new GDnews.w3cMarquee(d);});  
  255. }  
感觉不错!

你可能感兴趣的:(IE,prototype,Opera,UP)