Ver Mensaje Individual
  #7 (permalink)  
Antiguo 29/09/2010, 10:08
kardo
 
Fecha de Ingreso: septiembre-2010
Mensajes: 13
Antigüedad: 13 años, 8 meses
Puntos: 0
Respuesta: Rotación de banners

Muchas gracias a los dos por la ayuda pero está claro que javascript no es lo mio

He copiado tu codigo en un js y no me funciona. ¿que hago mal? las dos variables, ¿hay que ponerlas? Sigo el codigo y lo entiendo pero no me funciona.

_cronos2, tu codigo tampoco me funciona


var json = {
rotator : {
timer : 01,
images : [{
title : "Banner1",
imageUrl: "_aux_images_banner/banner1.gif",
url : "http://www.terra.es",
target : "_blank"
},{
title : "Banner2",
imageUrl: "_aux_images_banner/banner2.jpg",
url : "http://www.terra.es",
target : "_blank"
},{
title : "Banner3",
imageUrl: "_aux_images_banner/banner3.gif",
url : "http://www.terra.es",
target : "_blank"
},{
title : "Banner4",
imageUrl: "_aux_images_banner/banner4.gif",
url : "http://www.terra.es",
target : "_blank"
}]
}
};

//forma de uso ;)
//var rotador1 = new Rotador("banner",json);
//var rotador2 = new Rotador("otroContenedor",json);
///////////////////////////////////////////////////////////////
/**
* Constructor de Rotador
* @param container: recibe el ID del contenedor donde se renderizará
* @param data: información de imágenes y configuraciones
*
**/
var Rotador = function(container,data){
this.container = document.getElementById(container);
this.data = data
this.prints = json.rotator.images;
this.timer = json.rotator.timer;
this.r = Math.round(Math.random()*this.prints.length);
}

Rotador.prototype = {
//imprime una imagen
print : function() {
this.r = this.r < this.prints.length?this.r:0;

if(this.container) {
var img = this.prints[this.r];
container.innerHTML = "<a target='_blank' href='"+ img.url+ "'>"
+ "<img border='0' alt='" + img.title + "' title='" + img.title + "' src='"+ img.imageUrl+ "' />"
+ "</a>";
}
this.r++;
},

//crea un intervalo que imprime las imagenes consecutivas
rotator : function() {
this.thread = setInterval(this.print, this.timer*6000); //no usen un string para ejecutar la función!!!
},

//inicia el proceso de pintado
start : function(){
this.print();
this.rotator();
}
}