数字时钟与图像

时间:2017-03-22 23:15:35

标签: javascript

我想用图像制作数字时钟,但我不知道从哪里开始。 该项目包括用24小时风格的图像替换时钟编号。

从7到20(需要显示蓝色数字)(打开)从20到7(需要显示红色数字)(已关闭)。

我试图解决w3c问题" https://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock"

但我不知道如何用2 diffrents数组中的图像替换数字。它只需要在javascript中,因为它是我试图学习的东西。

我不知道我的问题是否清楚,但谢谢。

2 个答案:

答案 0 :(得分:0)

好吧我们如何把时间作为一个字符串,我建议取字符串的每个字母并使用图像来表示每个字母使用HTML img标签(我们可以使用纯javascript生成)。看看下面的例子。我希望它有所帮助。

function startTime() {
  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes();
  var s = today.getSeconds();
  m = checkTime(m);
  s = checkTime(s);
  //this variable contains the time as a string
  var string = h + ":" + m + ":" + s;
  //this converts string to a set of HTML img tags containing images
  var img = stringToImage(string);
  //this puts the time on the website
  document.getElementById('txt').innerHTML =
    string + "<br>" + img;
  //this loops the function every 1000 milliseconds (1 second)
  var t = setTimeout(startTime, 1000);
}

function checkTime(i) {
  if (i < 10) {
    i = "0" + i
  } // add zero in front of numbers < 10
  return i;
}

//This function takes each letter of an array and pairs it to an image of the img array 
function stringToImage(s) {
  //create a temporary blank variable to hold HTML images
  var temp = ""
  for (var i = 0; i < s.length; i++) {
    //we take the URL from array img and shove it into an HTML img tag
    //also append it to the temporary variable
    temp = temp + "<img src='" + img[s[i]] + "'/>"
  }
  //uncomment below line to see what the temporary variable says
  //console.log(temp)
  return temp
}


//all image URLs are put into this array. feel free to change URLs
var img = {
  "1": "http://www.kidsmathgamesonline.com/images/pictures/numbers120/number1.jpg",
  "2": "http://www.kidsmathgamesonline.com/images/pictures/numbers120/number2.jpg",
  "3": "http://www.kidsmathgamesonline.com/images/pictures/numbers120/number3.jpg",
  "4": "http://www.kidsmathgamesonline.com/images/pictures/numbers120/number4.jpg",
  "5": "http://www.kidsmathgamesonline.com/images/pictures/numbers120/number5.jpg",
  "6": "http://www.kidsmathgamesonline.com/images/pictures/numbers120/number6.jpg",
  "7": "http://www.kidsmathgamesonline.com/images/pictures/numbers120/number7.jpg",
  "8": "http://www.kidsmathgamesonline.com/images/pictures/numbers120/number8.jpg",
  "9": "http://www.kidsmathgamesonline.com/images/pictures/numbers120/number9.jpg",
  "0": "http://www.kidsmathgamesonline.com/images/pictures/numbers120/number0.jpg",
  ":": "https://images.duckduckgo.com/iu/?u=http%3A%2F%2Fwww.colonsemicolon.com%2Fwp-content%2Fuploads%2F2012%2F02%2Fcolon.gif&f=1"
}

//credit images to:
//http://www.kidsmathgamesonline.com/pictures/numbers.html
//http://www.colonsemicolon.com/
<!DOCTYPE html>
<html>

<head>
  <!--<script type="text/javascript" src="topic.js">-->
  </script>
</head>

<body onload="startTime()">

  <div id="txt"></div>
</body>

</html>

答案 1 :(得分:0)

setInterval(test,1000);
		function test()
		{
			$("body").load('#ch');
		}
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<!-- <meta http-equiv="refresh" content="1"> -->
	<script>
		
	</script>

</head>
<body>
<center>
	<h1>Digital Clock</h1>
	<hr>
	<div id="ch"><?php date_default_timezone_set("Asia/Kolkata"); echo date('h:i:s A e'); ?></div>
</center>
</body>
</html>