自动更改为下一个图像
change to next image automatically
我有以下代码用于显示悬停时更改的几个图像,但我也想添加一个功能,如果我不手动更改图像,它会自动更改图像。
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title><br />
</head>
<body>
<p>
<script type="text/javascript" language="javascript">
function changeImage(img){
document.getElementById('bigImage').src=img;
}
</script>
<img src="../Pictures/lightcircle.png" alt="" width="284" height="156" id="bigImage"
/>
<p> </p>
<div>
<p>
<img src="../Pictures/lightcircle2.png" height="79" width="78"
onmouseover="changeImage('../Pictures/lightcircle2.png')"/>
</p>
<p><img src="../Pictures/lightcircle.png" alt="" width="120" height="100"
onmouseover="changeImage('../Pictures/lightcircle.png')"/></p>
<p><img src="../Pictures/lightcircle2.png" alt="" width="78" height="79"
onmouseover="changeImage('../Pictures/lightcircle2.png')"/></p>
<p> </p>
</br>
</div>
</body>
</html>
我想做的是最好使用javascript自动更改显示的图像。我该怎么做?
使用setInterval运行一个函数来更改图像src。
var x = 0;
var images = new Array("../Pictures/lightcircle2.png","../Pictures/lightcircle.png");
var i = setInterval(auto, 3000);
function auto()
{
x++;
if (x == images.length)
x=0;
document.getElementById('bigImage').src=images[x];
}
这应该会对您有所帮助:
http://www.switchonthecode.com/tutorials/javascript-tutorial-using-setinterval-and-settimeout
听起来你想要一个旋转木马。如果是,试试这个。
将此添加到您的JavaScript
// The list of images you want to cycle through
var imageRotation = [
'../Pictures/lightcircle.png',
'../Pictures/lightcircle2.png'
];
// The current image being displayed
var currentImage = 0;
// A variable to hold the timer
var t;
// Call this to automatically start rotation. Currently set for a 5 sec rotation
function startCarousel(){
t=setInterval(changeCarousel,5000);
}
// Moves to the next picture
function changeCarousel(){
// Change to the next image
currentImage++;
// If there isn't a next image, go back to the start
if (currentImage == imageRotation.length) currentImage = 0;
// Change the image
document.getElementById('bigImage').src=imageRotation[currentImage];
}
然后将您的功能修改为
function changeImage(img){
// Stops the rotation
clearInterval(t);
// Assigns currentImage to the image they selected
currentImage = imageRotation.indexOf(img);
// Swap the image
document.getElementById('bigImage').src=imageRotation[currentImage];
// Start the rotation again in 10 sec
setTimeout(startCarousel, 10000);
}
相关文章:
- 在引导程序旋转木马中显示下一个图像
- 使用JQuery的图像滑块不显示我选择的下一个图像,而是从头开始显示
- 在jquery中显示下一个图像的问题
- 向上一个/下一个图像幻灯片添加计数器(仅限javascript/css)
- Fancybox在第二次单击时响应,并且不会切换到下一个图像
- 如何使它转到上一个图像而不是下一个图像
- Javascript幻灯片选择“下一个”图像混淆
- 如何使用javascript显示上一个和下一个图像
- 单击jqueryhtml更改为下一个图像
- 自动更改为下一个图像
- 3秒后的下一个图像
- 下一个图像未淡入.上一个图像淡入,下一个仅出现
- Jquery插件图像滚动与前一个和下一个图像可见的一面
- JQuery不显示下一个图像
- 如何获得下一个图像存储在数据库中的php
- 寻找下一个图像
- 超时后如何更改下一个图像
- 如何使用jQuery淡入列表中的下一个图像
- 显示预览(前一个和下一个图像)与引导旋转木马
- 加载下一个图像一旦上一个已经完成