How to Handle Mouse Even in Javascript Part 1
Submitted by thusitcp on Sunday, October 12, 2014 - 22:17.
Language
Most common way users interact with the computer is by using pointing device called mouse. This wonderful device help graters amount of improvement usability of computer applications. Today’s all the computer language support mouse events. In this article I am going to explain how to handle mouse event using JavaScript.
When we consider JavaScript event there are number of mouse event but here I am explaining most of commonly used and highly interactive event with web application. Following are some of the mouse event of JavaScript.
Click
dblclick
mouseover
mouseout
mouseenter
mouseleave
mousedown
mouseup
mousemove
contextmenu
mousewheel
DOMMouseScroll
Now I am going to describe some of event and when they are occurring and pro grammatically how to used them.
Click event.
Most common event is click event. This event is fired when you basically click on an element. This can be describe farther as “the click event is fired when you use your mouse to press down on an element and then release the press while still over that same element”. For a example we want to click a button and during click event we are go through following steps.
1. Mouse on the button ( mouse over event )
2. Press down the button (mouse down event )
3. Release the button ( mouse up event )
4. Finished the release
At the moment of finished the release mouse click event is firing. During the click event life cycle above events are occurring as sub events.
Double click event
Double click event fires when you do click event within split second. Time gap between two click event will be decide double click event firing or not.
Mouseover and Mouseout event
When you place mouse pointer on element mouse over event will be fired. Respectively when you move mouse pointer outside of that element mouse out event will be fire. These two event help developers to build highly interact and informative web applications.
Mouseenter and mouseleave
These two event are also identically same with mouse over and mouse out event when the parent element does not have any child element. If parent element contained any child element mouse over and mouse out event will be fired each time mouse in and out. The main diference is mouseenter and mouse leave event is it is firing only one time. It does not care any mouse movement on child element
Mousedown and mouseup event.
As I explain in mouse click event these two events are sub event of mouse click event. When you are press mouse on any element mousedown event occurring other hand when you release mouse mouseup event occurring. After you finish above two event successfully mouse click event will be occurred.
Mousemove event
When you are moving mouse trough out screen mouse move event is occurring. Every even has event properties. Mouse move event can capture exact point of mouse pointer is right now so we can access those properties by using event properties.
I will explain other complex mouse event and how to handle them using javascript in my feature article with more details.
- var button = document.querySelector("#testClicked");
- button.addEventListener("click", doClick, false);
- function doClick(e) {
- console.log("Hello how are you ?!");
- }
- var button = document.querySelector("#testDoubleClicked");
- button.addEventListener("click", doDoubleClick, false);
- function doDoubleClick(e) {
- console.log("Clicked Twise Me");
- }
- var button = document.querySelector("#buttonMouseEvent");
- button.addEventListener("mouseover", doMouseHover, false);
- button.addEventListener("mouseout", doMouseOut, false);
- function doMouseHover(e) {
- console.log("Mouse Hover Me");
- }
- function doMouseOut(e) {
- console.log("Mouse Hover Me");
- }
- var button = document.querySelector("#buttonMouseEvent");
- button.addEventListener("mousedown", doMouseDown, false);
- button.addEventListener("mouseup", doMouseUp, false);
- function doMouseDown(e) {
- console.log("Mouse Down");
- }
- function doMouseUp(e) {
- console.log("Mouse Up");
- }
- button.addEventListener("mousedown", doMouseDown, false);
- function doMouseDown(e) {
- console.log("my current coordinate are at X:" +e.screenX +" Y:" + e.screenY );
- }
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 32 views