admin管理员组文章数量:1279011
I have created a javascript function which takes two images from div style and displays it one picture first and the other picture after the specified time.
JavaScript
var imageID = 0;
function changeImage (every_seconds) {
if (!imageID) {
document.getElementById("myimage1");
imageID++;
}
if (imageID == 1) {
document.getElementById("myimage2");
}
}
//call same function again for x of seconds
setTimeout("changeImage(" + every_seconds + ")", ((every_seconds) * 50000000000));
}
HTML
<body style='background:black;' onload='changeimage(10)'>
<div style='position:absolute;width:100%;height:100%;left:0px;top:0px;' align='left'>
<img width='600px' height='500px' id='myimage1' src='.jpg'/>
</div>
<div style='position:absolute;width:100%;height:100%;left:0px;top:0px;' align='right'>
<img width='600px' height='500px' id='myimage2' src='.jpg'/>
</div>
</body>
Please can someone help how to get just a single image at a time and disappear the other one at the specified interval.
I have created a javascript function which takes two images from div style and displays it one picture first and the other picture after the specified time.
JavaScript
var imageID = 0;
function changeImage (every_seconds) {
if (!imageID) {
document.getElementById("myimage1");
imageID++;
}
if (imageID == 1) {
document.getElementById("myimage2");
}
}
//call same function again for x of seconds
setTimeout("changeImage(" + every_seconds + ")", ((every_seconds) * 50000000000));
}
HTML
<body style='background:black;' onload='changeimage(10)'>
<div style='position:absolute;width:100%;height:100%;left:0px;top:0px;' align='left'>
<img width='600px' height='500px' id='myimage1' src='http://www.photos.a-vsp./fotodb/14_green_cones.jpg'/>
</div>
<div style='position:absolute;width:100%;height:100%;left:0px;top:0px;' align='right'>
<img width='600px' height='500px' id='myimage2' src='http://www.hickerphoto./data/media/186/flower-bouquet-nice_12128.jpg'/>
</div>
</body>
Please can someone help how to get just a single image at a time and disappear the other one at the specified interval.
Share Improve this question edited Apr 8, 2015 at 21:05 royhowie 11.2k14 gold badges53 silver badges67 bronze badges asked Apr 7, 2015 at 4:33 deepdeep 7165 gold badges17 silver badges37 bronze badges4 Answers
Reset to default 5You can try the following if you want to try in vanilla JS without using library like JQuery
var imageID=0;
function changeImage(){
var img1 = document.getElementById("myimage1");
var img2 = document.getElementById("myimage2");
if(imageID %2 == 0){
img1.style.display = 'block';
img2.style.display = 'none';
}
else {
img2.style.display = 'block';
img1.style.display = 'none';
}
imageID++;
}
//call same function again for x of seconds
setInterval(changeImage, 1000);
<body style='background:black;'>
<div style='position:absolute;width:100%;height:100%;left:0px;top:0px;'>
<img width='600px' height='500px' id='myimage1' style='display:none' src='http://www.photos.a-vsp./fotodb/14_green_cones.jpg'/>
<img width='600px' height='500px' id='myimage2' style='display:none' src='http://www.hickerphoto./data/media/186/flower-bouquet-nice_12128.jpg'/>
</div>
</body>
There are two images placed inside the same container. Both of which are hidden when the page loads. You don't need the onload event handler in the body since the JS will get executed after the HTML loads. That time it will invoke the setInterval method that will change the image every 1 second.
You can hide the image at a specified time using jQuery setTimeout function,
Demo : http://jsfiddle/stanze/kdk6kkLv/
$(function() {
var hidden = $('.hidden');
setTimeout(function(){
$(hidden).hide()
}, 2000);
})
I suggest changing the HTML a bit by giving ids to the container divs too. Also let myDiv2 be hidden at first. We can then use a built-in function in javascript called setInterval()
to which we pass the function to execute as the first parameter and the time in milliseconds between two consecutive executions as the second parameter.
setInterval()
method calls a function repeatedly at specified intervals (in milliseconds).
<div id="myDiv1" style='position:absolute;width:100%;height:100%;left:0px;top:0px;' align='left'>
<img width='600px' height='500px' id='myimage1' src='http://www.photos.a-vsp./fotodb/14_green_cones.jpg'/>
</div>
<div id="myDiv2" style='display:none; position:absolute;width:100%;height:100%;left:0px;top:0px;' align='right'>
<img width='600px' height='500px' id='myimage2' src='http://www.hickerphoto./data/media/186/flower-bouquet-nice_12128.jpg'/>
</div>
Javascript:
setInterval(function() { //setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).
var div1 = document.getElementById("myDiv1");
var div2 = document.getElementById("myDiv2");
if(div2.display = '') { //This executes when div2 is visible (and div1 is hidden)
div1.display = ''; //This makes div1 (and myimage1) visible
div2.display = 'none'; //This hides div2 (and myimage2)
} else { //This executes when div1 is visible (and div2 is hidden)
div2.display = '';
div1.display = 'none';
}
}, 5000); //You can give time in milliseconds between each change instead of 5000 here. 5000ms = 5s.
You can look up setInterval()
here.
Image Slideshow using setinterval
for hide one and show another image.
You can try out below mentioned simple way:
window.onload = function() {
var image=document.getElementById("aaa");
var img_array=[...];
var index=0;
var interval = 2000;
function slide() {
image.src = img_array[index++%img_array.length];
}
setInterval(slide, interval);
}
Another option for the same:
Instead of setInterval(slide,2000);
use below code:
setTimeout(function() {
slide();
setTimeout(arguments.callee, interval)
}, interval);
本文标签: javascriptHow to show just one image at a time at a set intervalStack Overflow
版权声明:本文标题:javascript - How to show just one image at a time at a set interval? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741247777a2365213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论