Ver Mensaje Individual
  #8 (permalink)  
Antiguo 30/09/2009, 06:55
Avatar de ZiTAL
ZiTAL
 
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 2 meses
Puntos: 62
Respuesta: Aporte: zmodal ventana modal

He creado una nueva versión ya que la anterior versión iba muy lenta y le he añadido la funcionalidad de cuando se oculta un objeto se coge su tamaño y se crea un div vacio con el tamaño del objeto a ocultar para que la página no se descuadre.

zmodal.js
Código javascript:
Ver original
  1. /**
  2.  * @author zital
  3.  *
  4.  * zmodal: modal (pseudo-modal) box
  5.  * license: GPL 3.0
  6.  * version: 0.0.2
  7.  * based on subModal: can be downloaded from http://gabrito.com/files/subModal/
  8.  */
  9.  
  10. var zmodal = function()
  11. {
  12.     // public properties
  13.     this.title;
  14.     this.width;
  15.     this.height;
  16.     this.type;
  17.     this.content;
  18.    
  19.     // private properties
  20.     this.browser;
  21.     this.background;
  22.     this.modal;
  23.     this.events;
  24.     this.tags = new Array('embed', 'object');  
  25.  
  26.     // public events
  27.  
  28.     // main event
  29.     this.main = function()
  30.     {
  31.         // hidde this.tags elements
  32.         new recursive(document.body, this.tags, function(node)
  33.             {
  34.                 var div = document.createElement('div');
  35.                 div.style.width = getSize(node, 'width')+"px";
  36.                 div.style.height = getSize(node, 'height')+"px";               
  37.                 div.style.display = 'block';
  38.                 addClass(node, 'zhidden');
  39.                 node.parentNode.insertBefore(div, node);
  40.             }
  41.         );
  42.         // get browser
  43.         this.browser = getBrowser();
  44.         // set new events for web
  45.         setEvents.call(this);
  46.         // set modal window
  47.         setModal.call(this);
  48.     };
  49.    
  50.     this.close = function(t)
  51.     {
  52.         privateClose(t);
  53.     };
  54.    
  55.     // event to redefine, whe close modal do this
  56.     this.onClose = function(){};
  57.  
  58.     // private events  
  59.    
  60.     // set Modal window
  61.     var setModal = function()
  62.     {
  63.         // create main div and add to page
  64.         var div = document.createElement('div');
  65.         div.id = 'zmodal';
  66.         document.body.appendChild(div);
  67.        
  68.         // create background div
  69.         this.background = document.createElement('div');
  70.         this.background.className = 'background';
  71.  
  72.         // create modal div
  73.         this.modal = document.createElement('div');
  74.         this.modal.className = 'modal';
  75.  
  76.         // set modal position
  77.         setPosition.call(this);
  78.  
  79.         // add divs to modal div
  80.         div.appendChild(this.background);
  81.         div.appendChild(this.modal);
  82.  
  83.         // set Modal Content
  84.         setModalContent.call(this, this.type, this.content);
  85.     };
  86.  
  87.     // set Modal position
  88.     var setPosition = function()
  89.     {
  90.         // get Scroll positions
  91.         var scroll = getScroll();
  92.         // get page width
  93.         var width = getPageSize('width');
  94.         // get page height
  95.         var height = getPageSize('height');
  96.         // set background position
  97.         setBackgroundPosition.call(this, this.background, scroll, width, height);
  98.         // set modal position
  99.         setModalPosition.call(this, this.modal, scroll, width, height);
  100.     };
  101.  
  102.     // set background position
  103.     var setBackgroundPosition = function(elem, scroll, width, height)
  104.     {
  105.         // depend of scroll position
  106.         elem.style.left = scroll[0]+"px";
  107.         elem.style.top = scroll[1]+"px";
  108.         // if browser internet explorer
  109.         if(this.browser==='msie')
  110.         {
  111.             elem.style.width = width+"px";
  112.             elem.style.height = height+"px";
  113.         }
  114.         // no ie browser
  115.         else
  116.         {
  117.             elem.style.width = '100%';
  118.             elem.style.height = '100%';
  119.         }
  120.     };
  121.  
  122.     // set Modal position
  123.     var setModalPosition = function(elem, scroll, width, height)
  124.     {
  125.         elem.style.width = this.width+"px";
  126.         elem.style.height = this.height+"px";
  127.         elem.style.left = scroll[0]+(width-this.width)/2+"px";
  128.         elem.style.top = scroll[1]+(height-this.height)/2+"px";
  129.     };
  130.  
  131.     // set modal content
  132.     var setModalContent = function(type, content)
  133.     {
  134.         // add close img
  135.         setCloseImg.call(this);
  136.  
  137.         // modal out div (overflow: hidden)
  138.         var _out = document.createElement('div');
  139.         _out.className = 'out';
  140.         this.modal.appendChild(_out);
  141.  
  142.         // modal in div (overflow: auto)
  143.         var _in = document.createElement('div');
  144.         _in.className = 'in';
  145.         if(this.title!==undefined)
  146.         {
  147.             // set title to out div
  148.             setTitle(_out, this.title);
  149.             var titlediv = getByClass(document.getElementById('zmodal'), 'title');
  150.             // get title Height: is necessary to be added to web before get Height
  151.             var titleHeight = getSize(titlediv, 'height');
  152.             // substract title height to 'in' div
  153.             _in.style.height = (this.height-titleHeight)+"px"; 
  154.         }
  155.         // if no title full height
  156.         else
  157.             _in.style.height = this.height+"px";
  158.        
  159.         // full width
  160.         _in.style.width = this.width+"px";
  161.         // add in div to out div
  162.         _out.appendChild(_in);
  163.  
  164.         // modal content types
  165.         if(type==='html')
  166.             _in.innerHTML = content;
  167.         else if(type==='dom')
  168.             _in.appendChild(content);
  169.         // get DOM ID content and put in the modal, onclose restore div
  170.         else if(type==='id')
  171.             setElementChilds(_in, document.getElementById(content).childNodes);
  172.     };
  173.    
  174.     // set Modal title to out div
  175.     var setTitle = function(elem, title)
  176.     {
  177.         var div = document.createElement('div');
  178.         div.className = 'title';
  179.         var span = document.createElement('span');
  180.         span.appendChild(document.createTextNode(title));
  181.         div.appendChild(span);
  182.         elem.appendChild(div);
  183.     };
  184.  
  185.     // get Scroll info
  186.     var getScroll = function()
  187.     {
  188.         var scroll = [];
  189.         if (self.pageYOffset)
  190.         {
  191.             scroll[0] = self.pageXOffset;
  192.             scroll[1] = self.pageYOffset;
  193.         }
  194.         else if (document.documentElement && document.documentElement.scrollTop)
  195.         {
  196.             scroll[0] = document.documentElement.scrollLeft;
  197.             scroll[1] = document.documentElement.scrollTop;
  198.         }
  199.         else if (document.body)
  200.         {
  201.             scroll[0] = document.body.scrollLeft;
  202.             scroll[1] = document.body.scrollTop;
  203.         }
  204.         return scroll;
  205.     };
  206.  
  207.     // set Events to page
  208.     var setEvents = function()
  209.     {
  210.         var t = this;
  211.         t.events = [];
  212.         // get old Events and save
  213.         t.events[0] = window.onresize;
  214.         t.events[1] = window.onscroll;
  215.        
  216.         var _event = function()
  217.         {
  218.             // konqueror fix
  219.             window.setTimeout(function()
  220.             {
  221.                 setPosition.call(t);               
  222.             }, 1);
  223.         };
  224.  
  225.         window.onresize = function(){new _event();};
  226.         window.onscroll = function(){new _event();};
  227.     };
  228.    
  229.     // restore page old events
  230.     var restoreEvents = function(t)
  231.     {
  232.         window.onresize = t.events[0];
  233.         window.onscroll = t.events[1];
  234.     }
  235.  
  236.     // get page size by type
  237.     var getPageSize = function(type)
  238.     {
  239.         var a;
  240.         if(type==='width')
  241.         {
  242.             if (window.innerWidth!=window.undefined) a = window.innerWidth;
  243.             else if (document.compatMode=='CSS1Compat') a = document.documentElement.clientWidth;
  244.             else if (document.body) a = document.body.clientWidth;
  245.             else a = window.undefined;
  246.         }
  247.         else if(type==='height')
  248.         {
  249.             if (window.innerHeight!=window.undefined) a = window.innerHeight;
  250.             else if (document.compatMode=='CSS1Compat') a = document.documentElement.clientHeight;
  251.             else if (document.body) a = document.body.clientHeight;
  252.             else a = window.undefined;
  253.         }
  254.         return a;
  255.     };
  256.  
  257.     // get Element size in page
  258.     var getSize = function(elem, type)
  259.     {
  260.         if(type==='width')
  261.             return (elem.offsetWidth || elem.style.pixelWidth);
  262.         else if(type==='height')       
  263.             return (elem.offsetHeight || elem.style.pixelHeigth);      
  264.     };
  265.  
  266.     // get browser type
  267.     var getBrowser = function()
  268.     {
  269.         var a;
  270.         if(navigator.appName==='Microsoft Internet Explorer')
  271.             a = 'msie';
  272.         else
  273.             a = 'default';
  274.         return a;
  275.     };
  276.  
  277.     // set close button
  278.     var setCloseImg= function()
  279.     {
  280.         var t = this;
  281.         var div = document.createElement('div');
  282.         div.className = 'close';
  283.         var img = document.createElement('img');
  284.         img.src = 'img/x-trans.png';
  285.         img.title = 'close';
  286.         img.alt = 'close';
  287.        
  288.         // add event
  289.         img.onclick = function()
  290.         {
  291.             privateClose(t);
  292.         };
  293.         // if ie browser fix PNG
  294.         if(t.browser==='msie')
  295.             img = fixPng(img);
  296.         div.appendChild(img);
  297.         t.modal.appendChild(div);
  298.     };
  299.  
  300.     // remove element from page
  301.     var removeElement = function(elem)
  302.     {
  303.         elem.parentNode.removeChild(elem);
  304.     };
  305.  
  306.     // fix png for ie6
  307.     var fixPng = function(img)
  308.     {
  309.         var png = /\.png$/i;
  310.         if(png.test(img.src))
  311.         {
  312.             img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\""+img.src+"\",sizingMethod=\"image\")";
  313.             img.src = 'img/trans.gif';
  314.         }
  315.         return img;
  316.     };
  317.  
  318.     // add childs to element
  319.     var setElementChilds = function(elem, obj)
  320.     {
  321.         var len = obj.length;
  322.         for(var i=0;i<len;i++)
  323.             elem.appendChild(obj[i]);
  324.     };
  325.  
  326.     // onclose private method
  327.     var privateClose = function(t)
  328.     {
  329.         var zmodal = document.getElementById('zmodal');
  330.         // if type ID restore ID content to div
  331.         if(t.type==='id')
  332.         {
  333.             var _in = getByClass(zmodal, 'in');
  334.             setElementChilds(document.getElementById(t.content), _in.childNodes);
  335.         }
  336.         // remove modal
  337.         removeElement(zmodal);
  338.         // restore hidden elements
  339.         new recursive(document.body, t.tags, function(node)
  340.             {
  341.                 node.parentNode.removeChild(node.previousSibling);
  342.                 removeClass(node, 'zhidden');
  343.             }
  344.         );     
  345.         // restore events
  346.         restoreEvents(t);
  347.         // execute public onClose method
  348.         t.onClose();
  349.     };
  350.  
  351.     // get element by class (only returns 1)
  352.     var getByClass = function(elem, _class)
  353.     {
  354.         var childs = elem.childNodes;
  355.         var len = childs.length;
  356.         for(var i=0;i<len;i++)
  357.             if(childs[i].className===_class)
  358.                 return childs[i];
  359.             else if(childs[i].hasChildNodes())
  360.                 var a = getByClass(childs[i], _class);
  361.         return a;
  362.     };
  363.    
  364.     var addClass = function(elem, _class)
  365.     {
  366.         elem.className+= " "+_class;
  367.     };
  368.    
  369.     var removeClass = function(elem, _class)
  370.     {
  371.         var aClass = elem.className.split(' ');
  372.         var len = aClass.length;
  373.         var tmp = '';
  374.         for(var i=0;i<len;i++)
  375.             if(aClass[i]!==_class)
  376.                 tmp+=" "+aClass[i];
  377.         elem.className = tmp;
  378.     };
  379.    
  380.     var recursive = function(e, tags, f)
  381.     {
  382.         for(var node = e.firstChild; node; node = node.nextSibling)
  383.         {
  384.             if(node.nodeType===1)
  385.             {
  386.                 if(in_array(node.nodeName.toLowerCase(), tags))
  387.                     new f(node);
  388.                 else
  389.                     new recursive(node, tags, f);
  390.             }
  391.         }
  392.     };
  393.    
  394.     var in_array = function(text, a)
  395.     {
  396.         for(var i=a.length;i>0;i--)
  397.             if (a[i]===text)
  398.                 return true;
  399.         return false;
  400.     };
  401. };

sigo en el siguiente post
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan

Última edición por ZiTAL; 30/09/2009 a las 08:12