2012-01-29 247 views
0

我有一個PHP腳本,顯示一個圖像(根據當前時間),我希望它閃爍的圖像,所以我試圖添加一個腳本,但它的一些什麼不工作。請看一看。我會感謝你的幫助。提前致謝。圖像不閃爍

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 

</head> 

<body onload="startit();"> 
<?php 
$hour = date('H', time()); // Returns the hours of the server time, from 0 to 23. 

switch ($hour) { 
case '0': 
$img = '0.jpg'; 
break; 
case '1': 
$img = '0.jpg'; 
break; 
// ... and so on... 
default: 
$img = '1.jpg'; 
} 

?> 

<img src="<?php echo $img; ?>" id="mydiv" /> 
<script language=javascript> 
/*This script written by Chris Stahl*/ 
/*It may be used and modified by */ 
/*webmaster's for private use.  */ 
function startit() { 
window.setInterval("changecolor()",100); 
} 

function changecolor() { 
var rndred=Math.floor(256*Math.random()); 
var rndgreen=Math.floor(256*Math.random()); 
var rndblue=Math.floor(256*Math.random()); 
var colorarray=[rndred, rndgreen, rndblue]; 
var hexcolor="#"; 
for(i=0;i<3;i++) { 
hexcolor=hexcolor +""+ converttohex(Math.floor(colorarray[i]/16)); 
hexcolor=hexcolor +""+ converttohex(colorarray[i]%16); 
} 
document.getElementById("mydiv").style.color=hexcolor; 
} 
function converttohex(num) { 
switch (num) { 
case 10: return "A"; 
case 11: return "B"; 
case 12: return "C"; 
case 13: return "D"; 
case 14: return "E"; 
case 15: return "F"; 
default: return num; 
} 
} 
</script> 


</body> 
</html> 
+0

不會'style.color'只是影響相關元素的背景顏色?自從我做了任何css之後已經有一段時間了,但這就是我記得它的工作原理。我不認爲這會對圖像產生任何影響。 – Ilion 2012-01-29 06:37:57

回答

3

這將工作

http://jsfiddle.net/mplungjan/9Kdh5/

<body> 
    <div id="myDiv"><img src="http://rd.cas.de/tim/native/image.png" id="myImg" /></div> 
</body> 

<script type="text/javascript> 
/* This script written by Chris Stahl */ 
/* It may be used and modified by  */ 
/* webmaster's for private use.  */ 

window.onload=function() { 
    window.setInterval(changecolor,100); 
} 

function changecolor() { 
    var rndred=Math.floor(256*Math.random()); 
    var rndgreen=Math.floor(256*Math.random()); 
    var rndblue=Math.floor(256*Math.random()); 
    var colorarray=[rndred, rndgreen, rndblue]; 
    var hexcolor="#"; 
    for(i=0;i<3;i++) { 
    hexcolor=hexcolor +""+ converttohex(Math.floor(colorarray[i]/16)); 
    hexcolor=hexcolor +""+ converttohex(colorarray[i]%16); 
    } 
    document.getElementById("myDiv").style.backgroundColor=hexcolor; 
} 
function converttohex(num) { 
    switch (num) { 
    case 10: return "A"; 
    case 11: return "B"; 
    case 12: return "C"; 
    case 13: return "D"; 
    case 14: return "E"; 
    case 15: return "F"; 
    default: return num; 
    } 
} 
</script>