HTML5 Development Class: Mouse Events

By Curtis Dicken

http://www.htmlgoodies.com/html5/tutorials/HTML5-Development-Class-Mouse-Events-3880171.htm (Back to article)

Introduction to HTML 5: Mouse Events

HTML5 is changing and expanding the landscape of web development. There are a multitude of new attributes and events that are being introduced with HTML5. In this installment we’ll take a look at the new and old events surrounding the mouse including movement, position and clicking as well as the new drag and drop events.

About Events

For those new to HTML, events are simply things that happen like moving the mouse, clicking the mouse or hovering the mouse pointer over an element. The event allows you, the developer, to perform an action when an event occurs. The action usually takes the form of some JavaScript. A practical example of using an event to perform an action would be changing an image when the user hovers over it. In that instance the hovering is the event that triggers the action of changing the image.

Keep in mind all of these events are tied to individual elements on your page. That means simply clicking the mouse on an empty area of your page does not fire any onclick event. A specific element on the page must be clicked in order for an onclick or other event to fire.

Mouse Events Unchanged

It can be argued that the mouse events in HTML4 are probably the most used events. Because of that widespread use and the fact they are incredibly intuitive they are unchanged from HTML4 to HTML5. However, the roles of some of these events will take on new meaning with the addition of new events like ondrag and ondrop.

Mouse Events Expanded

In HTML5 events have been expanded to allow developers to add the dragging and dropping of objects to their repertoire. It also now includes an event triggered by scrolling. Lastly, there is a new event that allows developers to incorporate the actions of the mouse wheel.

Note:  The drag and drop events are a little different than the typical mouse events. When using the new drag and drop features of HTML5 you will have elements that you set as draggable as well as drop targets. The draggable elements will have specific events that pertain to them such as ondrag while the drop targets have other events particular to their role like the ondrop event. Drag events also typically use handlers which are basically just JavaScript functions designed to handle specific drag and drop functionality no matter what element triggers the event.

Conclusion

The new mouse events offered in HTML5 will eventually greatly expand the user experience. On the development side it will make rich user interfaces easier to implement and truly cross-browser compatible. The good news is that most of the new mouse events have already been implemented in most of the popular modern browsers. The bad news is that the implementation is still a work in progress and behavior is not identical across all browsers. There are subtle differences, especially when it comes to dragging and dropping.

Take a look at this great HTML5 drag and drop example in different browsers and see if you can spot the differences. Happy coding!