Forum Moderators: open
...
<script type="text/javascript">
<!--
var myImages = new Array("images/test1.jpg", "images/test2.jpg", "images/test3.jpg");
randomImage = Math.floor(Math.random()*myImages.length);
function displayImages()
{
document.write('<img src="'+myImages[randomImage]+'">');
if (randomImage < 3)
randomImage++;
else
randomImage=0;
}
//call function "displayImages()" every 2.5 seconds
setTimeout("displayImages()",2500)
}
//-->
</script>
</head>
<body id="page_body" onload="displayImages()">
...
Math.floor(Math.random()*myImages.length); to calculate a random number from 0 to <3 and copy it into a variable and then the function will display the images and if the index of the array is <3 then I increment it otherwise it starts from 0 again. then the setTImeout()will call the function every2.5 secs.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<base href="http://www.antobbo.webspace.virginmedia.com/photography/moretesting/">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="en">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Photography Home</title>
<link rel="stylesheet" type="text/css" href="stylesheets/containers.css">
<link rel="stylesheet" type="text/css" href="stylesheets/navigations.css">
<style type="text/css">
.picture_box_home {
width:708px;
min-height:inherit;
padding:4px;
background-color:#fff;
}
#pic {
display:block;
width:700px;
height:525px;
border:4px solid #7d003a;
}
</style>
<script type="text/javascript">
var myImages=['images/test1.jpg','images/test2.jpg','images/test3.jpg'];
var delay=2500; //call function "displayImages()" every 2.5 seconds
var temp;
function displayImages() {
var randomNumber=Math.floor(Math.random()*myImages.length);
if(randomNumber==temp) {
return displayImages();
}
temp=randomNumber;
document.getElementById('pic').src=myImages[randomNumber];
setTimeout(function(){displayImages()},delay)
}
window.addEventListener?
window.addEventListener('load',displayImages,false):
window.attachEvent('onload',displayImages);
</script>
</head>
<body id="page_body">
<div class="wrapper"> <!-- MAIN CONTAINER -->
<div class="wrap_content"> <!-- BOX FOR CONTENT-->
<div class="banner"> <!--BOX FOR BANNER -->
</div> <!--END OF BOX FOR BANNER -->
<h1>AB Photography</h1>
<div class="navigation_menu"> <!-- TOP NAVIGATION BOX-->
<ul>
<li class="button1"><a href="#">Test1</a></li>
<li class="button2"><a href="#">Test2</a></li>
<li class="button3"><a href="#">Test3</a></li>
<li class="button4"><a href="#">Test4</a></li>
</ul>
</div><!-- END OF TOP NAVIGATION BOX-->
<div class="picture_box_home"><!-- PICTURE BOX-->
<img id="pic" src="images/test1.jpg" alt="">
</div><!-- END OF PICTURE BOX-->
<p><br><br></p>
</div> <!-- END OF BOX FOR CONTENT-->
</div><!-- END OF MAIN CONTAINER "WRAPPER"-->
</body>
</html>
<base href="http://www.antobbo.webspace.virginmedia.com/photography/moretesting/">
<style type="text/css">
.picture_box_home {
width:708px;
min-height:inherit;
padding:4px;
background-color:#fff;
}
#pic {
display:block;
width:700px;
height:525px;
border:4px solid #7d003a;
}
</style>
var myImages=['images/test1.jpg','images/test2.jpg','images/test3.jpg']; function displayImages() {
var randomNumber=Math.floor(Math.random()*myImages.length);
if(randomNumber==temp) {
return displayImages();
} document.getElementById('pic').src=myImages[randomNumber]; setTimeout(function(){displayImages()},delay)
}
window.addEventListener?
window.addEventListener('load',displayImages,false):
window.attachEvent('onload',displayImages);
.picture_box_home {
width:708px;
min-height:inherit;
padding:4px;
background-color:#fff;
}
.picture_box_home {
clear:both;
border:7px solid #7d003a;
width:716px;
min-height:541px;
margin:5px auto;
position:relative;
}
.picture_box_home {
clear:both;
border:7px solid #7d003a;
width:708px;
padding:4px;
margin:5px auto;
background-color:#fff;
}
#pic {
display:block;
width:700px;
height:525px;
border:4px solid #7d003a;
}
var myImages=['images/test1.jpg','images/test2.jpg','images/test3.jpg'];
javascript Arrays using Literal Notation [google.com]
var randomNumber=Math.floor(Math.random()*myImages.length);
if(randomNumber==temp) {
return displayImages();
}
temp=randomNumber;
document.getElementById('pic').src=myImages[randomNumber];
<div class="picture_box_home"><!-- PICTURE BOX-->
<img id="pic" src="images/test1.jpg" alt="">
</div><!-- END OF PICTURE BOX-->
window.addEventListener?
window.addEventListener('load',displayImages,false):
window.attachEvent('onload',displayImages);
<body id="page_body" onload="displayImages()">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<base href="http://www.antobbo.webspace.virginmedia.com/photography/moretesting/">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="en">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Photography Home</title>
<link rel="stylesheet" type="text/css" href="stylesheets/containers.css">
<link rel="stylesheet" type="text/css" href="stylesheets/navigations.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style type="text/css">
.picture_box_home {
width:708px;
min-height:inherit;
padding:4px;
background-color:#fff;
}
#pic {
display:block;
width:700px;
height:525px;
border:4px solid #7d003a;
}
</style>
<script type="text/javascript">
var myImages=['images/test1.jpg','images/test2.jpg','images/test3.jpg'];
var delay=4000; //call function "displayImages()" every seconds
var temp;
function displayImages() {
var randomNumber=Math.floor(Math.random()*myImages.length);
if(randomNumber==temp) {
return displayImages();
}
temp=randomNumber;
$("#pic").fadeOut(4000);
document.getElementById('pic').src=myImages[randomNumber];
$("#pic").fadeIn(4000);
setTimeout("displayImages()",delay)
}
</script>
</head>
<body id="page_body" onload="displayImages()">
<div class="wrapper"> <!-- MAIN CONTAINER -->
<div class="wrap_content"> <!-- BOX FOR CONTENT-->
<div class="banner"> <!--BOX FOR BANNER -->
</div> <!--END OF BOX FOR BANNER -->
<h1>AB Photography</h1>
<div class="navigation_menu"> <!-- TOP NAVIGATION BOX-->
<ul>
<li class="button1"><a href="#">Test1</a></li>
<li class="button2"><a href="#">Test2</a></li>
<li class="button3"><a href="#">Test3</a></li>
<li class="button4"><a href="#">Test4</a></li>
</ul>
</div><!-- END OF TOP NAVIGATION BOX-->
<div class="picture_box_home"><!-- PICTURE BOX-->
<img id="pic" src="images/test1.jpg" alt="">
</div><!-- END OF PICTURE BOX-->
<p><br><br></p>
</div> <!-- END OF BOX FOR CONTENT-->
</div><!-- END OF MAIN CONTAINER "WRAPPER"-->
</body>
</html>
so how is possible that the frames they are supposed to represent are still coming up in the browser?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<base href="http://www.antobbo.webspace.virginmedia.com/photography/moretesting/">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="en">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Photography Home</title>
<link rel="stylesheet" type="text/css" href="stylesheets/containers.css">
<link rel="stylesheet" type="text/css" href="stylesheets/navigations.css">
<style type="text/css">
.picture_box_home {
width:708px;
min-height:inherit;
height:533px;
padding:4px;
background-color:#fff;
}
#pic {
display:block;
width:700px;
height:525px;
border:4px solid #7d003a;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var preloads=[];
function preload(){
for(var i=0;i<arguments.length;i++) {
preloads[preloads.length]=new Image();
preloads[preloads.length-1].src=arguments[i];
}
}
preload('images/test1.jpg','images/test2.jpg','images/test3.jpg');
var delay=4000; //call function "displayImages()" 4 every seconds
var temp;
var test=true;
var randomNumber;
function displayImages() {
test==true?(test=false,out()):(test=true,ink());
}
function out(){
setTimeout(function(){displayImages()},delay);
$("#pic").fadeOut(delay)
}
function ink(){
randomNumber=Math.floor(Math.random()*preloads.length);
if(randomNumber==temp) {
return ink();
}
temp=randomNumber;
document.getElementById('pic').src=preloads[randomNumber].src;
setTimeout(function(){displayImages()},delay);
$("#pic").fadeIn(delay)
}
window.addEventListener?
window.addEventListener('load',displayImages,false):
window.attachEvent('onload',displayImages);
</script>
</head>
<body id="page_body">
<div class="wrapper"> <!-- MAIN CONTAINER -->
<div class="wrap_content"> <!-- BOX FOR CONTENT-->
<div class="banner"> <!--BOX FOR BANNER -->
</div> <!--END OF BOX FOR BANNER -->
<h1 id="foo">AB Photography</h1>
<div class="navigation_menu"> <!-- TOP NAVIGATION BOX-->
<ul>
<li class="button1"><a href="#">Test1</a></li>
<li class="button2"><a href="#">Test2</a></li>
<li class="button3"><a href="#">Test3</a></li>
<li class="button4"><a href="#">Test4</a></li>
</ul>
</div><!-- END OF TOP NAVIGATION BOX-->
<div class="picture_box_home"><!-- PICTURE BOX-->
<img id="pic" src="images/test1.jpg" alt="">
</div><!-- END OF PICTURE BOX-->
<p><br><br></p>
</div> <!-- END OF BOX FOR CONTENT-->
</div><!-- END OF MAIN CONTAINER "WRAPPER"-->
</body>
</html>
function displayImages() {
test==true?(test=false,out()):(test=true,ink());
}
var preloads=[];
function preload(){
for(var i=0;i<arguments.length;i++) {
preloads[preloads.length]=new Image();
preloads[preloads.length-1].src=arguments[i];
}
}
preload('images/test1.jpg','images/test2.jpg','images/test3.jpg');
var delay=4000; //call function "displayImages()" 4 every seconds
var temp;
var test=true;
var randomNumber;
execute instead?) and since we said that test==true then this function ink(){
randomNumber=Math.floor(Math.random()*preloads.length);
if(randomNumber==temp) {
return ink();
}
temp=randomNumber;
document.getElementById('pic').src=preloads[randomNumber].src;
setTimeout(function(){displayImages()},delay);
$("#pic").fadeIn(delay)
}
The function preload() runs before the page loads.
I added it to the script to ensure smooth transition in the swapping of the images.
This...
test==true?(test=false,out()):(test=true,ink());
...is shorthand for...
if(test==true) {
test=false;
out();
}
else {
if(test==false) {
test=true;
ink();
}
...and alternates functions out() and ink() - this was originally in(), but "in"
appears to be a javascript reserved word, so I just added a random letter. ;)
No problem, you're very welcome. ;)