javascript - 模拟时钟 - 带有 Javascript 的 PHP - 日期函数
问题描述
对于我个人的挑战,我正在尝试重新创建一个低保真房间。在房间里,我想显示一个带有当前时间的模拟时钟,完全用 PHP 编程。
我目前能够通过 if 语句对其进行“硬编码”,但现在我想在正确的时间每分钟移动分钟部分。我在网上找不到它,所以我希望你能在这里帮助我!不知何故,我需要使它成为一个 for 循环,但我不知道如何遍历 css 部分。
将不胜感激任何帮助!
<div class="clock">
<?php
$m = date("i");
echo date("h:i:s");
//Need to become a for/while loop
if ($m == 14){
echo '<div id="mins" style="transform: rotate(60deg);"</div>';
}
?>
<div id="mins"></div>
</div>
.clock{
height: 150px;
width: 150px;
border-radius: 50%;
background: white;
position: absolute;
left: 45%;
top: 20%;
}
#mins{
height: 60px;
width: 5px;
left: 50%;
position: relative;
transform: translateX(-50%);
background: black;
transform: rotate(0deg);
transform-origin: bottom center;
}
解决方案
我使用 Javascript 的模拟脚本解决方案。不需要 PHP。
index.php
<div class="clock">
<div id="hours"></div>
<div id="minutes"></div>
<div id="seconds"></div>
</div>
javascript
const updateInMS = 1000;
// Get the HTML elements from the page.
const clock = document.getElementsByClassName('clock')[0];
const htmHours = document.getElementById('hours');
const htmMinutes = document.getElementById('minutes');
const htmSeconds = document.getElementById('seconds');
// Start the timer
startTimer();
function startTimer() {
// Trigger the tick function that loops the clock
tick();
}
function tick() {
setTimeout(function() {
// Retrieve the date
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const secondsInDegrees = (360 * seconds) / 60;
const minutesInDegrees = (360 * minutes) / 60;
const hoursInDegrees = (360 * hours) / 12;
htmHours.style.transform = 'rotate(' + hoursInDegrees + 'deg)';
htmMinutes.style.transform = 'rotate(' + minutesInDegrees + 'deg)';
htmSeconds.style.transform = 'rotate(' + secondsInDegrees + 'deg)';
tick();
}, updateInMS);
}
css
.clock {
height: 150px;
width: 150px;
border-radius: 50%;
background: white;
position: absolute;
left: 45%;
top: 20%;
}
.clock div {
height: 60px;
width: 5px;
left: 50%;
/* UPDATED THIS TO FIXED: */
position: fixed;
transform: translateX(-50%);
background: black;
transform: rotate(0deg);
transform-origin: bottom center;
}
#hours {
background: black;
}
#minutes {
background: red;
}
#seconds {
background: blue;
}
希望你喜欢我的解决方案。
推荐阅读
- python - 从安装程序打开时 Python 程序崩溃,但如果从 Spyder IDE 运行脚本则不会
- svg - SVG - 基线线
- python - 计算元组列表中元素的可达性
- android - 检查 Android 中的 VPN 连接是否处于活动状态并显示消息?
- shell - 在 Apache Guacamole 中的特定命令之前执行操作
- c++11 - 对象的 IPC 共享 -ZeroMQ 或 Boost::interprocess 哪个更快?
- highcharts - 如何仅使用 yii2 扩展在 Highcharts 中显示“正在加载...”消息?
- html - 将表单数据传递到angularjs中的其他页面
- animation - 我一直在尝试在搅拌机中以纹理绘制模式保存项目绘制下的所有编辑,但找不到“保存所有已编辑”按钮
- python - MSYS2环境下如何使用python访问库文件