|
JavaScript ButtonCoding a JavaScript button by yourselfJavaScript buttons can give nice effects to your web page. If done properly, JavaScript buttons give very impressive look and feel. This article shows you the creation of a JavaScript button in simple steps.The working of a JavaScript ButtonThe JavaScript button effects are created using images. The JavaScript button requires three images for the different stages (normal, active and clicked). When the user moves the mouse over the image, the image is switched to the 'active' image. When the user clicks the image, the image is switched to 'clicked' image This in effect creates the feeling of a button. JavaScript buttons are also known as Mouse over buttons.
Build Web Forms Faaaast!!Using Simfatic Forms it is very easy to design, create, install and maintain web forms; no coding required! Quickly create your form, install it and receive form submissions by email.Click here for more info The image object
In order to display an image, the HTML code is:
To refer this image in the JavaScript, you need to give a name to the image using the
'name' attribute.
The HTML code for the image is ready. Now our button will look as shown below: Accessing the Image From JavaScriptOnce the image object is inserted in the HTML page, you can access it using the name you have given.
There are many properties for the JavaScript image object. However, the property that we are interested is the 'src' property. This property contains the image file name. So if you change the src property of the image object to another file, the image will change. Handling the Mouse-Over EventWe need to change the image when the user moves the mouse over the image. So we have to handle the event that the browser sends when the user moves the mouse over the image.However, the image object does not support onMouseOver event. The workaround is to include the image within the anchor ( <A> ) tag and handle the mouse over event.
We need to correct the HTML for the image as We have defined the handler for the mouse over event as changeImage() function. Now we need to define this function.
The function (that we defined as the mouse over handler) just changes the src attribute to another image file.
The example is available here: Click the link above and the code working. Move the mouse over the button; the image changes. Handling the Mouse-Out Event
But as you can see, even when you move the mouse out, the
image is not changing back to the old image.
The changeImageBack() event handler has to take care of changing the image when the user moves the mouse cursor out of the image. The modified code is as shown below:
See the modified example file: Handling the Mouse Click EventYou may also want to change the image when the button is pressed. The Mouse Down and the Mouse Up messages are to be handled for creating this effect. The code below handles the Mouse up and Mouse down messages also:
The example file is here: Navigating to Another PageYou can give the URL in the 'href' attribute of the anchor ( <A> ) tag to navigate to that page when the user clicks the button.
For example: Image Pre-fetchingImage pre-fetching is a technique to make the browser load and decode the image early. Image pre-fetching could be used to create smooth image transition effects. You might have noticed that for the first time the button transition is not smooth, in the examples above.The solution is to make the browser load the image and keep it in cache. You can use the Image() JavaScript object to make the browser pre-fetch the images. The javascript code for creating the image object goes like this:
The image objects are created while loading the page. This forces the browser to load the images from the site and decode it. The button code we developed, with image pre-fetching support, is given below:
function changeImage()
The example file is available here: |
|
. Copyright © 2003-2009 JavaScript-coder.com. All rights reserved. |
||