因此,我正在學習第一個JavaScript類(總noob),其中一個任務是修改數字時鐘,顏色爲紅色至數小時,綠色分鐘,藍色至秒,然後在改變時增加相應顏色分量。我已經成功地爲每個元素(小時,分鐘,秒)分配了一個十進制顏色值(例如「#850000」),但我的大腦正在試圖弄清楚如何在小時,分鐘和秒改變時提高亮度,即紅色上升到「#870000」,從下午1:00點到下午2點00分,我到處搜索,沒有任何幫助,如何成功地做到這一點。這是我迄今爲止以及對此的任何幫助將不勝感激爲數字時鐘每小時(r),分鐘(g),秒(b)增加RGB分量
TJ
<script type="text/javascript">
<!--
function updateClock()
{
var currentTime = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
// Pad the minutes with leading zeros, if required
currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
// Pad the seconds with leading zeros, if required
currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
// Choose either "AM" or "PM" as appropriate
var timeOfDay = (currentHours < 12) ? "AM" : "PM";
// Convert the hours component to 12-hour format
currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
// Convert an hours component if "0" to "12"
currentHours = (currentHours == 0) ? 12 : currentHours;
// Get hold of the html elements by their ids
var hoursElement = document.getElementById("hours");
document.getElementById("hours").style.color = "#850000";
var minutesElement = document.getElementById("minutes");
document.getElementById("minutes").style.color = "#008500";
var secondsElement = document.getElementById("seconds");
document.getElementById("seconds").style.color = "#000085";
var am_pmElement = document.getElementById("am_pm");
// Put the clock sections text into the elements' innerHTML
hoursElement.innerHTML = currentHours;
minutesElement.innerHTML = currentMinutes;
secondsElement.innerHTML = currentSeconds;
am_pmElement.innerHTML = timeOfDay;
}
// -->
</script>
</head>
<body onload="updateClock(); setInterval('updateClock()', 1000)">
<h1 align="center">The JavaScript digital clock</h1>
<h2 align="center">Thomas Fertterer - Lab 2</h2>
<div id='clock' style="text-align: center">
<span id="hours"></span>:
<span id='minutes'></span>:
<span id='seconds'></span>
<span id='am_pm'></span>
</div>
</body>
</html>
有些什麼相關:http://collegesnippets.blogspot.com/2011/09/time-color.html – Ivan