|
查看: 1735|回复: 3
|
普通文字一直转换颜色 (HTML/JavaScript)
[复制链接]
|
|
|
本帖最后由 karhing 于 10-5-2010 06:20 PM 编辑
我要用html + JavaScript 吧文字转换颜色 ,(要自动转换一直换的,上网找到很多都是要按button 才换的 @@ )
我找到以下的sample,但是我是要整句字...不是一个字母
有人可以帮帮忙吗
- <SPAN ID="animate"><B>Text you want to animate</B></SPAN>
- <SCRIPT language="JavaScript">
- animate('animate', '#ff8000');
- </SCRIPT>
复制代码
- <SCRIPT language="JavaScript" type="text/javascript">
- /*
- Animate your text - Script
- Visit http://rainbow.arch.scriptmania.com/scripts/
- for this script and many more
- */
- var ie4 = false;
- if(document.all) {
- ie4 = true;
- }
- function setContent(name, value) {
- var d;
- if (ie4) {
- d = document.all[name];
- } else {
- d = document.getElementById(name);
- }
- d.innerHTML = value;
- }
- function getContent(name) {
- var d;
- if (ie4) {
- d = document.all[name];
- } else {
- d = document.getElementById(name);
- }
- return d.innerHTML;
- }
- function setColor(name, value) {
- var d;
- if (ie4) {
- d = document.all[name];
- } else {
- d = document.getElementById(name);
- }
- d.style.color = value;
- }
- function getColor(name) {
- var d;
- if (ie4) {
- d = document.all[name];
- } else {
- d = document.getElementById(name);
- }
- return d.style.color;
- }
- function animate(name, col) {
- var value = getContent(name);
- if (value.indexOf('<span') >= 0) { return; }
- var length = 0;
- var str = '';
- var ch;
- var token = '';
- var htmltag = false;
- for (i = 0; i < value.length; i++) {
- ch = value.substring(i, i+1);
- if (i < value.length - 1) { nextch = value.substring(i+1, i+2); } else { nextch = ' '; }
- token += ch;
- if (ch == '<' && '/aAbBpPhHiIoOuUlLtT'.indexOf(nextch) >= 0) { htmltag = true; }
- if (ch == '>' && htmltag) { htmltag = false; }
- if (!htmltag && ch.charCodeAt(0) > 30 && ch != ' ' && ch != '\n') {
- str += '<span id="' + name + '_' + length + '">' + token + '</span>';
- token = '';
- length++;
- }
- }
- setContent(name, str);
- command = 'animateloop(\'' + name + '\', ' + length + ', 0, 1, \'' + col + '\')';
- setTimeout(command , 100);
- }
- function animateloop(name, length, ind, delta, col) {
- var next = ind + delta;
- if (next >= length) { delta = delta * -1; next = ind + delta; }
- if (next < 0) { delta = delta * -1; next = ind + delta; }
- setColor(name + '_' + ind, getColor(name + '_' + next));
- setColor(name + '_' + next, col);
- command = 'animateloop(\'' + name + '\', ' + length + ', ' + next + ', ' + delta + ', \'' + col + '\')';
- setTimeout(command , 100);
- }
- </SCRIPT>
复制代码
还是有其他更简单的写法吗?
我只要2种颜色一直换而已, |
|
|
|
|
|
|
|
|
|
|
发表于 10-5-2010 05:51 PM
|
显示全部楼层
java 和 java script 都
傻傻分不清楚..  |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-5-2010 06:19 PM
|
显示全部楼层
本帖最后由 karhing 于 10-5-2010 06:22 PM 编辑
忘记了@@" 是 java script 才对
找到很多都是click 了 button 才换颜色的 
- <script language="Javascript">
- function changeTextColor(color) {
- document.body.style.color = color;
- }
- //-->
- </script>
- <form>
- <input type="button" value="red" onClick="changeTextColor(this.value)">
- <input type="button" value="green" onClick="changeTextColor(this.value)">
- <input type="button" value="blue" onClick="changeTextColor(this.value)">
- <input type="button" value="black" onClick="changeTextColor(this.value)">
- </form>
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 11-5-2010 12:29 AM
|
显示全部楼层
本帖最后由 doktorkoo 于 11-5-2010 01:09 AM 编辑
回复 3# karhing
你看看这是你要的吗?- <html>
- <script language="Javascript">
- var timeseconds=0;
- for( ; ;)
- {
- timeseconds += 1000;
- setTimeout("document.fgColor='black';",timeseconds);
- timeseconds += 1000;
- setTimeout("document.fgColor='blue';",timeseconds);
- }
- </script>
- <body>
- <h1>Hello Word</h1>
- </body>
- </html>
复制代码 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|