我正在寻找在秒数框扩展之间的时间延迟,然后在几秒钟之后,框阴影。我尝试过使用setInterval,但没有好处。还有其他干净的方法吗?
setInterval(clock,1000);
function clock(){
var d = new Date();
document.getElementById("hour").innerHTML = "Hours: " + d.getHours();
document.getElementById("minutes").innerHTML = "Minutes: " + d.getMinutes();
document.getElementById("seconds").innerHTML = "Seconds: " + d.getSeconds();
}
function myfunction(){
var object = document.getElementById("seconds");
object.style.padding = "100px";
object.style.boxShadow = "0px 0px 15px black";
}

div{
position:absolute;
left:100px;
top:100px;
text-align:center;
border:5px solid grey;
padding:15px;
margin:0;
transition:0.3s;
}
div:hover{
box-shadow:0px 5px 5px black;
}
p:nth-child(2){
width:100px;
border:1px solid black;
background-color:lightyellow;
}
p:nth-child(3){
width:100px;
border:1px solid black;
background-color:lightblue;
}
p:nth-child(4){
width:100px;
border:1px solid black;
background-color:lightgreen;
transition:.5s;
}

<button onclick="myfunction()">Change Seconds background settings</button>
<div>
<p>Casio</p>
<p id="hour"></p>
<p id="minutes"></p>
<p id="seconds"></p>
</div>
&#13;
答案 0 :(得分:1)
您可以通过为transition
属性添加多个属性来实现此目的,并在您的情况下为box-shadow
属性设置延迟,此处为1s
transition: padding .5s, box-shadow .5s 1s;
Stack snippet
setInterval(clock, 1000);
function clock() {
var d = new Date();
document.getElementById("hour").innerHTML = "Hours: " + d.getHours();
document.getElementById("minutes").innerHTML = "Minutes: " + d.getMinutes();
document.getElementById("seconds").innerHTML = "Seconds: " + d.getSeconds();
}
function myfunction() {
var object = document.getElementById("seconds");
object.style.padding = "100px";
object.style.boxShadow = "0px 0px 15px black";
}
div {
position: absolute;
left: 100px;
top: 100px;
text-align: center;
border: 5px solid grey;
padding: 15px;
margin: 0;
transition: 0.3s;
}
div:hover {
box-shadow: 0px 5px 5px black;
}
p:nth-child(2) {
width: 100px;
border: 1px solid black;
background-color: lightyellow;
}
p:nth-child(3) {
width: 100px;
border: 1px solid black;
background-color: lightblue;
}
p:nth-child(4) {
width: 100px;
border: 1px solid black;
background-color: lightgreen;
transition: padding .5s, box-shadow .5s 1s;
}
<button onclick="myfunction()">Change Seconds background settings</button>
<div>
<p>Casio</p>
<p id="hour"></p>
<p id="minutes"></p>
<p id="seconds"></p>
</div>
根据评论,以下是如何使用setTimeout
进行延迟
setInterval(clock, 1000);
function clock() {
var d = new Date();
document.getElementById("hour").innerHTML = "Hours: " + d.getHours();
document.getElementById("minutes").innerHTML = "Minutes: " + d.getMinutes();
document.getElementById("seconds").innerHTML = "Seconds: " + d.getSeconds();
}
function myfunction() {
var object = document.getElementById("seconds");
object.style.padding = "100px";
setTimeout(function() {
object.style.boxShadow = "0px 0px 15px black";
}, 1000);
}
div {
position: absolute;
left: 100px;
top: 100px;
text-align: center;
border: 5px solid grey;
padding: 15px;
margin: 0;
transition: 0.3s;
}
div:hover {
box-shadow: 0px 5px 5px black;
}
p:nth-child(2) {
width: 100px;
border: 1px solid black;
background-color: lightyellow;
}
p:nth-child(3) {
width: 100px;
border: 1px solid black;
background-color: lightblue;
}
p:nth-child(4) {
width: 100px;
border: 1px solid black;
background-color: lightgreen;
transition: .5s;
}
<button onclick="myfunction()">Change Seconds background settings</button>
<div>
<p>Casio</p>
<p id="hour"></p>
<p id="minutes"></p>
<p id="seconds"></p>
</div>