Allen Pitts
asked on
Javascript change images with button
Good afternoon expert,
Created image gallery. Would like to have buttons cycle through images .
Found this script.
Cant get it ro work. Cant see how the image file_number is being passed
to changeImage function.
There are four images named 'image1.jpg, image2.jpg, image3.jpg, and image4.jpg,
Thanks
Allen in Dallas
++++++++++++code begin++++++++++++
<html>
<head>
<title>Image Change</title>
<script language="JavaScript"<!--
ImageNames = new Object();
ImageNames.length = 3; //Because arrays start at 0, the length is one
//less than the number of images.
for (counter = 0; counter < 4; counter++) {
file_number = counter + 1;
file_name = ("image" + file_number + ".jpg");
ImageNames[counter] = file_name;
}
//--></script>
<script language="JavaScript"><!--
which_image_loaded = 0;
function changeImage(direction) {
which_image_loaded += direction;
if (which_image_loaded < 0)
which_image_loaded = 3; //Again, one less than the actual number of images.
if (which_image_loaded > 3)
which_image_loaded = 0;
if (document.images)
document.myimage.src = ImageNames[which_image_loa ded];
}
//--></script>
</head>
<body>
<!--http://www.irt.org/articles/js132/-->
<img src="image1.jpg" name="myimage">
<form>
<input type="button" value="<<" onClick='changeImage(-1);' >
<input type="button" value=">>" onClick='changeImage(1);'>
</form>
</body>
</html>
++++++++code end+++++++++++++++++++++++ ++
Created image gallery. Would like to have buttons cycle through images .
Found this script.
Cant get it ro work. Cant see how the image file_number is being passed
to changeImage function.
There are four images named 'image1.jpg, image2.jpg, image3.jpg, and image4.jpg,
Thanks
Allen in Dallas
++++++++++++code begin++++++++++++
<html>
<head>
<title>Image Change</title>
<script language="JavaScript"<!--
ImageNames = new Object();
ImageNames.length = 3; //Because arrays start at 0, the length is one
//less than the number of images.
for (counter = 0; counter < 4; counter++) {
file_number = counter + 1;
file_name = ("image" + file_number + ".jpg");
ImageNames[counter] = file_name;
}
//--></script>
<script language="JavaScript"><!--
which_image_loaded = 0;
function changeImage(direction) {
which_image_loaded += direction;
if (which_image_loaded < 0)
which_image_loaded = 3; //Again, one less than the actual number of images.
if (which_image_loaded > 3)
which_image_loaded = 0;
if (document.images)
document.myimage.src = ImageNames[which_image_loa
}
//--></script>
</head>
<body>
<!--http://www.irt.org/articles/js132/-->
<img src="image1.jpg" name="myimage">
<form>
<input type="button" value="<<" onClick='changeImage(-1);'
<input type="button" value=">>" onClick='changeImage(1);'>
</form>
</body>
</html>
++++++++code end+++++++++++++++++++++++
source of the page :
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script type='text/javascript' src='/js/lib/dummy.js'></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css">
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type='text/css'>
</style>
<script type='text/javascript'>//<![CDATA[
var ImageNames = [
"http://i.imgur.com/RANDv.gif",
"http://www.deviantart.com/download/322227392/toastie_gangnam_style_by_chocomilkterrorist-d5bug8w.gif",
"http://www.deviantart.com/download/321169183/shadow_gangnam_style_by_chocomilkterrorist-d5b7rq7.gif",
"http://img.photobucket.com/albums/v128/jeremyh/pony_gangnam_style_by_1n33d4hug-d5arai9.gif"
];
var currentIndex = 0;
window.onload = function() {
document.getElementById("next").onclick = new Function("changeImage(+1)");
document.getElementById("prev").onclick = new Function("changeImage(-1)");
changeImage(0);
}
function changeImage(n) {
if(n>0&&(currentIndex+n<ImageNames.length)) currentIndex++;
else if(n<0&¤tIndex>0) currentIndex--;
document.getElementById("myimage").setAttribute("src", ImageNames[currentIndex]);
}
//]]>
</script>
</head>
<body>
<img id="myimage">
<form>
<input type="button" value="<<" id="prev" />
<input type="button" value=">>" id="next" />
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window