MzBehavior.js
上传用户:yatiankong
上传日期:2014-05-16
资源大小:5604k
文件大小:14k
源码类别:

Applet

开发平台:

Java

  1. /*---------------------------------------------------------------------------*
  2. |  Subject:    Html Element behavior base
  3. |  NameSpace:  System.Web.Forms.MzBehavior
  4. |  Author:     meizz
  5. |  Created:    2006-08-05
  6. |  Version:    2006-12-14
  7. |-----------------------------------
  8. |  MSN: huangfr@msn.com QQ:112889082 Copyright (c) meizz
  9. |  http://www.meizz.com/jsframework/ MIT-style license
  10. |  The above copyright notice and this permission notice shall be
  11. |  included in all copies or substantial portions of the Software
  12. *---------------------------------------------------------------------------*/
  13. //Using("System.Web.Forms.MzEffect");
  14. //op{interval, duration, dynamic}
  15. function MzBehavior()
  16. {
  17.   this.element=MzElement.check(arguments[0]);
  18.   if(!this.element) return;
  19.   this.options=System.extend({
  20.     interval: 20,   //milliseconds
  21.     duration: 360,  //milliseconds
  22.     dynamic:  true
  23.   },arguments[1]||{});
  24.   //prevent repeated behavior
  25.   this.attributeName = "att_"+ this._className.replace(/W/g, "_");
  26.   if(t=this.element.getAttribute(this.attributeName)) return;
  27.   this.element.setAttribute(this.attributeName, "meizz", 0);
  28.   if("function"==typeof this.initialize) this.initialize();
  29. }
  30. MzBehavior.Extends(System, "MzBehavior");
  31. //op{color, beginColor, backgroundColor, backgroundImage}
  32. MzBehavior.Highlight=function(obj,op)
  33. {
  34.   op=System.extend({backgroundColor: "#D4D0C8"}, op||{});
  35.   MzBehavior.apply(this, [obj, op]);
  36. };
  37. t=MzBehavior.Highlight.Extends(MzBehavior, "MzBehavior.Highlight");
  38. t.initialize=function()
  39. {
  40.   var me=this, op=me.options, obj=me.element;
  41.   op._color = obj.style.color;
  42.   op._backgroundColor = obj.style.backgroundColor;
  43.   op._backgroundImage = obj.style.backgroundImage; if(!op.beginColor){
  44.   if(System.ie) op.beginColor=(obj.currentStyle||obj.style).backgroundColor;
  45.   else op.beginColor = obj.getCssValue("background-color");
  46.   if(!op.beginColor || op.beginColor=="transparent")op.beginColor="#FFFFFF";}
  47.   op.beginColor = MzEffect.formatColor(op.beginColor);
  48.   this.mouseover=function()
  49.   {
  50.     if(me.outer) return;
  51.     if(op.color) obj.style.color=op.color;
  52.     if(op.dynamic)
  53.     {
  54.       MzEffect.highlight(obj, {
  55.         interval:   op.interval,
  56.         duration:   op.duration,
  57.         beginColor: op.beginColor,
  58.         endColor:   op.backgroundColor,
  59.         finalColor: op.backgroundColor,
  60.         onafterfinish: function(e)
  61.         {
  62.           if(op.color) obj.style.color=op.color;
  63.           if(op.backgroundImage) obj.style.backgroundImage=op.backgroundImage;
  64.         }});
  65.     }
  66.     else
  67.     {
  68.       if(op.color)
  69.       obj.style.color=op.color;if(op.backgroundImage)
  70.       obj.style.backgroundImage = op.backgroundImage;
  71.       obj.style.backgroundColor = op.backgroundColor;
  72.     }
  73.     me.outer=true;
  74.   };
  75.   this.mouseout=function()
  76.   {
  77.     if(me.inner) return; obj.style.color=op._color;
  78.     if(op.dynamic)
  79.     {
  80.       MzEffect.highlight(obj, {
  81.         interval:   op.interval,
  82.         duration:   op.duration,
  83.         beginColor: op.backgroundColor,
  84.         endColor:   op.beginColor,
  85.         finalColor: op.beginColor,
  86.         onafterfinish: function(e)
  87.         {
  88.           obj.style.color=op._color;if(op.backgroundImage)
  89.           obj.style.backgroundImage = op._backgroundImage;
  90.           obj.style.backgroundColor = op._backgroundColor;
  91.         }});
  92.     }
  93.     else
  94.     {
  95.       obj.style.color=op._color;if(op.backgroundImage)
  96.       obj.style.backgroundImage = op._backgroundImage;
  97.       obj.style.backgroundColor = op._backgroundColor;
  98.     }
  99.     me.outer=false;
  100.   };
  101.   this.mouseoverHandler=function()
  102.   {
  103.     clearTimeout(me.timer); me.inner=true;
  104.     setTimeout(me.mouseover, 1);
  105.   };
  106.   this.mouseoutHandler =function()
  107.   {
  108.     me.outer=!(me.inner=false);
  109.     me.timer=setTimeout(me.mouseout, 1);
  110.   };
  111.   this.restore=function()
  112.   {
  113.     this.inner=false; this.mouseout();
  114.     obj.removeAttribute(me.attributeName);
  115.     obj.detachEvent("onmouseover",this.mouseoverHandler);
  116.     obj.detachEvent("onmouseout", this.mouseoutHandler);
  117.   };
  118.   obj.attachEvent("onmouseover", this.mouseoverHandler);
  119.   obj.attachEvent("onmouseout",  this.mouseoutHandler);
  120. };
  121. //op{dark,light,color,backgroundColor,backgroundImage,condition(e)}
  122. MzBehavior.Emboss=function(element, op)
  123. {
  124.   op = System.extend({
  125.     dark: "#808080",
  126.     light:"#F5F5F5",
  127.     borderWidth: 1,
  128.     continual: false,
  129.     condition: function(e){return true;}
  130.   },op||{});
  131.   MzBehavior.apply(this, [element, op]);
  132. };
  133. t=MzBehavior.Emboss.Extends(MzBehavior, "MzBehavior.Emboss");
  134. t.initialize=function()
  135. {
  136.   var me=this, obj=this.element; op=this.options, bw = op.borderWidth;
  137.   var borderStyle = bw==1 ? "solid " : "outset ";
  138.   op._color = obj.style.color;
  139.   op._backgroundColor = obj.style.backgroundColor;
  140.   op._backgroundImage = obj.style.backgroundImage;
  141.   this.especial=false;
  142.   /MSIE (d+(.d+)?)/.test(navigator.userAgent);
  143.   if(System.ie)this.especial=parseFloat(RegExp.$1)<7;
  144.   this.especial = this.especial || window.opera;
  145.   if(this.especial)
  146.   {
  147.     var es = obj.currentStyle || obj.style;
  148.     var PT = ((op._PT=es.paddingTop)   =="auto"?0:parseInt(op._PT))+bw;
  149.     var PL = ((op._PL=es.paddingLeft)  =="auto"?0:parseInt(op._PL))+bw;
  150.     var PR = ((op._PR=es.paddingRight) =="auto"?0:parseInt(op._PR))+bw;
  151.     var PB = ((op._PB=es.paddingBottom)=="auto"?0:parseInt(op._PB))+bw;
  152.     var es = obj.style;
  153.     this._pristine=function()
  154.     {
  155.       es.paddingTop =PT +"px"; es.paddingBottom=PB +"px";
  156.       es.paddingLeft=PL +"px"; es.paddingRight =PR +"px";
  157.       es.border="none";   es.color=op._color;
  158.       es.backgroundColor=op._backgroundColor;
  159.       es.backgroundImage=op._backgroundImage;
  160.     };
  161.     this.mouseover=function()
  162.     {
  163.       if(!op.condition(me)){me._pristine(); return;}
  164.       es.borderTop = es.borderLeft    = borderStyle + bw +"px "+ op.light;
  165.       es.borderRight = es.borderBottom= borderStyle + bw +"px "+ op.dark;
  166.       es.paddingTop=(PT-bw) +"px"; es.paddingBottom=(PB-bw) +"px";
  167.       es.paddingLeft=(PL-bw) +"px"; es.paddingRight=(PR-bw) +"px";
  168.       if(op.color) es.color=op.color;
  169.       if(op.backgroundColor) es.backgroundColor=op.backgroundColor;
  170.       if(op.backgroundImage) es.backgroundImage=op.backgroundImage;
  171.     };
  172.     this.mousedown=function()
  173.     {
  174.       if(!op.condition(me)){me._pristine(); return;}
  175.       es.borderTop = es.borderLeft = "inset "+ bw +"px "+ op.dark;
  176.       es.borderRight=es.borderBottom="inset "+ bw +"px "+ op.light;
  177.       es.paddingTop=(PT-bw) +"px"; es.paddingBottom=(PB-bw) +"px";
  178.       es.paddingLeft=(PL-bw) +"px"; es.paddingRight=(PR-bw) +"px";
  179.       if(op.color) es.color=op.color;
  180.       if(op.backgroundColor) es.backgroundColor=op.backgroundColor;
  181.       if(op.backgroundImage) es.backgroundImage=op.backgroundImage;
  182.     };
  183.   }
  184.   else
  185.   {
  186.     var es=obj.style;
  187.     op._BT=es.borderTop;   op._BL=es.borderLeft;
  188.     op._BR=es.borderRight; op._BB=es.borderBottom;
  189.     es.borderTop = es.borderLeft = "solid "+ bw +"px transparent";
  190.     es.borderRight = es.borderBottom = "solid "+ bw +"px transparent";
  191.     this._pristine=function()
  192.     {
  193.       es.borderTop = es.borderLeft = "solid "+ bw +"px transparent";
  194.       es.borderRight = es.borderBottom = "solid "+ bw +"px transparent";
  195.       es.color=op._color;
  196.       es.backgroundColor=op._backgroundColor;
  197.       es.backgroundImage=op._backgroundImage;
  198.     };
  199.     this.mouseover=function()
  200.     {
  201.       if(!op.condition(me)){me._pristine(); return;}
  202.       es.borderTop = es.borderLeft     = borderStyle + bw +"px "+ op.light;
  203.       es.borderRight = es.borderBottom = borderStyle + bw +"px "+ op.dark;
  204.       if(op.color) es.color=op.color;
  205.       if(op.backgroundColor) es.backgroundColor=op.backgroundColor;
  206.       if(op.backgroundImage) es.backgroundImage=op.backgroundImage;
  207.     };
  208.     this.mousedown=function()
  209.     {
  210.       if(!op.condition(me)){me._pristine(); return;}
  211.       es.borderTop = es.borderLeft = "inset "+ bw +"px "+ op.dark;
  212.       es.borderRight = es.borderBottom = "inset "+ bw +"px "+ op.light;
  213.       if(op.color) es.color=op.color;
  214.       if(op.backgroundColor) es.backgroundColor=op.backgroundColor;
  215.       if(op.backgroundImage) es.backgroundImage=op.backgroundImage;
  216.     };
  217.   }
  218.   this._pristine();
  219.   obj.attachEvent("onmouseover", this.mouseover);
  220.   obj.attachEvent("onmouseout",  this._pristine);
  221.   obj.attachEvent("onmousedown", this.mousedown);
  222.   obj.attachEvent("onclick",     this.mouseover);
  223.   this.restore=function()
  224.   {
  225.     if(this.especial)
  226.     {
  227.       es.paddingTop=op._PT; es.paddingBottom=op._PB;
  228.       es.paddingLeft=op._PL; es.paddingRight=op._PR;
  229.     }
  230.     else
  231.     {
  232.       es.borderTop=op._BT; es.borderBottom=op._BB;
  233.       es.borderLeft=op._BL; es.borderRight=op._BR;
  234.     }
  235.     obj.removeAttribute(me.attributeName);
  236.     obj.detachEvent("onmouseover", this.mouseover);
  237.     obj.detachEvent("onmouseout",  this._pristine);
  238.     obj.detachEvent("onmousedown", this.mousedown);
  239.     obj.detachEvent("onclick",     this.mouseover);
  240.   };
  241. };
  242. //2006-11-29
  243. //op{interval,duration,direction,dynamic,continual,width,height,increased,controls,selectedClassName}
  244. MzBehavior.Rotate=function(element, op)
  245. {
  246.   op = System.extend(
  247.   {
  248.     interval: 3000,
  249.     duration: 2000,
  250.     continual: true,
  251.     direction: "random"
  252.   },op||{});
  253.   MzBehavior.apply(this, [element, op]);
  254. };
  255. MzBehavior.Rotate.Extends(MzBehavior, "MzBehavior.Rotate");
  256. MzBehavior.Rotate.prototype.initialize=function()
  257. {
  258.   var me=this, obj=me.element, op=me.options;
  259.   me.interval = op.duration + op.interval;
  260.   me.nodes = [];
  261.   me.timer = null;
  262.   me.activeIndex = 1;
  263.   me.currentIndex = 0;
  264.   obj.style.position = "relative"; //20061208
  265.   for(var i=0, n=obj.childNodes.length; i<n; i++)
  266.   {
  267.     var node = obj.childNodes[i];
  268.     if (node.nodeType==1 && node.tagName) me.nodes.push(node);
  269.   } if (me.nodes.length<=1) return; n=op.controls;
  270.   for(var i=1; i<me.nodes.length; i++) MzElement.hide(me.nodes[i]);
  271.   if("undefined"!=typeof(n)&& n.length&&"object"==typeof(n[0])&&n[0].tagName)
  272.   {
  273.     System.call(me);
  274.     for(var i=0;i<n.length;i++)if(n[i].tagName)n[i].onmouseover=
  275.     new Function("Instance('"+ me.hashCode +"').focus("+ i +")");
  276.     n[me.currentIndex].className=op.selectedClassName||"selected";
  277.   }
  278.   if(this.options.continual) me.timer=
  279.   setTimeout(function(){me.change();}, me.interval);
  280.   setTimeout(function()
  281.   {
  282.     var w = parseFloat(op.width);
  283.     var h = parseFloat(op.height);
  284.     op.width = op.width ? (isNaN(w) ? "" : w) : "";
  285.     op.height= op.height? (isNaN(h) ? "" : h) : "";
  286.     w=parseFloat(System.ie ? obj.currentStyle.width :obj.getCssValue("width"));
  287.     h=parseFloat(System.ie ? obj.currentStyle.height:obj.getCssValue("height"));
  288.     op.width = op.width || (isNaN(w) ? "" : w);
  289.     op.height= op.height|| (isNaN(h) ? "" : h);
  290.     op.width = op.width || me.nodes[0].offsetWidth;
  291.     op.height= op.height|| me.nodes[0].offsetHeight;
  292.     obj.style.width = op.width +"px";
  293.     obj.style.height= op.height+"px";
  294.     obj.style.overflow = "hidden";
  295.   }, 1);
  296. };
  297. MzBehavior.Rotate.prototype.change=function()
  298. {
  299.   if (this.dispatchEvent(new System.Event("onchange"))) this.mask();
  300.   var me = this, op=me.options, n=op.controls;
  301.   if("undefined"!=typeof(n)&& n.length&&"object"==typeof(n[0])&&n[0].tagName)
  302.   {
  303.     for(var i=0; i<n.length; i++) n[i].className = "";
  304.     n[me.currentIndex].className= op.selectedClassName || "selected";
  305.   }
  306.   if(this.options.continual) this.timer=
  307.   setTimeout(function(){me.change();}, me.interval);
  308. };
  309. MzBehavior.Rotate.prototype.mask=function()
  310. {
  311.   var me=this, L=me.nodes.length, I=me.currentIndex, N=me.activeIndex;
  312.   this.currentIndex = N;  this.activeIndex = N+1>=L ? 0 : N+1;
  313.   if(!this.options.dynamic)
  314.   {
  315.     MzElement.hide.apply(this,this.nodes);
  316.     MzElement.show(this.nodes[N]); return;
  317.   }
  318.   if("boolean"==typeof me.options.increased) var B=me.options.increased; else
  319.   var B  = Math.ceil(Math.random()* 1000) % 2 == 0; //true: strengthen
  320.   var maskIndex = B ? N : I, oldIndex  = B ? I : N;
  321.   MzElement.show(me.nodes[I]);
  322.   var region = me.nodes[maskIndex]; MzElement.show(region);
  323.   var originalWidth = region.style.width || "";
  324.   var originalHeight= region.style.height|| "";
  325.   var ow=region.offsetWidth, oh=region.offsetHeight;
  326.   function mm(s){var n=parseFloat(region.getCssValue(s)); return isNaN(n)?0:n;}
  327.   if(!System.ie) //hack for moz opera
  328.   {
  329.     ow -= mm("padding-left");
  330.     ow -= mm("padding-right");
  331.     ow -= mm("border-left-width");
  332.     ow -= mm("border-right-width");
  333.     oh -= mm("padding-top");
  334.     oh -= mm("padding-bottom");
  335.     oh -= mm("border-top-width");
  336.     oh -= mm("border-bottom-width");
  337.   }
  338.   with(region.style)
  339.   {
  340.     zIndex = 1;
  341.     top = left= "0px";
  342.     position = "absolute";
  343.     width = ow +"px";
  344.     height = oh +"px";
  345.   }
  346.   MzElement.show(me.nodes[oldIndex]);
  347.   this.effect = MzEffect.mask(region, 
  348.   {
  349.     trend: B,
  350.     duration: me.options.duration,
  351.     direction: me.options.direction,
  352.     onafterfinish: function(e)
  353.     {
  354.       if(B) MzElement.hide(me.nodes[oldIndex]);
  355.       with(e.element.style)
  356.       {
  357.         position="";zIndex="";
  358.         top = left = "";
  359.         width = originalWidth;
  360.         height = originalHeight;
  361.       }
  362.     }
  363.   });
  364. };
  365. MzBehavior.Rotate.prototype.focus=function(n)
  366. {
  367.   var L=this.nodes.length,I=this.currentIndex;
  368.   if(n>=L) n=L-1; if(L<=1 || n==I) return;
  369.   MzElement.hide.apply(this, this.nodes);
  370.   clearTimeout(this.timer);
  371.   if(this.effect)this.effect.end();
  372.   this.activeIndex=n<0?0:n;this.change();
  373. };
  374. //2006-11-29
  375. //op{interval, duration, direction}
  376. MzBehavior.Marquee=function(element, op)
  377. {
  378.   op = System.extend(
  379.   {
  380.     interval: 3000,
  381.     duration: 2000,
  382.     direction: "random"
  383.   },op||{});
  384.   MzBehavior.apply(this, [element, op]);
  385. };
  386. MzBehavior.Marquee.Extends(MzBehavior, "MzBehavior.Marquee");
  387. MzBehavior.Marquee.prototype.initialize=function()
  388. {
  389.   var me=this, obj=me.element, op=me.options, a=obj.childNodes;
  390.   obj.style.overflow = "hidden";
  391.   var div = document.createElement("DIV");
  392.   obj.insertBefore(div, obj.firstChild);
  393.   div.style.backgroundColor="green";
  394.   for(var i=a.length-1; i>=0; i--) div.insertBefore(a[i], div.firstChild);
  395. };
  396. //[static method]
  397. MzBehavior.highlight=function(e, op){return new MzBehavior.Highlight(e, op);}
  398. MzBehavior.emboss   =function(e, op){return new MzBehavior.Emboss(e, op);}
  399. MzBehavior.rotate   =function(e, op){return new MzBehavior.Rotate(e, op);}
  400. MzBehavior.marquee  =function(e, op){return new MzBehavior.Marquee(e, op);}