我正在使用javascript创建一个图像幻灯片,但代码不起作用
I am using javascript to create an image slideshow, but the code is not working
我正在使用javascript创建一个图像幻灯片,但没有显示任何图像。
我做错了什么?
window.onLoad(
window.setInterval(function() {
var source = ["niepic1.jpg", "niepic.png"];
var count = 0;
document.getElementById("cover_main").style.backgroundImage = url(source[count]);
if (count >= 2) {
count = 0;
} else {
count++;
}
}, 5000);
);
#cover_main {
background-image: url("");
height: 45%;
background-size: 100% 100%;
width:100%;
height:480px;
}
#cover_main_inside {
background-color:#A8A8A8 ;
opacity: 0.76;
margin-left: 75%;
margin-top: 2%;
}
<div class="row" id="cover_main">
<div class="col-3" style=" color:black; background-color:; border-radius:15px;" id="cover_main_inside">
<form class="form" action="pages/main/aboutteam.html" method="POST">
<h2 style="margin-left:40px;">Log In:</h2>
E-mail Address:<br>
<input id="name" type="email" name="email" placeholder="name@nie.ac.in" required><br><br>
Password:<br>
<input type="password" name="password" placeholder="********" required><br><br>
<input class="input-button" type="submit" name="submit" value="Let Me In">
</form><br>
<a href="pages/main/profile.html" style="margin-left:35px;">Forgot Password?</a>
</div>
</div>
编写的代码有几个问题——首先,我认为您没有正确调用window.onload
。此外,将count
变量移出setInterval
调用,否则在设置backgroundImage
时它将始终为0。您也不需要重新设置source
数组,这样就可以从setInterval
中取出。最后,源数组中只有2个元素,如果计数大于1,则可以重置计数,因为JavaScript数组是0索引的。
var count = 0;
var source = ["niepic1.jpg","niepic.png"];
window.onload = function () {
window.setInterval(function() {
document.getElementById("cover_main").style.backgroundImage = "url('" + source[count] + "')";
if (count >= 1)
{
count = 0;
}
else
{
count++;
}
}, 5000);
}
在编写时在代码中
document.getElementById("cover_main").style.backgroundImage = url(source[count]);
这里你指的是url
函数的url
变量或更多变量,它不存在并破坏了你的代码,所以使用
document.getElementById("cover_main").style.backgroundImage = 'url("'+source[count]+'")';
var count = 0;
var source = ["niepic1.jpg", "niepic.png"];
setInterval(function() {
document.getElementById("cover_main").style.backgroundImage = 'url("'+source[count]+'")';
document.getElementById("cover_main").innerHTML = "current background-image style is "+document.getElementById("cover_main").style.backgroundImage;
count = (count >= 1)? 0 : ++count;
}, 1000);
#cover_main {
background-image: url("");
height: 45%;
background-size: 100% 100%;
width:100%;
height:480px;
}
#cover_main_inside {
background-color:#A8A8A8 ;
opacity: 0.76;
margin-left: 75%;
margin-top: 2%;
}
<div class="row" id="cover_main">
<div class="col-3" style=" color:black; background-color:; border-radius:15px;" id="cover_main_inside">
<form class="form" action="pages/main/aboutteam.html" method="POST">
<h2 style="margin-left:40px;">Log In:</h2>
E-mail Address:<br>
<input id="name" type="email" name="email" placeholder="name@nie.ac.in" required><br><br>
Password:<br>
<input type="password" name="password" placeholder="********" required><br><br>
<input class="input-button" type="submit" name="submit" value="Let Me In">
</form><br>
<a href="pages/main/profile.html" style="margin-left:35px;">Forgot Password?</a>
</div>
</div>
相关文章:
- 如何在鼠标悬停时在另一个图像上滑动图像.
- 如何将多个画布保存为一个图像
- 是否可以仅通过jQuery将图像替换为用户动态选择的另一个图像
- Javascript/jQuery压缩一个图像正确的评论帖子,并使用Ajax Laravel 5.2保存到控制器中
- 如何在设定的时间间隔内一次只显示一个图像
- 如何在另一个html文件的框架中包含图像
- 循环以检查数组中的最后一个图像
- 下载Div&画布为一个图像
- 使用D3.js和GeoJson在地图上的某些坐标上设置一个小图像
- 当页面包含指向一个图像的多个链接时,fancyBox会复制缩略图辅助图像
- PNG图像被覆盖,但每个图像都有一个链接可以更改,每次一个-Javascript/jQuery/CSS
- 如何从 javascript 中可能具有一个或多个图像的字符串中仅获取图像名称
- 在引导程序旋转木马中显示下一个图像
- 拉斐尔用一个图像填满了这个圆圈
- 一个javascript实现base64图像编码并将结果写入文本文件
- 当我点击MVC视图中的图像时,如何打开另一个页面
- 创建,显示图像作为背景图像一个接一个- JavaScript
- 添加图像一个接一个的元素点击
- 将图像一个叠在另一个上面
- 将图像一个堆叠到另一个(javascript)