Quick & Easy JavaScript Mouseover Images Without Using MooTools

By  on  

Not everyone uses the MooTools framework or any framework for that matter so my article, MooTools Image MouseOvers - Cleaner JavaScript Code, Less Hassle, wouldn't apply to everyone. I do know, however, that most everyone has a need for a quick and simple JavaScript mouseover function that can be used without MooTools.

I've developed the following function for creating mouseover image functionality.

The Code

function mo(over_out, image) {
	var src = image.src, ext = src.substring(src.lastIndexOf('.'),src.length);
	image.src= (over_out == 1 ? src.replace(ext, '-mo' + ext) : src.replace('-mo' + ext, ext)); return; //ternary operators -- sweet!
}

The Usage

Home

The Explanation

There are to two arguments -- over_out represents whether the mouseover or mouseout image should be displayed. "1" means the mouse is over, "0" means the mouse is out; "image" is the image object. If over_out is 1, the function replaces the file extension with "-mo." + {file-extension}. If over_out is 0, the function removes the "-mo". Simple, right?

This method isn't quite as slick as the MooTools method because you don't avoid the "onmouseover" and "onmouseout" attributes for the image, but it is minimal code inside each attribute. The JavaScript also uses ternary operators (works the same in JavaScript as it does in PHP.)

Do you have a better one? Please share!

Recent Features

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

  • By
    Face Detection with jQuery

    I've always been intrigued by recognition software because I cannot imagine the logic that goes into all of the algorithms. Whether it's voice, face, or other types of detection, people look and sound so different, pictures are shot differently, and from different angles, I...

  • By
    Using Opacity to Show Focus with jQuery

    A few days back I debuted a sweet article that made use of MooTools JavaScript and opacity to show focus on a specified element. Here's how to accomplish that feat using jQuery. The jQuery JavaScript There you have it. Opacity is a very simple but effective...

Discussion

  1. I never really used any of those libraries but after reading some of these articles that explain how to not use them I think that many people rely too much on these bloated libraries.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!