Niveau
..........
En rapport...

Faire clignoter du texte en continu

Voici un petit script qui vous permettra de faire clignoter un texte de deux couleurs differentes indéfiniment.

javascript
	function rgb2hex(r,g,b) {
		var hexVal = function(n) {
			var data = "0123456789ABCDEF";
			if (n==null) return "00";
			n=parseInt(n); 
			if (n==0 || isNaN(n)) return "00";
			n=Math.round(Math.min(Math.max(0,n),255));
			return data.charAt((n-n%16)/16) + data.charAt(n%16);
		}
		return hexVal(r)+hexVal(g)+hexVal(b);
	}
 
 
	function blinkMe(myElement,speed,firstColor,lastColor) {
		var fn=function( ) {
			var hactual = myElement.style.color.replace(/(rgb\(|\))/i,"").split(",");
			if (hactual.length!=1)
				hactual="#"+rgb2hex(hactual[0],hactual[1],hactual[2]);
			else
				hactual=hactual[0];
 
			myElement.style.color=(hactual.toLowerCase()==firstColor.toLowerCase())?lastColor:firstColor;
		}
		var interval_name = window.setInterval(function () { 
			fn.apply(this, arguments); 
		}, speed);
	}

Il suffit d'appeler la fonction blinkMe avec comme argument:
myElement qui représente l'objet qui contient le texte a faire clignoter.
speed la vitesse de clignotement
firstColor la première couleur affichée. (en hexadecimal)
lastColor la seconde couleur. (en hexadecimal)
Exemple:
blinkMe(document.getElementById(“test”),500,”#FF0000”,”#0000FF”);

La fonction rgb2hex permet de récupérer le code héxadecimal d'une couleur en RGB. En effet firefox renvoie un code RGB lorsqu'on utilise obj.style.color.
Voir : convertir_le_rgb_en_hexadecimal

Lancer l'exemple
30/11/2007 10:46 -

ManiT4c
Vous désirez créer votre site web ? Je suiswebmaster en normandie sinon vous pouvez consulter mon profil yoolink pour suivre mon actu etmon twitter.

Discussion

Si vous ne pouvez pas lire les lettres, tlchargez ce fichier WEV et coutez les.