Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML flashing text colors

This is quite an unusual request, but..

Is there anyway to make some text alternate between 2 colors every second?

So it appears as though it's flashing between say... red and grey? I don't mean background color, I mean the actual font color. I'm assuming it would need javascript or something.

Is there any simple way to do that?

(disregarding the fact it could look ugly)

UPDATE

Id like to call to this function several times on my page, each one passing along a different color to alternate with GREY

.

like image 647
user1022585 Avatar asked Dec 02 '22 00:12

user1022585


2 Answers

As per your request:

    function flashtext(ele,col) {
    var tmpColCheck = document.getElementById( ele ).style.color;

      if (tmpColCheck === 'silver') {
        document.getElementById( ele ).style.color = col;
      } else {
        document.getElementById( ele ).style.color = 'silver';
      }
    } 

    setInterval(function() {
        flashtext('flashingtext','red');
        flashtext('flashingtext2','blue');
        flashtext('flashingtext3','green');
    }, 500 ); //set an interval timer up to repeat the function

JSFiddle here: http://jsfiddle.net/neuroflux/rXVUh/14/

like image 120
Barrie Reader Avatar answered Dec 04 '22 10:12

Barrie Reader


Here's an easy way to do it with plain JavaScript:

function flash() {
    var text = document.getElementById('foo');
    text.style.color = (text.style.color=='red') ? 'green':'red';
}
var clr = setInterval(flash, 1000);

This will alternate the color of the text between red and green every second. jsFiddle example.

Here's another example where you can set the colors of different elements:

function flash(el, c1, c2) {
    var text = document.getElementById(el);
    text.style.color = (text.style.color == c2) ? c1 : c2;
}
var clr1 = setInterval(function() { flash('foo1', 'gray', 'red') }, 1000);
var clr2 = setInterval(function() { flash('foo2', 'gray', 'blue') }, 1000);
var clr3 = setInterval(function() { flash('foo3', 'gray', 'green') }, 1000);

and the jsFiddle to go with it. You pass the ID of the element you want to flash and the two colors to alternate between.

like image 44
j08691 Avatar answered Dec 04 '22 10:12

j08691