/    Sign up×
Community /Pin to ProfileBookmark

Need Help making a "tabled Slide show" with 4 pics

My professor gave us this problem:

Create an html document named MidtermExam4.html. Add a table to this document with 2 rows
and 2 columns. Use the Internet or your own photo library to find 4 similarly sized photos and place one photo in
each of the 4 cells of the table. Write JavaScript code that will change the location of the each image to a different
table cell every 4 seconds. For example:
After 0 Seconds, you table would look like this:
Image 1 Image 2
Image 3 Image 4
After 4 Seconds:
Image 4 Image1
Image 2 Image 3

Anyway, I managed to get the pics on the html doc and I’m able to get the first pic to cycle through the others. But, the other pics just stay the same. Here’s the code I have so far:

<!DOCTYPE html
PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>

<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<title></title>
<meta http-equiv=”content-type” content=”text/html;charset=iso-8859-1″ />
<script type=”text/javascript”>
/* <![CDATA[*/
var curpic=”one”;
var changepics;
function changer() {
switch (curpic)
{
case “one”:
document.images[0].src= “firebird1.jpg”;
curpic = “four”;
break;
case “two”:
document.images[0].src= “firebird2.jpg”;
curpic = “one”;
break;
case “three”:
document.images[0].src= “firebird3.jpg”;
curpic = “two”;
break;
case “four”:
document.images[0].src= “firebird4.jpg”;
curpic = “three”;
break;

}

}
/* ]]> */
</script>
</head>

<body onload=”var changepics=setInterval(‘changer()’, 4000);”>

<table>
<tr>
<td><img src=”firebird1.jpg” width=”559″ height=”243″ />
<td><img src=”firebird2.jpg” width=”400″ height=”251″ />
</tr>
<tr>
<td><img src=”firebird3.jpg” width=”480″ height=”340″ />
<td><img src=”firebird4.jpg” width=”440″ height=”330″ />
</tr>
</table>
</body>
</html>

I don’t need any fancy styles or effects, I just need it to work the way as mentioned above. I searched all over the net and found nothing.

Any help you guys could give would be most appreciated. Thanks!

-Andrew

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 11.2010 — Try this modification ...
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;title&gt;Shifting Images&lt;/title&gt;
&lt;meta http-equiv="content-type" content="text/html;charset=iso-8859-1" /&gt;
&lt;script type="text/javascript"&gt;
// For: http://www.webdeveloper.com/forum/showthread.php?t=226173

/* &lt;![CDATA[*/
var curpic=0;
var changepics;

// var baseURL = '';
// var firebirdImgList = ['firebird1.jpg','firebird2.jpg','firebird3.jpg','firebird4.jpg'];
// substitue above for code below
var baseURL = 'http://www.nova.edu/hpd/otm/pics/4fun/';
var firebirdImgList = ['11.jpg','12.jpg','13.jpg','14.jpg'];

function changer() {
curpic = (curpic+1) % firebirdImgList.length;
for (var i=0; i&lt;firebirdImgList.length; i++) {
document.images[i].src= baseURL+firebirdImgList[curpic];
document.images[i].alt= firebirdImgList[curpic];
curpic = (curpic+1) % firebirdImgList.length;
}
}
/* ]]&gt; */
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="changer();changepics=setInterval('changer()', 4000);"&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="" alt="" width="440" height="340" /&gt;
&lt;td&gt;&lt;img src="" alt="" width="440" height="340" /&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="" alt="" width="440" height="340" /&gt;
&lt;td&gt;&lt;img src="" alt="" width="440" height="340" /&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;


BTW: Wrap your code between [ code] and [ /code] tags (without the spaces)

to make it easier for forum readers to view your scripts. See 'sticky' info at top of main forum page.

Good Luck!

?
Copy linkTweet thisAlerts:
@tirnaMar 12.2010 — I would have preloaded the images into an image object array as well but I'm not sure if you have covered that in your course yet, so I 'fixed' your JS using a similar method to your original.

Firstly, when you have multiple img tags on your page, they will be stored in an array in your browser. Since you have 4 img tags, you will have images[0], images[1], etc. Your original JS only uses images[0] which is partly why only the first image was cycling.

Also, I couldn't see where you were incrementing curpic and so it was alway set to "one"

I changed curpic to an integer and incremented it in each case block.

The following will cycle the images as you wanted.

BTW - I want 20% of the marks you will get for this :p


[CODE]
<script type="text/javascript">
var curpic=1;
var changepics;
function changer() {
switch (curpic)
{
case 1:
//alert(curpic);
document.images[1].src= "firebird1.jpg";
document.images[2].src= "firebird2.jpg";
document.images[3].src= "firebird3.jpg";
document.images[0].src= "firebird4.jpg";

//incremement the scenario counter
curpic = curpic + 1;
if(curpic > 4) curpic = 1;
break;

case 2:
//alert(curpic);
document.images[2].src= "firebird1.jpg";
document.images[3].src= "firebird2.jpg";
document.images[0].src= "firebird3.jpg";
document.images[1].src= "firebird4.jpg";

//incremement the scenario counter
curpic = curpic + 1;
if(curpic > 4) curpic = 1;
break;

case 3:
//alert(curpic);
document.images[3].src= "firebird1.jpg";
document.images[0].src= "firebird2.jpg";
document.images[1].src= "firebird3.jpg";
document.images[2].src= "firebird4.jpg";

//incremement the scenario counter
curpic = curpic + 1;
if(curpic > 4) curpic = 1;
break;

case 4:
//alert(curpic);
document.images[0].src= "firebird1.jpg";
document.images[1].src= "firebird2.jpg";
document.images[2].src= "firebird3.jpg";
document.images[3].src= "firebird4.jpg";

//incremement the scenario counter
curpic = curpic + 1;
if(curpic > 4) curpic = 1;
break;
}
}
</script>
[/CODE]
×

Success!

Help @truemuck spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...