Foros del Web » Programando para Internet » Javascript »

Aporte: zmodal ventana modal

Estas en el tema de Aporte: zmodal ventana modal en el foro de Javascript en Foros del Web. Aupa siempre he tenido pendiente el poder hacer una ventana modal, pues aquí lo tenéis: http://zital.no-ip.org/probak/zmodal/ El css: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código css: Ver original #zmodal ...
  #1 (permalink)  
Antiguo 16/04/2009, 10:21
Avatar de ZiTAL  
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 2 meses
Puntos: 62
De acuerdo Aporte: zmodal ventana modal (pseudo-modal)

Aupa siempre he tenido pendiente el poder hacer una ventana modal, pues aquí lo tenéis:

http://zital.no-ip.org/probak/zmodal/

El css:
Código css:
Ver original
  1. #zmodal .background
  2. {
  3.     z-index: 10;
  4.     background-color: #000;
  5.     position: absolute;
  6.     opacity: 0.5;
  7.     filter: alpha(opacity="50");
  8. }
  9. #zmodal .modal
  10. {
  11.     z-index: 11;
  12.     background-color: #fff;
  13.     position: absolute;
  14.     height: 100%;
  15.     border: 1px solid black;
  16.     padding: 5px;
  17. }
  18. #zmodal .title
  19. {
  20.     color: #fff;
  21.     background-color: #000;
  22.     overflow: hidden;
  23.     padding: 5px;
  24.     width: 100%;
  25. }
  26. #zmodal .title span
  27. {
  28.     text-align: right;
  29. }
  30. #zmodal .out
  31. {
  32.     width: 100%;
  33.     height: 100%;
  34.     overflow: hidden;
  35. }
  36. #zmodal .in
  37. {
  38.     overflow: auto;
  39. }
  40. #zmodal .close img
  41. {
  42.     position: absolute;
  43.     width: 25px;
  44.     height: 29px;
  45.     padding: 0;
  46.     cursor: pointer;
  47.     display: inline;
  48.     right: -8px;
  49.     top: -8px;
  50.     z-index: 12;
  51. }

y el js:

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

por ahora funciona en IE6, IE7, firefox3, arora, chrome/iron, en konqueror no funciona el onscroll pero por lo demás sí.

Para terminar quisiera darle las gracias a:gabrito.com por su código en el que me he basado.

http://gabrito.com/files/subModal/

DEMO funcionando:

http://zital.no-ip.org/probak/zmodal/
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan

Última edición por ZiTAL; 30/09/2009 a las 08:09 Razón: caricatos tiene razon :)
  #2 (permalink)  
Antiguo 16/04/2009, 10:41
Avatar de Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: Aporte: zmodal ventana modal

Buen aporte. Sólo una de sugerencia: Verificar si existen selects, objets o embeds y ocultarlos durante la apertura de la ventana (si no lo hacés, no aparecerán debajo) y volver a mostrarlos durante el cierre de la misma.
  #3 (permalink)  
Antiguo 17/04/2009, 02:01
Avatar de 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

tienes razón muchas gracias, ahora me pongo a ello ;)
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan
  #4 (permalink)  
Antiguo 17/04/2009, 03:04
Avatar de 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 añadido nueva propiedad publica:
Código javascript:
Ver original
  1. this.hiddeTags = new Array('object', 'embed', 'select');

y 3 metodos privado:
Código javascript:
Ver original
  1. var addClass = function(elem, _class)
  2.     {
  3.         elem.className+= " "+_class;
  4.     };
  5.     var removeClass = function(elem, _class)
  6.     {
  7.         var aClass = elem.className.split(' ');
  8.         var len = aClass.length;
  9.         var tmp = '';
  10.         for(var i=0;i<len;i++)
  11.             if(aClass[i]!==_class)
  12.                 tmp+=" "+aClass[i];
  13.         elem.className = tmp;
  14.     };
  15.     var setClassByTags = function(_parent, aTags, _class, _type)
  16.     {
  17.         var childs = _parent.childNodes;
  18.         var clen = childs.length;
  19.         var tlen = aTags.length;
  20.         for(var i=0;i<clen;i++)
  21.         {
  22.             for(var j=0;j<tlen;j++)
  23.             {
  24.                 if(childs[i].nodeName.toLowerCase()===aTags[j])
  25.                 {
  26.                     if(_type==='add')
  27.                         addClass(childs[i], _class);
  28.                     else if(_type==='remove')
  29.                         removeClass(childs[i], _class);
  30.                 }
  31.                 if(childs[i].hasChildNodes())
  32.                     setClassByTags(childs[i], aTags, _class, _type);
  33.             }
  34.         }      
  35.     };

luego al llamar al metodo publico main, ocultamos los elementos añadiendo la clase zhidden:

Código javascript:
Ver original
  1. // hidde elements
  2.         setClassByTags(document.body, this.hiddeTags, 'zhidden', 'add');

y luego en el metodo privado privateClose lo restauramos:

Código javascript:
Ver original
  1. setClassByTags(document.body, t.hiddeTags, 'zhidden', 'remove');

añadimos zhidden al css:

Código css:
Ver original
  1. .zhidden
  2. {
  3.     display: none;
  4. }

En cuanto pueda lo subo a la demo ;)
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan
  #5 (permalink)  
Antiguo 17/04/2009, 03:08
Avatar de 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

No me entraba en el post anterior lo pongo aquí:
zmodal.css
Código css:
Ver original
  1. #zmodal .background
  2. {
  3.     z-index: 10;
  4.     background-color: #000;
  5.     position: absolute;
  6.     opacity: 0.5;
  7.     filter: alpha(opacity="50");
  8. }
  9. #zmodal .modal
  10. {
  11.     z-index: 11;
  12.     background-color: #fff;
  13.     position: absolute;
  14.     height: 100&#37;;
  15.     border: 1px solid black;
  16.     padding: 5px;
  17. }
  18. #zmodal .title
  19. {
  20.     color: #fff;
  21.     background-color: #000;
  22.     overflow: hidden;
  23.     padding: 5px;
  24.     width: 100%;
  25. }
  26. #zmodal .title span
  27. {
  28.     text-align: right;
  29. }
  30. #zmodal .out
  31. {
  32.     width: 100%;
  33.     height: 100%;
  34.     overflow: hidden;
  35. }
  36. #zmodal .in
  37. {
  38.     overflow: auto;
  39. }
  40. #zmodal .close img
  41. {
  42.     position: absolute;
  43.     width: 25px;
  44.     height: 29px;
  45.     padding: 0;
  46.     cursor: pointer;
  47.     display: inline;
  48.     right: -8px;
  49.     top: -8px;
  50.     z-index: 12;
  51. }
  52. .zhidden
  53. {
  54.     display: none;
  55. }
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan
  #6 (permalink)  
Antiguo 17/04/2009, 03:09
Avatar de 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

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

Euskerie ahuen eta bijotzan
  #7 (permalink)  
Antiguo 17/04/2009, 10:43
Avatar de 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

Añadido soporte total para konqueror ;)

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

Euskerie ahuen eta bijotzan
  #8 (permalink)  
Antiguo 30/09/2009, 06:55
Avatar de 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
  #9 (permalink)  
Antiguo 30/09/2009, 06:57
Avatar de 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

zmodal.css
Código css:
Ver original
  1. #zmodal .background
  2. {
  3.     z-index: 10;
  4.     background-color: #000;
  5.     position: absolute;
  6.     opacity: 0.5;
  7.     filter: alpha(opacity="50");
  8. }
  9. #zmodal .modal
  10. {
  11.     z-index: 11;
  12.     background-color: #fff;
  13.     position: absolute;
  14.     height: 100&#37;;
  15.     border: 1px solid black;
  16.     padding: 5px;
  17. }
  18. #zmodal .title
  19. {
  20.     color: #fff;
  21.     background-color: #000;
  22.     overflow: hidden;
  23.     padding: 5px;
  24.     width: 100%;
  25. }
  26. #zmodal .title span
  27. {
  28.     text-align: right;
  29. }
  30. #zmodal .out
  31. {
  32.     width: 100%;
  33.     height: 100%;
  34.     overflow: hidden;
  35. }
  36. #zmodal .in
  37. {
  38.     overflow: auto;
  39. }
  40. #zmodal .close img
  41. {
  42.     position: absolute;
  43.     width: 25px;
  44.     height: 29px;
  45.     padding: 0;
  46.     cursor: pointer;
  47.     display: inline;
  48.     right: -8px;
  49.     top: -8px;
  50.     z-index: 12;
  51. }
  52.  
  53. .zhidden
  54. {
  55.     display: none;
  56. }

ejemplo de uso:

http://zital.no-ip.org/probak/zmodal
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan

Última edición por ZiTAL; 30/09/2009 a las 07:04
  #10 (permalink)  
Antiguo 30/09/2009, 07:24
Avatar de caricatos
Moderador
 
Fecha de Ingreso: abril-2002
Ubicación: Torremolinos (Málaga)
Mensajes: 19.607
Antigüedad: 22 años
Puntos: 1284
Respuesta: Aporte: zmodal ventana modal

Hola:

El único reproche que se me ocurre es denominar "modal" cuando no lo es... ¿Acaso es malo usar el prefijo pseudo...? o usar otro nombre... Ya sé que no tienes la culpa porque han catalogado con ese tipo las lightbox y similares... solo quiero dejar esta nota...

Por cierto. ¡Buen trabajo!

Saludos
__________________
Por favor:
No hagan preguntas de temas de foros en mensajes privados... no las respondo
  #11 (permalink)  
Antiguo 30/09/2009, 08:08
Avatar de 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

Hola caricatos un placer volver a verte, pues la verdad es que tienes razón nos hemos acostumbrado mal :(
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:34.