javascript 时钟上的秒针倒退
second hand on javascript clock goes backwards
我在 JavaScript 的 30 天里做了一个练习。练习是制作一个时钟。我做了时钟here on CodePen.你玩一下,你会发现秒针到头的时候,其实是向后转360度到下一秒。这是此版本时钟的 JavaScript:
const secondHand = document.querySelector('.second-hand');
const minuteHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds();
const secondsDegrees = ((seconds / 60) * 360) + 90;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const minutes = now.getMinutes();
const minutesDegrees = ((minutes / 60) * 360) + 90;
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
const hours = now.getHours();
const hoursDegrees = ((hours / 60) * 360) + 90;
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
}
setInterval(setDate, 1000);
我查看了其他没有问题的人为他们的时钟做了什么(他们的 CodePen 是 here. 我更改了我的 secondsDegrees、minuteDegrees 和 hoursDegrees 的计算以匹配他们的。
我想现在,除了事物的顺序,我们的代码几乎是一样的。然而,现在我的秒针在到达 30 秒标记时正在倒转。这是我修改后的代码:
const secondHand = document.querySelector('.second-hand');
const minuteHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds();
const secondsDegrees = -90 + (seconds * 6);
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const minutes = now.getMinutes();
const minutesDegrees = -90 + (minutes * 360/60);
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
const hours = now.getHours();
const hoursDegrees = -90 + (hours * (360/12));
hoursDegrees = hoursDegrees + (0.5 * minutes);
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
}
setInterval(setDate, 1000);
现场示例(时钟为 HTML 和 CSS):
const secondHand = document.querySelector('.second-hand');
const minuteHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds();
const secondsDegrees = ((seconds / 60) * 360) + 90;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const minutes = now.getMinutes();
const minutesDegrees = ((minutes / 60) * 360) + 90;
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
const hours = now.getHours();
const hoursDegrees = ((hours / 60) * 360) + 90;
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
}
setInterval(setDate, 1000);
html {
background: #018DED url(http://unsplash.it/1500/1000?image=881&blur=50);
background-size: cover;
font-family: 'helvetica neue';
text-align: center;
font-size: 10px;
}
body {
margin: 0;
font-size: 2rem;
display: flex;
flex: 1;
min-height: 100vh;
align-items: center;
}
.clock {
width: 30rem;
height: 30rem;
border: 20px solid white;
border-radius: 50%;
margin: 50px auto;
position: relative;
padding: 2rem;
box-shadow: 0 0 0 4px rgba(0, 0, 0, 0.1), inset 0 0 0 3px #EFEFEF, inset 0 0 10px black, 0 0 10px rgba(0, 0, 0, 0.2);
}
.clock-face {
position: relative;
width: 100%;
height: 100%;
transform: translateY(-3px);
/* account for the height of the clock hands */
}
.hand {
width: 50%;
height: 6px;
position: absolute;
top: 50%;
transform-origin: 100%;
transform: rotate(90deg);
transition: all 0.05s;
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
}
.hour-hand,
.min-hand {
background: black;
}
.second-hand {
background: red;
}
<div class="clock">
<div class="clock-face">
<div class="hand hour-hand"></div>
<div class="hand min-hand"></div>
<div class="hand second-hand"></div>
</div>
</div>
问题是过渡。当秒数从 59 变为 0 时,您正在从 444 度转换回 90 度,因此当浏览器应用 CSS过渡。
如果您在从 59 度到 0 度(例如,回到 90 度)时禁用过渡,您将看不到它。请注意,这适用于所有手,而不仅仅是秒。
const secondHand = document.querySelector('.second-hand');
const minuteHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds();
const secondsDegrees = ((seconds / 60) * 360) + 90;
if (secondsDegrees == 90) {
secondHand.classList.add("no-transition");
} else {
secondHand.classList.remove("no-transition");
}
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const minutes = now.getMinutes();
const minutesDegrees = ((minutes / 60) * 360) + 90;
if (minutesDegrees == 90) {
minuteHand.classList.add("no-transition");
} else {
minuteHand.classList.remove("no-transition");
}
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
const hours = now.getHours();
const hoursDegrees = ((hours / 60) * 360) + 90;
if (hoursDegrees == 90) {
hourHand.classList.add("no-transition");
} else {
hourHand.classList.remove("no-transition");
}
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
}
setInterval(setDate, 1000);
html {
background: #018DED url(http://unsplash.it/1500/1000?image=881&blur=50);
background-size: cover;
font-family: 'helvetica neue';
text-align: center;
font-size: 10px;
}
body {
margin: 0;
font-size: 2rem;
display: flex;
flex: 1;
min-height: 100vh;
align-items: center;
}
.clock {
width: 30rem;
height: 30rem;
border: 20px solid white;
border-radius: 50%;
margin: 50px auto;
position: relative;
padding: 2rem;
box-shadow: 0 0 0 4px rgba(0, 0, 0, 0.1), inset 0 0 0 3px #EFEFEF, inset 0 0 10px black, 0 0 10px rgba(0, 0, 0, 0.2);
}
.clock-face {
position: relative;
width: 100%;
height: 100%;
transform: translateY(-3px);
/* account for the height of the clock hands */
}
.hand {
width: 50%;
height: 6px;
position: absolute;
top: 50%;
transform-origin: 100%;
transform: rotate(90deg);
transition: all 0.05s;
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
}
.no-transition {
transition: none;
}
.hour-hand,
.min-hand {
background: black;
}
.second-hand {
background: red;
}
<div class="clock">
<div class="clock-face">
<div class="hand hour-hand"></div>
<div class="hand min-hand"></div>
<div class="hand second-hand"></div>
</div>
</div>
我使用了这段代码并且它有效。只需将时钟秒度增加到大于 360 度即可正常工作。
var timeinterval=null;
var srotation=null;
var newrotation=null;
function clockfunc()
{
timeinterval=setInterval(clockdisp,1000);
}
function clockdisp()
{
var d=new Date();
var htime=d.getHours();
var mtime=d.getMinutes();
var stime=d.getSeconds();
var hrotation=30*htime+mtime/2;
var mrotation=6*mtime;
if(newrotation>=354)
{
newrotation=newrotation+6;
}
if(srotation<354 || srotation==null)
{
srotation=6*stime;
newrotation=srotation;
}
document.getElementById("hrhand").style.transform="rotate("+hrotation+ "deg)";
document.getElementById("mhand").style.transform="rotate("+mrotation+ "deg)";
document.getElementById("shand").style.transform="rotate("+newrotation+ "deg)";
}
我在 JavaScript 的 30 天里做了一个练习。练习是制作一个时钟。我做了时钟here on CodePen.你玩一下,你会发现秒针到头的时候,其实是向后转360度到下一秒。这是此版本时钟的 JavaScript:
const secondHand = document.querySelector('.second-hand');
const minuteHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds();
const secondsDegrees = ((seconds / 60) * 360) + 90;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const minutes = now.getMinutes();
const minutesDegrees = ((minutes / 60) * 360) + 90;
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
const hours = now.getHours();
const hoursDegrees = ((hours / 60) * 360) + 90;
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
}
setInterval(setDate, 1000);
我查看了其他没有问题的人为他们的时钟做了什么(他们的 CodePen 是 here. 我更改了我的 secondsDegrees、minuteDegrees 和 hoursDegrees 的计算以匹配他们的。 我想现在,除了事物的顺序,我们的代码几乎是一样的。然而,现在我的秒针在到达 30 秒标记时正在倒转。这是我修改后的代码:
const secondHand = document.querySelector('.second-hand');
const minuteHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds();
const secondsDegrees = -90 + (seconds * 6);
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const minutes = now.getMinutes();
const minutesDegrees = -90 + (minutes * 360/60);
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
const hours = now.getHours();
const hoursDegrees = -90 + (hours * (360/12));
hoursDegrees = hoursDegrees + (0.5 * minutes);
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
}
setInterval(setDate, 1000);
现场示例(时钟为 HTML 和 CSS):
const secondHand = document.querySelector('.second-hand');
const minuteHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds();
const secondsDegrees = ((seconds / 60) * 360) + 90;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const minutes = now.getMinutes();
const minutesDegrees = ((minutes / 60) * 360) + 90;
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
const hours = now.getHours();
const hoursDegrees = ((hours / 60) * 360) + 90;
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
}
setInterval(setDate, 1000);
html {
background: #018DED url(http://unsplash.it/1500/1000?image=881&blur=50);
background-size: cover;
font-family: 'helvetica neue';
text-align: center;
font-size: 10px;
}
body {
margin: 0;
font-size: 2rem;
display: flex;
flex: 1;
min-height: 100vh;
align-items: center;
}
.clock {
width: 30rem;
height: 30rem;
border: 20px solid white;
border-radius: 50%;
margin: 50px auto;
position: relative;
padding: 2rem;
box-shadow: 0 0 0 4px rgba(0, 0, 0, 0.1), inset 0 0 0 3px #EFEFEF, inset 0 0 10px black, 0 0 10px rgba(0, 0, 0, 0.2);
}
.clock-face {
position: relative;
width: 100%;
height: 100%;
transform: translateY(-3px);
/* account for the height of the clock hands */
}
.hand {
width: 50%;
height: 6px;
position: absolute;
top: 50%;
transform-origin: 100%;
transform: rotate(90deg);
transition: all 0.05s;
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
}
.hour-hand,
.min-hand {
background: black;
}
.second-hand {
background: red;
}
<div class="clock">
<div class="clock-face">
<div class="hand hour-hand"></div>
<div class="hand min-hand"></div>
<div class="hand second-hand"></div>
</div>
</div>
问题是过渡。当秒数从 59 变为 0 时,您正在从 444 度转换回 90 度,因此当浏览器应用 CSS过渡。
如果您在从 59 度到 0 度(例如,回到 90 度)时禁用过渡,您将看不到它。请注意,这适用于所有手,而不仅仅是秒。
const secondHand = document.querySelector('.second-hand');
const minuteHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds();
const secondsDegrees = ((seconds / 60) * 360) + 90;
if (secondsDegrees == 90) {
secondHand.classList.add("no-transition");
} else {
secondHand.classList.remove("no-transition");
}
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const minutes = now.getMinutes();
const minutesDegrees = ((minutes / 60) * 360) + 90;
if (minutesDegrees == 90) {
minuteHand.classList.add("no-transition");
} else {
minuteHand.classList.remove("no-transition");
}
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
const hours = now.getHours();
const hoursDegrees = ((hours / 60) * 360) + 90;
if (hoursDegrees == 90) {
hourHand.classList.add("no-transition");
} else {
hourHand.classList.remove("no-transition");
}
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
}
setInterval(setDate, 1000);
html {
background: #018DED url(http://unsplash.it/1500/1000?image=881&blur=50);
background-size: cover;
font-family: 'helvetica neue';
text-align: center;
font-size: 10px;
}
body {
margin: 0;
font-size: 2rem;
display: flex;
flex: 1;
min-height: 100vh;
align-items: center;
}
.clock {
width: 30rem;
height: 30rem;
border: 20px solid white;
border-radius: 50%;
margin: 50px auto;
position: relative;
padding: 2rem;
box-shadow: 0 0 0 4px rgba(0, 0, 0, 0.1), inset 0 0 0 3px #EFEFEF, inset 0 0 10px black, 0 0 10px rgba(0, 0, 0, 0.2);
}
.clock-face {
position: relative;
width: 100%;
height: 100%;
transform: translateY(-3px);
/* account for the height of the clock hands */
}
.hand {
width: 50%;
height: 6px;
position: absolute;
top: 50%;
transform-origin: 100%;
transform: rotate(90deg);
transition: all 0.05s;
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
}
.no-transition {
transition: none;
}
.hour-hand,
.min-hand {
background: black;
}
.second-hand {
background: red;
}
<div class="clock">
<div class="clock-face">
<div class="hand hour-hand"></div>
<div class="hand min-hand"></div>
<div class="hand second-hand"></div>
</div>
</div>
我使用了这段代码并且它有效。只需将时钟秒度增加到大于 360 度即可正常工作。
var timeinterval=null;
var srotation=null;
var newrotation=null;
function clockfunc()
{
timeinterval=setInterval(clockdisp,1000);
}
function clockdisp()
{
var d=new Date();
var htime=d.getHours();
var mtime=d.getMinutes();
var stime=d.getSeconds();
var hrotation=30*htime+mtime/2;
var mrotation=6*mtime;
if(newrotation>=354)
{
newrotation=newrotation+6;
}
if(srotation<354 || srotation==null)
{
srotation=6*stime;
newrotation=srotation;
}
document.getElementById("hrhand").style.transform="rotate("+hrotation+ "deg)";
document.getElementById("mhand").style.transform="rotate("+mrotation+ "deg)";
document.getElementById("shand").style.transform="rotate("+newrotation+ "deg)";
}