Ver Mensaje Individual
  #3 (permalink)  
Antiguo 12/03/2015, 11:12
Pantaláimon
 
Fecha de Ingreso: julio-2006
Ubicación: Barcelona
Mensajes: 244
Antigüedad: 17 años, 9 meses
Puntos: 32
Respuesta: Frase cambiante en el subtítulo

Orientado a objetos: https://jsfiddle.net/hct6q5ce/

Código HTML:
Ver original
  1. <h1 id="rotando"></h1>
  2. var rotor = new Rotor ({
  3.     id: 'rotando',
  4.     interval: 1000,
  5.     frases: [
  6.         "TEXTO FRASE 0<br>-AUTOR 0",
  7.         "TEXTO FRASE 1<br>-AUTOR 1",
  8.         "TEXTO FRASE 2<br>-AUTOR 2"
  9.     ]
  10. });
  11.  
  12. rotor.play();

Código Javascript:
Ver original
  1. function Rotor(options) {
  2.     if (!(this instanceof Rotor)) {
  3.         return new Rotor(options)
  4.     }
  5.    
  6.     this.frases   = options.frases
  7.     this.elem     = document.getElementById(options.id) //|| options.elem
  8.     this.interval = options.interval
  9.     this.indice   = Math.floor(Math.random() * this.frases.length)
  10. }
  11.  
  12. Rotor.prototype.play = function () {
  13.     if (this.indice === this.frases.length) {
  14.         this.indice = 0
  15.     }
  16.     this.elem.innerHTML = this.frases[this.indice]
  17.     console.log(this.indice)
  18.     ++this.indice
  19.     setTimeout(this.play.bind(this), this.interval)
  20. }
__________________
github.com/xgbuils | npm/xgbuils