Web Design Articles – Web Designing Tips

Accueil >> free email >> Web Design Articles – Web Designing Tips
Web Design Articles – Web Designing Tips

Mouseovers first became technically feasible with the release of Netscape Navigator 2.0, which included a new scripting language called JavaScript. The idea was to allow the Web to transcend for the first time the basic functionality of HTML. As standards in this area converge, the use of scripting on the Web is becoming more feasible. The swell in popularity of the script-powered mouseover (as opposed to the comparatively slow and lazy Java implementations that were popular for a brief time) is a testament to the usefulness of JavaScript and its kin.
The Image Object
Frankly speaking, you don‘t need to be an expert with JavaScript to understand this article, as everything is explained from the ground up.
In most respects, JavaScript is an object-oriented language, which means that in the world of JavaScript, everything is an object. A browser window is an object (referred to as the window), a Web page is an object (the document) and the images in the Web page are objects in their own right. The nice thing about objects is that they have properties that you can change.
If you are familiar with the basics of HTML, you’ll be used to the concept that you can create an image object on a Web page using the tag:
Now, without knowing it, when you type this, you automatically create a JavaScript image object, which will allow you to change its properties later on if you like. This is exactly what we want to follow to create a mouseover. When the mouse moves over the image we want to change what the image looks like, and what the image looks like is a property of the image object.
To simplify referring to this particular image object later on, we can assign it a name. This is pretty easy to do, and just requires us adding the NAME attribute to our tag.
Now our image object is named "myimage", and can be referred to as such. At this point, it‘s worth pointing out that JavaScript is largely case sensitive. In other words, our image named "myimage" cannot be referred to as "MYIMAGE" or "MyImage".
You are aware that objects in JavaScript have properties that we can change. We can set a property of our "myimage" object with the following general JavaScript statement:
document.images["myimage"].property = newvalue;
Where property is the name of the property you want to change and new value is the new value you are assigning it. This line can be read:
"In the current document, in the image called "myimage", set property to newvalue."
You must be wondering to know how to apply this to the mouseover. Well in a mouseover, we change the image displayed by the image object. Just as in HTML, where we use the SRC attribute of the tag to indicate the URL of the image file we want to display, in JavaScript we set the src property of the image object to the URL of the image file we want it to display.
Using the general JavaScript statement as mentioned above, this is written simply:
document.images["myimage"].src = "newimage.gif";
How Mouseovers Work

Now that you know how to change an image‘s appearance using JavaScript, we need to figure out how to do this when the user places the cursor over the image. Also, we need to know how to switch it back again when the user‘s mouse isn‘t over the image anymore. The feature of JavaScript that allows us to do this is called event handlers.
Event handlers can be thought of as "triggers" that cause things to happen when a certain condition is met. The two event handlers that interest us when it comes to Mouseovers are onMouseOver and onMouseOut. These allow you to define certain pieces of JavaScript to be run whenever the user‘s mouse hovers over or passes out of a given HTML element.
While the HTML 4.01 specification says that we should be able to define onMouseOver and onMouseOut event handlers for any HTML element, Netscape Navigator falls a bit short by only providing this facility for the anchor reference.
 
This creates a link that does nothing (void) when clicked. If you did want it to link to something, you‘d just stick your URL in the place of javascript:void(0). To add our event handlers is as simple as inserting them as attributes to the tag:

In the above, overmyimage() and outmyimage() are JavaScript functions that handle changing the image between the "on" and "off" states. A function is a piece of JavaScript that has been set aside to be "triggered" at some later time. In most cases, functions are defined in the header of your HTML file (between the and tags). Our overmyimage() function would look something like this:

We would insert the code that‘s to be run whenever this function is called (triggered) at the place we have indicated.
Now, armed with a basic knowledge of the image object, event handlers, and functions, we are now ready to create…
Our First Mouseover

If you‘ve been following closely and fully understand the elements we have covered so far, their assembly into a working mouseover should be quite easy. Now just follow the simple steps to achieve this.
First, let‘s start by creating our image. It will be 70 pixels wide and 30 pixels high, and the filename for the "inactive" image will be "off.gif". We do this just as we would normally, using the HTML tag. We‘ll also use the NAME attribute to assign it the name "my1stMouseOver".
Now we‘ll add a "link to nowhere" around our image, so that we can hook in onMouseOver and onMouseOut event handlers, set to trigger JavaScript functions that "activate" and "deactivate" the image. Appropriately enough, we will call these functions activate() and deactivate().

Now all that‘s left is to write the JavaScript functions, activate() and deactivate(). The activate() function will change the src property of the "my1stMouseOver" to the "active" image file, which we‘ll call "on.gif".
function activate() {
document.images["my1stMouseOver"].src = "on.gif";
}
Similarly, the deactivate() function will change the src property of the "my1stMouseOver" back to the "inactive" image file, "off.gif".
function deactivate() {
document.images["my1stMouseOver"].src = "off.gif";
}
And that‘s it! We‘ve now created an image that displays "off.gif", except when the user places the mouse pointer over it, at which time it changes to display "on.gif". When the user moves the mouse away again, it goes back to displaying "off.gif".
In the following sections, we‘ll take the basic mouseover that we created in the last section and add a couple of bells and whistles that will improve its expandability and performance. Overall, these are good skills to know if you‘re going to be making serious use of mouseovers in the pages you design.
Sharing Functions
In our basic mouseover, we used one tag and two JavaScript functions to obtain the effect we were looking for, but on a page with 5 mouseovers, that would mean having to write 10 different functions (not to mention coming up with 10 different names for them!). What if there was a way to use the same two functions for all the mouseovers on your page?
As it turns out, this is surprisingly easy to do. First, we must create JavaScript variables that contain the filename of each mouseover‘s "on" and "off" image. If, for example, we had created a page with three tags that we wanted to be mouseovers, and we‘d named these images "first", "second" and "third" using the NAME attribute of the tag, we would create variables with names of the form name_on and name_off, like this:
var first_off = "image1off.gif";
var first_on = "image1on.gif";
var second_off = "image2off.gif";
var second_on = "image2on.gif";
var third_off = "image3off.gif";
var third_on = "image3on.gif";
The next thing we have to do is set up the onMouseOver and onMouseOut event handlers to pass the name of the image to change to our functions. For example, the code for our "second" Mouseover would look like this:

Notice that the argument (the text to be passed to the function) is surrounded by single quotes (‘). This is so that they don‘t interfere with the double quotes (") that surround the event handler.
By passing the name of the image to be changed, to our ‘activate’ and ‘deactivate’ functions, we give them a way to know which image to change when they‘re triggered. This is how we manage to share the two functions between as many mouseovers as we want. All that‘s left is to modify these functions to receive this image name, and use it to modify the correct image.
The code for our new functions will look like this:
function activate(imgName) {
document.images[imgName].src = eval( imgName + "_on" );
}

function deactivate(imgName) {
document.images[imgName].src = eval( imgName + "_off" );
}
By placing the word imgName between the parentheses that follow the name of the functions, we tell them to accept a value that‘s being passed to them (in this case, the name of the image to change), and stick it into a variable called imgName until we finish processing the function. We then make use of this variable in two places. First, we use it to indicate which image object who‘s src property we want to change (images[imgName]). Second, we use it to refer to the variable that contains the correct filename. This is done using the eval(…) built-in function.
eval( imgName + "_on" )
All this does is takes the value of the imgName variable, sticks it together with the piece of text "_on" and then evaluates the result as if it were a normal piece of JavaScript code. To make this as clear as possible, let‘s go through what would happen if the onMouseOver event handler were triggered on the image named "third".
When the onMouseOver event handler is triggered, it runs the activate function, and passes it the name of the image — "third". The function accepts this argument, and places "third" wherever the variable imgName appears.
function activate("third") {
document.images["third"].src = eval( "third" + "_on" );
}
The eval(…) built-in function sticks the two strings it has together (as indicated by the ‘+‘ operator) and then evaluates it as if it were a normal piece of JavaScript:
document.images["third"].src = third_on;
Now third_on is the name of the variable containing the "on" image filename for the "third" image, so its value is substituted in:
document.images["third"].src = "image3on.gif";
And that‘s it — we‘re left with a statement of the same form we used for our basic mouseover, which will change the src property of the "third" image to "image3on.gif"! The key was in naming the variables that contained the filenames so that the eval(…) function could come up with the variable name by just tacking "_on" or "_off" onto the end of the name of the image.

Preloading Images

Most Web browsers these days are pretty smart. When they load something, they put it into a cache on your system where it will remain for a little while for quick and easy access in case it‘s needed again. Most browsers aren‘t smart enough to load things ahead of time, though. In most cases, this isn‘t a big problem, but when it comes to mouseovers it can be trying at times!
When a user places the cursor over one of your mouseovers, the Web browser will realize it needs to display the "on" image. If this is the first time this user accesses your site, chances are that image isn‘t in the cache, and must be loaded from your site. This causes a delay that can often be longer that it takes for the user to have moved the mouse back off the mouseover, having never seen that pretty light-up button you‘d planned to display. Worse, if the "on" image is only partially loaded, the user may see a horrible, blurry mess in the place of the slick image you‘d planned.
How do we overcome this problem? The solution is to preload the images required by the mouseover into the user‘s cache so that we can be sure they‘re available before we attempt to display them. To do this, we must make creative use of the JavaScript image object.
Up until now, we‘ve only created image objects with the standard HTML tag. We can, however, create them using only JavaScript. Such JavaScript-only image objects do not display on the Web page, but they do load and cache and they have the same properties as an image object created with HTML. The strategy, then, is to create JavaScript-only image objects for every one of the image files to be used in your mouseovers. To do this for our example of the three mouseovers used in the previous section, we can replace our variable declarations with the following image declarations:
var first_off = new Image();
first_off.src = "off1.gif";
var first_on = new Image();
first_on.src = "on1.gif";

var second_off = new Image();
second_off.src = "off2.gif";
var second_on = new Image();
second_on.src = "on2.gif";
var third_off = new Image();
third_off.src = "off3.gif";
var third_on = new Image();
third_on.src = "on3.gif";
For each image file, we create a new image object, and then set its src attribute to the appropriate filename. In setting this property, the browser decides it needs to download the image file, at which point it is stored in the cache for quick retrieval when it is actually needed for display. We obey the same naming convention for the image objects as we did for the filename variables in the previous section.
Now all that‘s left is to adjust our ‘activate’ and ‘deactivate’ functions once again so that they use the values of the src properties of the JavaScript (only image objects instead of the values of the variables.)
function activate(imgName) {
document.images[imgName].src = eval( imgName + "_on.src" );
}

function deactivate(imgName) {
document.images[imgName].src = eval( imgName + "_off.src" );
}
Finally, we can make one more adjustment to make doubly sure that the user never sees a half-loaded image. Although this preloading technique does load the mouseover images ahead of time, it is still possible for the user to trigger a mouseover before the images have completely loaded. To guard against this, we put a little "valve" in the ‘activate’ and ‘deactivate’ functions:
function activate(imgName) {
if ( eval(imgName + "_on.complete") ) {
document.images[imgName].src = eval( imgName + "_on.src" );
}
}

function deactivate(imgName) {
if ( eval(imgName + "_off.complete") ) {
document.images[imgName].src = eval( imgName + "_off.src" );
}
}
This makes use of another property of the JavaScript image object. We already know the src property contains the URL of the image file it represents. Here we use the complete property, which is true if the image has completed loading, and false if it has not. These newly-modified versions of activate and deactivate only change the displayed image if the JavaScript-only image object responsible for preloading the new image is complete.
Advanced Techniques
Finally, this section will show you a simple way to make sure your mouseover pages are compatible with older browsers, as well as a neat trick to prevent preloading mouseover images from slowing down the rest of your page.
Browser Compatibility with Object Detection
Now that you know all about how to make mouseovers that work well, it‘s worth taking a moment to learn how to make ones that don‘t work just as well. While mouseovers of the type I‘ve outlined up until now will work consistently on all recent JavaScript-enabled browsers (Netscape 3+, as well as MSIE 4+), there are some browsers that support JavaScript, but don‘t fully support the image object.
One such browser is Microsoft Internet Explorer 3.0. If we were to load a page that contained mouseovers with the code I‘ve used up until now on MSIE 3.0, we‘d see a lot of ugly JavaScript errors. Since we can‘t do Mouseovers on this browser, the least we can do is getting rid of the error messages.
While there are several ways to detect the version of the user‘s browser in JavaScript and react accordingly, such techniques can sometimes prove unpredictable in the face of less mainstream Web browsers. Also, whenever either Netscape or Microsoft release a new version of their browser software, any page using such browser detection scripts would need to be updated.
Much more reliable and efficient is a little-known technique called object detection. The idea is that, instead of detecting the version of the browser, we detect the features that the browser supports. As any browser that supports the image object will be capable of mouseovers, we need only detect whether or not the browser knows what image objects are before we do anything that requires support for them. This is done with a simple if statement:
if (document.images) {
… code here …
}
Which can be read:
"If the current document contains images then do the following…"
If we add an if (document.images) to the activate and deactivate functions, and place one around the image declarations, we‘ll have mouseovers that quietly disappear on browsers that don‘t support the image object.
Delaying Image Loading
Preloading images is all well and good, but what if you have 20 mouseovers on a given page? Do you really want to have the browser busy downloading 20 images before it even gets to start loading the actual content of the page? Obviously not. A nice way around this is to use the on Load event handler of the tag to trigger a function that loads the mouseover images only after the HTML code for the page has been loaded. This way, all the images actually displayed on the page are put ahead of the mouseover images in the download order.
The use of onLoad is fairly straightforward:
All that‘s left is to put the preloading of the mouseover images inside the loadImages() function.
if (document.images) {
var first_off = new Image();
var first_on = new Image();

var second_off = new Image();
var second_on = new Image();

var third_off = new Image();
var third_on = new Image();
}

function loadImages() {
if (document.images) {
first_off.src = "off1.gif";
first_on.src = "on1.gif";

second_off.src = "off2.gif";
second_on.src = "on2.gif";

third_off.src = "off3.gif";
third_on.src = "on3.gif";
}
}
The point worth observing here is that we have left the creation of the JavaScript-only image objects outside the loadImages() function. This is done for two reasons. First, the creation of the objects doesn‘t load any files: only the setting of the src properties does that, so leaving the object declarations outside the function won‘t slow down the loading of the rest of the page. Second, objects or variables that are created inside a function only exist inside that function. If we were to move the creation of the image objects inside the function, they would disappear as soon as the function finished running, and the ‘activate’ and ‘deactivate’ functions would no longer be able to use them.
That‘s it! Now our mouseover images will only be loaded when the on Load event handler of the tag is triggered, and this only happens once the HTML code for the whole page has been loaded, and any images on that page have started loading. Whether this is desirable or not is up to you to judge, but it can be a good idea in cases where there are a lot of mouseovers on a page and their being immediately available is not that important.
The Essence:
In this article we have tried to throw enough light on the techniques involved in making Web page elements respond to the movement of the user‘s mouse. Not only did we cover the basics in terms that, hopefully, were accessible to anyone with a superficial knowledge of Web design and general programming, but we also covered some more advanced topics that would prove useful to the more experienced Web developer.

Tagged ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*


nine + 9 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Accueil >> free email >> Web Design Articles – Web Designing Tips
Web Design Articles – Web Designing Tips

Build a Mercator
Style 2 — A Wireframe Sphere

You can also create an Action to do a lot of the grunt work for you. While an Action won‘t be able to create this effect completely, it can do most of the steps involved.
Note to Mac Users: The PC key Ctrl = the Mac key Cmd. And the PC key Alt = the Mac key Option. Don‘t forget to Save after every 2 or 3 steps!
 
Create a new document and give it equal length and width dimensions, i.e. 300px by 300px. Use a white background (for this example, at least). Create a New Layer (Ctrl+Shift+N). Then create a circular selection that fills most of the canvas area. Hold down the ALT and SHIFT keys to create a perfect circle that draws from the center outward.

 
 
 
Apply a white-to-black Radial Gradient to the selection. Holding the Shift key down, start the gradient tool at the very top center of the circle, and drag it to the very bottom center. Leave the selection going.

 
 
 
Add an Adjustment Layer right above the sphere layer (click the split black and white circle image at the bottom of the Layers palette). Choose the "Posterize" filter for this adjustment layer. Set the filter to 6. Now just see what happens. We get 6 individually shaded sections, which will end up becoming our wireframe rings. So set the filter to the number of rings you want. If your selection is no longer active, Ctrl-click on the gradient sphere layer again to reselect it.
Add another Adjustment Layer right above the previous one. Choose the "Brightness and Contrast" filter for this layer. Set the Brightness up to +25, and Contrast down to -50. This will allow you to see every level of gray clearly. This shall also be helpful in the steps to come. If your selection is no longer active, Ctrl-click on the gradient sphere layer again to reselect it. Now you have left this part until after the Adjustment layers were added, specifically so you could see what happens when you apply the "Spherize" filter. Click on the gradient sphere layer to make it the active layer. Apply the "Spherize" filter (FILTER>Distort>Spherize) using the ‘Normal‘ mode, and set to 100% strength.  
Did you notice how applying this filter made the gray sections of the sphere flatter, and gave them a better, proper shape for our effect? If you didn‘t apply this filter to your gradient, your globe‘s lines would be too steep — they wouldn‘t look correct. Once you‘ve done this, leave the selection going. Under the EDIT menu choose ‘Copy Merged‘ (Ctrl+Shift+C). Open the Channels palette and create a new channel. Then paste what you just copied into the channel (press Ctrl+V). Now you can deselect. Now under the FILTER menu, choose ‘Noise>Median‘. Set it to 3px, and click OK. Then apply it twice more by pressing Ctrl+F twice. And now, also from the FILTER>Noise menu, choose ‘Dust and Scratches‘. Apply this with the settings Radius=6 and Threshold=0. Before going on, create a duplicate of your new channel as a backup. These filters are used to help smooth out the lines dividing the different shades. Unfortunately, they can‘t smooth them completely, which is one of the limitations of trying to achieve this effect with filters only. Actually the problem filter here is the ‘Posterize‘ filter, not the other two, but don‘t worry – you shall get the lines even cleaner by applying one more filter.

 
 
 
 
Now open the FILTER menu and choose Stylize>Glowing Edges. Use the settings 5/20/15. And now for the secret ingredient — a filter that will smooth our lines out even more! Under the ‘FILTER>Stylize‘ menu, choose ‘Diffuse>Anisotropic‘. Apply it once, and then press Ctrl+F 4 times to apply the filter a total of 5 times. Lastly, press Ctrl+L to open the Levels filter. Drag the right slider left, to 128. Then start dragging the left slider to the right until your lines get cleaner, harder edges. Don‘t go too far, or the lines will look jagged. If that happens, move the left slider back a bit. Click OK to apply.
Ctrl-Click the channel to select the lines. Then go back to the Layers palette and create a New Layer, filling the selection with black. Then Deselect. Now you‘ll need to ‘hide‘ the bottom 3 layers — the original gradient sphere layer, and the two adjustment layers. Click on their ‘eye‘ icons to hide those layers. You should end up seeing a negative version of your channel. Name this layer ‘base sphere‘. If your lines are a little uneven, then run the ‘Diffuse>Anisotropic‘ filter again, and apply the Unsharp Mask set to 250% / 0.5 / 0. If they‘re just a little fuzzy, then only apply the Unsharp Mask filter set to 100% / 0.5 / 0.
Extra Tips
This entire effect can be made into an Action. But as an Action it would be best if you followed the advice given in tip 4, below. I would also advise that if you make it into an Action, you get the Action to create a new document for the Mercator effect, and not combine it with any current image you may be working with. Once the effect is made, you can just merge the layers and drag it into the document you‘re working on. If your circle‘s border is uneven, just select the gradient sphere and invert the selection. Then while on the outline layer — ‘base sphere‘ — press Delete. Now invert the selection back again and under the EDIT menu choose Stroke. Apply a 7 pixel stroke, in the center. That should give you a nice clean border. If you want the lines to be thicker, then apply the ‘Minimum‘ filter to the layer (FILTER>Other>Minimum). I wouldn‘t go higher than 1 or 2 pixels using this filter though, as the lines will start to look distorted. There‘s a way to get slightly nicer results for this effect. Start out by making your globe twice the size you intend on using. Then, after you‘ve completed all the steps, resize your document down by 50%. Because Photoshop re-interpolates the image, the lines become a little smoother. This is especially good if you plan on thickening the lines up, as mentioned in the previous tip. Actually the ‘Anisotropic‘ setting of the ‘Diffuse‘ filter is the filter that would allow us to really clean up and smooth out the edges of the lines. And the point here is that just because you don‘t find a solution to a problem right away, doesn‘t mean there isn‘t one to find. You ought to have patience and make continuous efforts with your actions until you discover the right filter using the perfect settings. It is like trial and error method where in you don’t have any alternative. Next, you are going to add the vertical lines to the sphere to give it a look of a wireframe globe.
Initially this process might seem tedious but once you get into the groove of the steps, this effect can take only few minutes to achieve — especially if you create an Action to do most of the work. This is the part that would be difficult to create using an Action, as it involves manual selections.
 
Create a New Layer above your ‘base sphere‘ layer. You don‘t have to name this one. Ctrl-Click the original shaded sphere layer to make a selection of it. Now Stroke this selection by 5 pixels, in the center (EDIT>Stroke…)

 
 
 
 
Again under the EDIT menu, go to the ‘Transform‘ option and choose the ‘Scale‘ function. Hold down the ALT key, grab one of the side control handles, and drag it inward so the circle gets thinner. Make it look like an oval — like the example image here. Create another New Layer, and repeat the same steps as above, but this time, resize the circle until it‘s even thinner than the first one. You‘ll notice that this step makes the line really thin. We can easily fix this using the ‘Maximum‘ filter found under the ‘Other‘ option (FILTER>Other>Minimum…).
After both the vertical line layers have been created to your satisfaction, click on the top one and press Ctrl+E to merge them together. Then merge the vertical lines layer to the ‘base shape‘ layer. Now you should have only one layer that has your entire Mercator on it, and you‘re free to colour it, fill it with a texture, etc.
And that‘s all about the end result. You are through!
Extra Tips
There is one more tiny detail that really finishes off the effect. Select the ‘Transform>Scale‘ tool and reduce the height of both of the vertical line layers by 2 or 3 pixels. Basically, you want the tops of these lines to sit just below the top line of the ‘base sphere‘ layer. But don‘t let any white area show in between them (see the example image). If you ‘Link‘ these layers together you can resize them both at the same time.

 
 
 
At this point, you might do well to create a new custom Brush out of your Mercator shape. That way, next time it will only be one click away. So, how can you do it? In PS7: Just make sure the Mercator layer is active, then under the EDIT menu choose "Define Brush". In PS6 and 5.5: Ctrl-Click the Mercator layer to select it, then open the EDIT menu and choose "Define Brush". If you‘re wondering why I don‘t suggest creating a Vector Shape from this, it‘s because Photoshop‘s pretty terrible when it tries to convert this image to vector — the lines are just not smooth enough for it. You‘d be better off to create one in a vector (Illustration) program and import it. At least as brush, you have the option of scaling it before you create it (in PS7 only. In earlier versions you‘ll have to create it, then use the ‘Transform>Scale‘ tool).  
We shall throw more light on giving effects to the so called “Mercator” in our next article. Wait for that exciting and informative article.
Will You? Ha….Ha…..

Tagged ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*


three × 2 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>