130 likes | 138 Views
Learn to manipulate web page images dynamically using the document.images attribute in JavaScript. Explore image referencing, updating, mouse event handling, pre-loading, and animation techniques.
E N D
Goals By the end of this lecture you should … • Understand that DOM tracks available images in a web page using the document.images attribute (for which JavaScript constructs an array object). • Understand how to use the document.images attribute to dynamically manipulate images on the user's screen, based on user-driven events.
The document.images Attribute • The document.images attribute represents an array of objects created in the current web page by the <img> tag. Each <img> occupies a subscript of the array, beginning with the first <img> tag. The subscript 0 represents it.
Referencing Images • We can reference images in a document by referring to their subscript in the images arrays:window.document.images[0] • We can also reference images using the value assigned to the html name/id attributes:window.document.images[“myImage1”]
Updating the src of an Image • To change an image, we can dynamically change the path assigned to its src attribute:window.document.images[0].src = “images/vacationImage02.jpg”
Mouse Event Handlers • onMouseOver – The user puts the mouse over an image/document region. • onMouseOut – The user takes the mouse away from an image/document region. • onMouseDown – The user clicks on a specific image/document region. • onMouseUp – The user releases the mouse button over a previously-clicked specific image/document region.
Take the next few minutes to examine the files called DocumentImages_01.htmlandDocumentImages_02.html
Pre-Loading Images • It’s considered good practice to pre-load images into a browser’s cache before accessing them for an image swap or animation. To do so, create an image object:var updateImage = new Image(); • Then, update the new image’s src attribute:updateImage.src = “images/image01.gif”;
Take the next few minutes to examine the files called DocumentImages_03.html
“Animating” Images • We can display a succession of images by continuously updating the src attribute using a function called by the window.setInterval() method. • Remember that the setInterval() method returns an interval object. We can then pass the name of the object to window.clearInterval() to stop animation.
Take the next few minutes to examine the files called DocumentImages_04.html
Summary • JavaScript tracks all web page images created using the <img> tag via a special attribute, document.images, which JavaScript represents as an Array. • We can pre-load individual images into memory by using the Image constructor. continued …
Summary • Each image has a src attribute, which represents the path of an image on the web server. • Various mouse events that JavaScript can track include onMouseOver, onMouseOut, onMouseDown and onMouseUp. • We can use the setInterval() and clearInterval()window methods to "animate" images.