Ver Mensaje Individual
  #4 (permalink)  
Antiguo 02/12/2014, 23:01
spocnic
 
Fecha de Ingreso: julio-2014
Ubicación: México
Mensajes: 13
Antigüedad: 10 años, 1 mes
Puntos: 0
Respuesta: Cambiar de color con un clic JS

Ya lo intente pero sin tener éxito hice un ejemplo básico con la mapa de imágenes pero no da los clics...

Aqui lo que hice...
Código HTML:
Ver original
  1. <!doctype html>
  2.     <head>
  3.         <meta charset="UTF-8">
  4.         <title>JS</title>    
  5.        
  6.                 <!-- JavaScript -->
  7.                 <script type="text/javascript" src="js/animacionColores.js"></script>
  8.     </head>
  9.  
  10.     <body>
  11.             <img src="imagenes/ciruclo.png" alt="" width="500" height="500" usemap="#circulo"/>
  12.                 <map name="circulo">
  13.                         <area shape="poly" coords="251,346,254,464,119,412,47,313,42,212,88,116,164,55,236,38,244,38" href="#" onclick = "color(this)" data-cuenta = "-1" >
  14.                
  15.                         <area shape="poly" coords="353,63,443,170,444,327,385,408,335,444,283,459,357,64,271,42,264,85,270,254,280,461" href="#" onclick = "color(this)" data-cuenta = "-1">
  16.                 </map>
  17.     </body>
  18. </html>

Código Javascript:
Ver original
  1. function color(elemento){
  2.     var colores = ["gray", "red", "yellow", "white"],
  3.         cuenta = Number(elemento.dataset.cuenta);
  4.    
  5.     if (cuenta + 1 <= colores.length - 1){
  6.         cuenta++;
  7.     }
  8.     else{
  9.         cuenta = 0;
  10.     }
  11.    
  12.     elemento.style.background = colores[cuenta];
  13.     elemento.dataset.cuenta = cuenta;
  14.     /*elemento.innerText = cuenta + 1;*/
  15. }