15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 1 ✦ Generic HTML Element Objects (Chapter 15)<br />

IE4+ and NN6+ simplify the implementation of this kind of three-state image button<br />

by allowing you to assign the event handlers directly to IMG element objects.<br />

Wrapping images inside links is a backward compatibility approach that allows older<br />

browsers to respond to clicks on images for navigation or other scripting tasks.<br />

onMouseEnter<br />

onMouseLeave<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓<br />

Example<br />

You can modify Listing 15-43 with the IE5.5 syntax by substituting onMouseEnter<br />

for onMouseOver and onMouseLeave for onMouseOut. The effect is the same.<br />

onMouseMove<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility (✓) ✓ ✓ ✓ ✓<br />

Example<br />

Listing 15-43 is a simplified example of dragging elements in IE4+. (See Chapter<br />

31 of the <strong>JavaScript</strong> <strong>Bible</strong> for more dragging examples.) Three images are individually<br />

positioned on the page. Most of the scripting code concerns itself with the<br />

geography of click locations, the stacking order of the images, and the management<br />

of the onMouseMove event handler so that it is active only when an item is dragged.<br />

Scripts assign the onMouseDown and onMouseUp event handlers to the document<br />

object, invoking the engage() and release() functions, respectively. When a user<br />

mouses down anywhere in the document, the engage() function starts by invoking<br />

setSelectedObj(). This function examines the target of the mouseDown event. If it<br />

is one of the map images, the selectedObj global variable is set to the image<br />

object and the element is brought to the front of the stacking order of images (any<br />

previously stacked image is returned to its normal position in the stack).<br />

MouseDown events on any other element simply make sure that the selectedObj<br />

variable is null. The presence of a value assigned to selectedObj serves as a kind<br />

of switch for other functions: When the variable contains a value, it means that the<br />

user is doing something associated with dragging an element.<br />

Back at the engage() function — provided the user mouses down on one of the<br />

draggable images — the onMouseMove event handler is assigned to the document<br />

object, setting it to invoke the dragIt() function. For the sake of users, the offset<br />

of the mouse down event from the top-left corner of the image is preserved in the<br />

117<br />

elementObject.onMouseMove

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!