使用 JavaScript 在 5 秒内更改 5 张图像
Changing 5 images in 5 seconds each using JavaScript
我是 JavaScript 的新手,我不明白我应该如何在 5 个不同的 gif 图像之间更改。分配如下:
第 11 章的项目文件夹包含音乐会系列(音乐会 1.gif 到 音乐会5.gif的五个广告图片。创建一个脚本,该脚本循环浏览图像并显示每个图像五秒钟。将文档另存为 ConcertAds.html。
这是我到目前为止的代码:
<!DOCTYPE HTML>
<html>
<head>
<title>Concert Ads</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
/* <![CDATA[ */
var concertads = new Array(5);
concertads[0] = new Image();
concertads[1] = new Image();
concertads[2] = new Image();
concertads[3] = new Image();
concertads[4] = new Image();
concertads[0].src = "concert1.gif";
concertads[1].src = "concert2.gif";
concertads[2].src = "concert3.gif";
concertads[3].src = "concert4.gif";
concertads[4].src = "concert5.gif";
var completeAds;
var currImgIndex = 0;
var maxImgIndex = concertads.length-1;
function concertAd() {
if (completeAds) {
completeAds.src = concertads[0].src;
++currImgIndex;
if (currImgIndex > maxImgIndex) {
currImgIndex = 0;
}
}
}
function initImgCycle(adsID) {
completeAds = document.getElementByID(adsID)
if (completeAds) {
setInterval("cycle()", 5000);
}
}
/* ]]> */
</script>
</head>
<body onload="initImgCycle('adBanner');">
<img src="concert1.gif" id="adBanner" alt="Concert Ads" />
</body>
</html>
我将不胜感激任何帮助,我真的很困惑。谢谢你的时间!
imgCounter=0;
function cycle(){
if(imgCounter<5){
imgCounter++;
document.getElementById("imgX").src="concert" + imgCounter + ".gif";
setTimeout("cycle();", 5000);
}
}
cycle();
一定要有一个
<img id="imgX" src="" alt="img"/>
或 HTML 中的类似内容
目前尚不清楚是希望它们同时显示 5 秒,还是希望它们一个接一个地显示 5 秒。我选择了第一个。
(function() {
var images = [
new Image(), new Image(), new Image(), new Image(), new Image()
];
images[0].src = 'http://bit.ly/sbK2Ub';
images[1].src = 'http://bit.ly/lMMo0n';
images[2].src = 'http://bit.ly/LFtGyc';
images[3].src = 'http://bit.ly/MY3skL';
images[4].src = 'http://bit.ly/M8iQk3';
function show( elem ) {
document.body.appendChild( elem );
}
function remove( elem ) {
document.body.removeChild( elem );
}
window.onload = function() {
images.forEach(function(a, b) {
show( images[b] );
});
window.setTimeout(function() {
images.forEach(function(a, b) {
remove( images[b] );
});
}, 5000);
};
})();
以下是使用不同图像的演示(单击"渲染"以运行代码): http://jsbin.com/akoqek/2/edit
所以它会像下面这样:
`<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<title>Goselin Greeting cards</title>
<script type="text/javascript">
/* <![CDATA[ */
imgCounter=0;
function cycle(){
if(imgCounter<5){
imgCounter++;
document.getElementById("imgX").src="concert" + imgCounter + ".gif";
setTimeout("cycle();", 5000);
}
}
/* ]]> */
</script>
</head>
<body onload="initImgCycle('adBanner');">
<h1>Consert Series</h1>
<img id="imgX" src="" alt="img"/>
</body>
</html>
我试图弄清楚同样的事情。
相关文章:
- jQuery图像幻灯片,在停止后淡出到第一张图像
- 在两整页上打印两张图像
- 单击从一张图像切换到两张图像,然后再切换回一张图像
- 在javascript中逐步显示4张图像
- 单击另一张图像,显示一张图像几秒钟
- Jquery渐变图像与多张幻灯片的惰性加载程序
- 粘性滚动显示两张图像
- 三.js:在立方体贴图中仅更改一张脸的图像
- 悬停时,图像从另一张图像后面滑出
- 用 HTML 中的一行连接 2 张图像.姜戈模板
- 如何停止幻灯片放映隐藏文本和图像跳到顶部,而下一张幻灯片进来
- 从列表中选择 4 张图像
- JavaScript 使用 url 上传图像(图像已经在互联网上,不同的域)
- 使用JavaScript更改背景图像图像以进行图像幻灯片
- 将大约 300 张画布图像保存在一个数组中并发送一次进行处理,在 chrome 中崩溃,但在 Firefox 中工作正常
- 上传前无法显示图像图像
- 单击上一张图像,显示一张新图像
- 编辑图像/图像详细信息-替换按钮缺失
- 在缩略图上更改滑块图像.图像改变一秒钟,然后恢复
- 在jQuery中移动图像(图像裁剪)