JavaScript Event Object _ mouse Event

Source: Internet
Author: User

Mouse events are the most common type of event on the Web, after all, the mouse is still the most important positioning device. Then through the event object can get to the mouse button information and screen coordinate acquisition and so on.

One, the mouse button

The Click event is triggered only when the primary mouse button is clicked (usually the left button of the mouse), so it is not necessary to detect the button's information.

For MouseDown and MouseUp events, however, there is a button property in their event object that represents the press or Release button.

  

<script type= "Text/javascript" >    function() {        function  (EVT) {            var e = evt | | window.event;             // achieve cross-browser compatibility Get event object             alert (e.button)     ;}; </script>

In the vast majority of cases, we only use the primary and secondary three click Key, IE given the other key combinations are generally not available. So, we just need to do these three kinds of compatibility can.

<script type= "Text/javascript" >functionGetbutton (evt) {//Cross-Browser right-click the appropriate        varE = EVT | |window.event; if(EVT) {//Chrome Browser supports web and IE            returnE.button;//be aware of the Order of Judgment}Else if(window.event) {Switch(E.button) { Case1 :                    return0;  Case4 :                    return1;  Case2 :                    return2; } }} window.onload=function() {document.onmouseup=function(EVT) {//called            if(Getbutton (evt) = = 0) {alert (' Press the left button! ‘); }Else if(Getbutton (evt) = = 1) {alert (' Pressed the middle button! ‘); }Else if(Getbutton (evt) = = 2) {alert (' Press the right button! ‘ );    }        }; };</script>

Second, visual area and screen coordinates

The event object provides two sets of properties for the browser coordinates, one for the left side of the page's visual area, and the other for screen coordinates.

  

<script type= "Text/javascript" >    function() {        function  (EVT) {            var e = evt | | window.event;             + ', ' + e.clienty);             + ', ' + e.screeny);        };    }; </script>

Third, modifier keys

Sometimes, we need to use some keys on the keyboard to match the mouse to trigger some special events.

These keys are: Shfit, Ctrl, Alt, and meat (Windows keys in Windows, CMD in Mac), which are often used to modify mouse events and behavior, so they are called modifier keys. (combined with mouse)

<script type= "Text/javascript" >functionGetKey (evt) {varE = EVT | |window.event; varKeys = []; if(E.shiftkey) {Keys.push (' Shift ');//adding elements to an array        }        if(E.ctrlkey) {Keys.push (' Ctrl '); }        if(E.altkey) {Keys.push (' Alt '); }                returnkeys; } window.onload=function() {Document.onclick=function(evt) {alert (GetKey (EVT));    }; };</script>

JavaScript Event Object _ mouse Event

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.