gpt4 book ai didi

javascript - 在 javascript 中逐步显示 4 个图像

转载 作者:行者123 更新时间:2023-11-28 06:02:10 25 4
gpt4 key购买 nike

<!DOCTYPE html>
<html>
<head>
<style>
#steps img{
-webkit-transition: background-image 1.0s linear 0s;
transition: background-image 1.0s linear 0s;
float: left;
display: none;
height: 50px;
width: 50px;
}
</style>
<script>
var i = 0;
var elem;
function nextStep(){
i++;
var img = document.createElement("img");
img.src = "image"+i+".png"
img.width = "50px";
img.height = "50px";
img.id = "step" + i;
document.getElementById("steps").appendChild(img);
elem = document.getElementById(img.id);
elem.style.display = "inline-block";
elem.style.opacity = "0";
setTimeout(slide(),5000);
}
function slide(){
elem.style.opacity = "1";
if(i > 3){
clearTimeout(slide());
clearTimeout(nextStep());
}
setTimeout(nextStep(),5000);
}
</script>
</head>
<body>
<h1>Steps</h1>
<div id="steps">
<script>
nextStep();
</script>
</div>
<p id="text"></p>
</body>
</html>

我有 4 张图像,我想用 css3 效果逐步创建和显示它们,它首先执行 nextStep() 函数,该函数将 img 子对象创建到 div 并更改显示和不透明度。在调用 slide() 之后,不透明度设置为 1,我们再次调用 nextStep()。当 i > 3 时,我们停止显示。当我测试它时,它立即显示 4 个图像,没有任何效果

编辑:

<!DOCTYPE html>
<html>
<head>
<style>
#steps img{
/*
-webkit-transition: background-image 1.0s linear 0s;
transition: background-image 1.0s linear 0s;
*/
float: left;
display: none;
height: 50px;
width: 50px;
}
</style>
<script>
var i = 0;
var elem;
//var slide;
//var nextStep;
function nextStep(){
i++;
var img = document.createElement("img");
img.src = "image"+i+".png"
img.width = "50px";
img.height = "50px";
img.id = "step" + i;
document.getElementById("steps").appendChild(img);
elem = document.getElementById(img.id);
elem.style.display = "inline-block";
//elem.style.opacity = "0";
setTimeout(slide(),2000);
}
function slide(){
//elem.style.opacity = "1";
if(i < 3){
//clearTimeout(slide());
//clearTimeout(nextStep());
setTimeout(nextStep(),2000);
}

}
</script>
</head>
<body>
<h1>Steps</h1>
<div id="steps">
<script>
document.onload = function (){
nextStep();
};
</script>
</div>
<p id="text"></p>
</body>
</html>

最佳答案

在对 DOM 进行任何操作之前,您应该始终等待 DOM 完全加载。加载文档时使用 document.onload 执行脚本:

<script>
document.onload = function (){
nextSteps()
};
</script>

编辑:您的代码存在更多问题。这里不需要 clearTimeout 函数。另外,你没有正确使用它。引自 w3schools 的文档:

The clearTimeout() method clears a timer set with the setTimeout() method.

The ID value returned by setTimeout() is used as the parameter for the clearTimeout() method.

Note: To be able to use the clearTimeout() method, you must use a global variable when creating the timeout method:

myVar = setTimeout("javascript function",milliseconds); Then, if the function has not already been executed, you will be able to stop the execution by calling the clearTimeout() method.

您的幻灯片功能应如下所示:

function slide(){
elem.style.opacity = "1";
if(i < 3){
setTimeout(nextStep,5000);
}
}

编辑 2:

setTimeout 需要对函数的引用,例如不带括号的函数名称:

setTimeout(nextStep,5000);

nextStep 是对函数的引用
nextStep()是函数执行后返回的结果(本例未定义)

关于javascript - 在 javascript 中逐步显示 4 个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36688172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com