Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/12/2013, 14:27
Avatar de berkeleyPunk
berkeleyPunk
 
Fecha de Ingreso: febrero-2013
Ubicación: México :C
Mensajes: 565
Antigüedad: 12 años, 1 mes
Puntos: 22
Sonrisa ¿Cómo hacer background aleatorio cada x tiempo y con efecto FADE?

Buena tarde, ojalá puedan ayudarme.

Quiero hacer lo sig: que la imagen de background cambie aleatoriamente cada x tiempo, y que la transición entre imágenes tenga el efecto fade. Eso es todo.

El siguiente código funciona para la parte de que el background cambie cada 3 segundos de forma aleatoria.
Código Javascript:
Ver original
  1. setInterval( function()
  2. {
  3.     var numColorAleatorio = Math.floor( Math.random()*6 );
  4.     document.getElementById("body").style.backgroundImage = 'url(fondo_0' + numColorAleatorio + '.jpg)';   
  5. }, 3000);
  6. </script>


La bronca está con lo del efecto fade. Googleando me encontré con este sitio: http://www.disegnocentell.com.ar/notas2.php?id=203, en el cual hay un javascript para hacer el efecto FADE-in y FADE-out a una imagen.

Le he movido por todos lados para adecuarlo a mis necesidades, pero no logro hacerlo funcionar como deseo. Aquí les pongo lo que llevo hasta el momento. No logro avanzar más. El sig código hace casi lo que necesito, con la diferencia que el efecto fade lo hace sólo entre la 1ra y 2da imágenes, no al resto. Pongo de una vez todo el código del archivo para evitarles la molestia del copy-paste por partes.

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>FADE IN OUT ALPHA</title>
  6. <style type="text/css">
  7. html, body {width:100%; height:100%;}
  8.  
  9. function set_opacity(div, value) {
  10.     div.style.opacity = value;
  11.     div.style.MozOpacity = value;
  12.     div.style.KhtmlOpacity = value;
  13.     div.style.filter = 'alpha(opacity=' + value*100 + ')';
  14.     div.style.zoom=1;//necesario para Explorer
  15. }
  16. var transicion=function(inicio,fin,segundos){
  17.     var _this=this;
  18.     this.test=0;
  19.     if(_this.intervalo)clearInterval(_this.intervalo);
  20.     if(this.val && Math.abs(fin-_this.val)<0.01)return;
  21.     this.val=!this.val?inicio<1?inicio+.0001:inicio:this.val;
  22.    set_opacity(this, this.val);
  23.    this.pasos=(fin-inicio)/100;
  24.    this.pausa=segundos*10;
  25.    this.intervalo=setInterval(
  26.    function(){
  27.        if(_this.test>99 || Math.abs(fin-_this.val)<0.01){
  28.          clearInterval(_this.intervalo);
  29.        }
  30.        _this.test++;
  31.        //document.getElementById("log").innerHTML=_this.test;
  32.        _this.val=_this.val+_this.pasos;
  33.        if(_this.val<=.01)  
  34.            _this.style.display='none';
  35.        else
  36.            _this.style.display='block';
  37.        set_opacity(_this, _this.val);
  38.    },this.pausa);
  39. }
  40.  
  41. function ver(){
  42.    var obj=document.getElementById("fondo");
  43.    transicion.call(obj,0,1,3);
  44. //  setTimeout( cerrar(), 4000); //Entiendo que debería funcionar la función cerrar() 4 segundos después que termina de ejecutarse la función ver(), pero no pasa así.
  45. }
  46.  
  47.  
  48. function cerrar(){
  49.    var obj=document.getElementById("fondo");
  50.    transicion.call(obj,1,0,3);
  51. }
  52.  
  53.  
  54.  
  55.    
  56. /* *****************************************************************************************
  57.   *****************************************************************************************
  58.   ****   El siguiente código hace que se cambie aleatoriamente de background, pero el  ****
  59.   ****   FADE sólo funciona entre la 1ra y 2da imágenes. Al resto no les pone FADE.    ****
  60.   *****************************************************************************************
  61.   ***************************************************************************************** */
  62. setInterval( function()
  63. {
  64.     ver();
  65.     var numColorAleatorio = Math.floor( Math.random()*6 );
  66.     document.getElementById("fondo").style.backgroundImage = 'url(fondo_0' + numColorAleatorio + '.jpg)';
  67. }, 3000);
  68.  
  69. </script>  
  70. </head>
  71.  
  72.   <div id="fondo" style="position:absolute; top:0; left:0; width:100%; height:100%; background-image:url(fondo_01.jpg)">
  73.   </div>
  74. </body>
  75. </html>



Saludos cordiales.