In this exercise we'll use javascript to create a simple slide show that runs forward and backward.
<div id="sideshow">
<h3>My Slide Show</h3>
<p><img src="images/boat1.jpg" id="myPicture" width="200" height="200" alt="Slideshow" />
</p>
<p><a href="previous.html" id="prevLink"><< Previous</a> <a href="next.html" id="nextLink">Next >></a></p>
</div>
Obviously you are free to modify the design and add styles. However, leave the id names "myPicture," "prevLink," and "nextLink" alone—we will be referring to them with the javascript.
<script language="javascript" type="text/javascript">
</script>
window.onload = initLinks;
var myPix = new Array("images/boat1.jpg","images/falls.jpg"," images/hoist.jpg","images/jayCook.jpg","images/marshGrass.jpg", "images/nokomis.jpg","images/pond.jpg");
var thisPic = 0;
First we execute a function called "initLinks" which we will write in the next step. Then we set up a variable called "myPix" and assign the contents of an array (which contains the names of my pictures) to it. A second variable called "thisPic" is given an initial value of zero.
function initLinks() {
document.getElementById("prevLink").onclick = processPrevious;
document.getElementById("nextLink").onclick = processNext;
}
This function uses the DOM to find the "previous" and "next" links in the html code and tells the browser to execute the corresponding function should either be clicked.
function processPrevious() {
if (thisPic == 0) {
thisPic = myPix.length;
}
thisPic--;
document.getElementById("myPicture").src = myPix[thisPic];
return false;
}
function processNext() {
thisPic++;
if (thisPic == myPix.length) {
thisPic = 0;
}
document.getElementById("myPicture").src = myPix[thisPic];
return false;
}
4 pts |
Portfolio quality design. |
3 pts |
Good looking; above average work. |
2 pts |
It works, but it's nothing fancy. |
1 pts |
Something's not working right. |
0 pts |
Poor showing; mostly incomplete or full of errors. |