spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / dhtml / column1
Developer News
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next
Logo

New JavaScript Mouse Events:
using onMouseDown and onMouseUp for dynamic buttons

Pass your mouse pointer over the round navigation buttons on the top right. This rollover effect is, of course, created with the onMouseOver and onMouseOut event handlers combined with the JavaScript 1.1 images array.

Now with JavaScript 1.2, in Communicator, and JScript 3.0, in Internet Explorer 4, we have 4 (count 'em, four) more mouse action event handlers to work with: onMouseDown, onMouseUp, onMouseMove, and onDbleClick. We will be discussing onMouseMove, the most powerful of the bunch, extensively in the weeks to come. For the present example, all we need are the first two.

It will come as no surprise to learn that onMouseDown and onMouseUp, are triggered whenever a mouse button is pressed or released. Essentially, they are the two component parts of the old onClick event handler. And they provide an additional dimension to our rollovers.

R.I.P. <INPUT TYPE=BUTTON>

Till now, whenever we needed a button that would appear pressed when we clicked on it, our only choice was to create a form with a <INPUT TYPE=BUTTON VALUE="Click Me!"> tag. Like so:

Every platform supplies their own generic button shape and colour leaving the web author with no control over the look of a live button. The new event handlers have eliminated this limitation.

Step by Step

First of all we must assemble the component parts by creating two images:

a regular raised button: and a pressed button:

In our HTML, we position the raised button appropriately and give the image a name (see sidebar), in this case: imBut.

<IMG NAME="imBut" SRC="imButup.gif" WIDTH=62 HEIGHT=28 BORDER=0>

This image now has a place in the images array, and can be referenced as:

document.images["imBut"]

NOTE: It can also be referenced by its index in the images array, established by the order in which it was loaded:
 document.images[index]
This is a dangerous way of referring to images in scripts. If you edit your page, and physically insert an image before a later referenced image, the latter image will automatically be assigned a different index and your code will have to be modified to reflect the change. This relative method of addressing is best used in scripts that perform tasks on the whole image array and need an integer reference for each element for counting purposes. For our use, the absolute method, using its string "name", is best.

We now have a named displayed image, and we know the filename/URL of the image we want to load over it to create our effect. Every element in the images array has a script-modifiable src property. We swap images with JavaScript:

document.images['imBut'].src = 'imButdown.gif'

We call this script snippet in our <A> tag whenever our mouse button is down:

onMouseDown = "document.images['imBut'].src = 'imButdown.gif'"

...and this one whenever our mouse button is released (up):

onMouseUp = "document.images['imBut'].src = 'imButup.gif'"

Resulting in:

Click Me!

Code

<A HREF="yourlinkgoeshere.html"
   onMouseDown = "document.images['imBut'].src='imButdown.gif'"
   onMouseUp = "document.images['imBut'].src='imButUp.gif'">
<IMG NAME="imBut" SRC="imButup.gif" WIDTH=62 HEIGHT=28 BORDER=0></A>

IMPORTANT! To speed up the image swapping, don't forget to preload the pressed image. All images that are meant to be swapped should be preloaded. Otherwise, the new image will appear only after an internet connection is made and it is downloaded. Almost defeats the purpose of a rollover.

If preloading isn't lighting up any bulbs for you, we've provided a reminder of how to preload images.

Produced by Peter Belesis and



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Choosing the Right Online Backup Provider · Mother Avaya Nurtures Her Technology Partners · Software as a Service a Winning Model for Hotspot Provider

All Rights Reserved. Legal Notices.
Created: 07/23/97
Revised: 09/28/97

URL: http://www.webreference.com/dhtml/column1/