Ver Mensaje Individual
  #5 (permalink)  
Antiguo 04/12/2013, 14:26
Pantaláimon
 
Fecha de Ingreso: julio-2006
Ubicación: Barcelona
Mensajes: 244
Antigüedad: 17 años, 10 meses
Puntos: 32
Respuesta: Usar transiciones CSS3 para simplificar código Javascript

He creado este fichero CSS:
Código CSS:
Ver original
  1. /* css/test.css */
  2. .transition {
  3.   -webkit-transition: all 2s linear;
  4.      -moz-transition: all 2s linear;
  5.       -ms-transition: all 2s linear;
  6.        -o-transition: all 2s linear;
  7.           transition: all 2s linear;
  8. }
  9.  
  10. .minimized {
  11.   font-size: 0px;
  12.   opacity: 0.0;
  13. }
y lo he insertado en el código HTML
Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <html lang='es'>
  3.   <head>
  4.     <meta charset='UTF-8' />
  5.     <link rel='stylesheet' type='text/css' href='css/test.css' />
  6.     <script type='text/javascript' src='js/insert_text.js'></script>
  7.     ...

He modificado el manejador de eventos simplicandose a esto:
Código Javascript:
Ver original
  1. window.onload = function() {
  2.  
  3.     document.getElementById( 'button_1' ).onclick = function() {
  4.         // crear elemento span invisible
  5.         var span = document.create( 'span', 'hola' );
  6.         span.className = 'transition minimized';
  7.  
  8.         // insertar elemento como segundo hijo de p
  9.         var section = this.parentNode.parentNode;
  10.         elem = section.getElementsByTagName('p')[0];
  11.         var inserted = elem.insert( span, 1 );
  12.  
  13.         // hacer visible el nodo
  14.         if( inserted ) {
  15.             inserted.className = 'transition';
  16.         }
  17.     }
  18. }
Pero sigue sin haber transición. El elemento se inserta correctamente pero de forma instantánea.


Edit, he probado con la propiedad classList y sus métodos add y remove pero sigo con el mismo problema:
Código Javascript:
Ver original
  1. // crear elemento span invisible
  2.         var span = document.create( 'span', 'hola' );
  3.         span.classList.add('transition');
  4.         span.classList.add('minimized';
  5.  
  6.         // insertar elemento como segundo hijo de p
  7.         var section = this.parentNode.parentNode;
  8.         elem = section.getElementsByTagName('p')[0];
  9.         var inserted = elem.insert( span, 1 );
  10.  
  11.         // hacer visible el nodo
  12.         if( inserted ) {
  13.             inserted.classList.remove('minimized');
  14.         }
Un saludo!

Última edición por Pantaláimon; 04/12/2013 a las 15:10