Ver Mensaje Individual
  #5 (permalink)  
Antiguo 14/05/2014, 07:25
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 18 años
Puntos: 292
Respuesta: Problema con un array

El problema que refieres de que se "confunden los enlaces" tiene que ver con que las imagenes siguen rotando pero el navegador coje un enlace cuando te colocas sobre la imagen.

Esto se solucionaria deteniendo la rotacion cuando te colocas sobre la imagen (onmouseover) y reanudandola cuando sales de ella (onmouseout)


Aca la solucion a ese problema en especial (probado):

Código Javascript:
Ver original
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4.     <title>Rotación de banners con Javascript</title>  
  5. <script>
  6.  
  7. var estado = true;
  8.  
  9. //creo array de imágenes
  10. array_imagen = new Array(4)
  11. array_imagen[0] = new Image(120,60)
  12. array_imagen[0].src = "images/lentejas.jpg"
  13. array_imagen[1] = new Image(120,60)
  14. array_imagen[1].src = "images/plato-presentado.jpg"
  15. array_imagen[2] = new Image(120,60)
  16. array_imagen[2].src = "images/Salsa-de-cebolla-con-tomate.jpg"
  17. array_imagen[3] = new Image(120,60)
  18. array_imagen[3].src = "images/compuesto-de-varias-frutas.jpg"
  19.  
  20. //creo el array de URLs
  21. array_url = new Array(4)
  22. array_url[0] = "http://www.cocina.com"
  23. array_url[1] = "http://www.elgurmet.com/"
  24. array_url[2] = "https://www.google.com.co/search?q=cocinando"
  25. array_url[3] = "http://www.culinario.net"
  26.  
  27. //variable para llevar la cuenta de la imagen siguiente
  28. contador = 0
  29.  
  30. //función para rotar el banner
  31. function alternar_banner()
  32. {
  33.     var timer
  34.    
  35.     console.log('Estado es '+estado)
  36.    
  37.     if (estado)
  38.     {
  39.         window.document["banner"].src = array_imagen[contador].src
  40.         window.document.links[0].href = array_url[contador]
  41.         contador ++
  42.         contador = contador % array_imagen.length
  43.         timer = setTimeout("alternar_banner();",1500)
  44.     }else
  45.         clearTimeout(timer)
  46. }
  47.  
  48.                // cross-browser
  49.         function addEventHandler(elem,eventType,handler)
  50.         {
  51.             if (elem.addEventListener)
  52.                 elem.addEventListener (eventType,handler,false);
  53.             else
  54.                 if (elem.attachEvent) elem.attachEvent ('on'+eventType,handler);
  55.         }
  56.        
  57.         // cross-browser
  58.         function removeEventHandler(elem,eventType,handler)
  59.         {      
  60.             if (elem.removeEventListener)
  61.                 elem.removeEventListener (eventType,handler,false);
  62.                
  63.             if (elem.detachEvent)
  64.                 elem.detachEvent ('on'+eventType,handler);
  65.         }
  66.        
  67.         // Mis handlers
  68.         function handlerFunction1() {estado=false;}
  69.         function handlerFunction2() { estado=true;alternar_banner(); }
  70.                
  71.        
  72.         function init() {      
  73.             addEventHandler(document.getElementById('imagen'),'mouseover',handlerFunction1);  // desactiva rotacion
  74.            
  75.             addEventHandler(document.getElementById('imagen'),'mouseout',handlerFunction2);  // activa rotacion
  76.            
  77.             alternar_banner();         
  78.         }
  79.    
  80. </script>
  81. </head>
  82.  
  83. <body onload="init();">
  84.  
  85. <a href=""><img src="" name="banner" width=120 height=60 border=0 id="imagen" ></a>
  86.  
  87.  
  88. </body>
  89. </html>

Logicamente necesitas las imagenes que aparecen ahi y en el path en que sean accesibles (en mi caso las puse en ./images)
__________________
Salu2!

Última edición por Italico76; 14/05/2014 a las 13:40