Javascript - 纯色文字Flash

时间:2012-02-16 06:55:19

标签: javascript

基本上我想做的就是让文字闪烁不同的颜色。等文字闪烁蓝色然后是红色,然后是粉红色,然后是紫色等。先谢谢

3 个答案:

答案 0 :(得分:1)

您可以使用CSS3而不是Javascript。下面的示例从红色变为黄色,绿色,蓝色并返回红色。请记住为mozilla,webkit等添加特定的供应商前缀(例如 -moz-animation -moz-keyframes

<强>的index.html

<!doctype html>
<html>
<head>
    <title>Blinking Text</title>    
    <link rel="stylesheet" href="style.css" />      
</head>
<body>   
    <div class="test">Text</div>       
   </body>
</html>

<强>的style.css

.test
{
    font-size: 48pt;
    animation: 2s blink steps(1) infinite;  
}

@keyframes blink
{
    0% { color: red; }
    25% { color: yellow; }
    50% { color: green; }
    75% { color: blue; }
    100% {color: red; }
}

答案 1 :(得分:0)

看看这个jsfiddle。为了教学目的,我评论了JS的每个部分:http://jsfiddle.net/VXRpz/

答案 2 :(得分:0)

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
相关问题