"Write a JS library of your own" 4. Improve cross-browser event operations

Source: Internet
Author: User

1. Stop bubbling.

function Stoppropagation (event) {        = Event | | getEvent (event);         if (event.stoppropagation) {            event.stoppropagation ();        } Else {            true;        }    }    lily[' event ' = stoppropagation;

2. Block event default action.

function Preventdefault (event) {        = Event | | getEvent (event);         if (event.preventdefault) {            event.preventdefault ();        } Else {            false;        }    }    lily[' preventdefault '] = preventdefault;

3. Execute the function when the document has finished loading without waiting for the picture to be loaded.

functionaddloadevent (loadevent, waitforimages) {if(!iscompatible ())return false; if(waitforimages) {addevent (window,' Load ', loadevent); }        varinit =function (){            if(Arguments.callee.done)return; Arguments.callee.done=true;        Loadevent.apply (document, arguments);        }; if(Document.addeventlistener) {Document.addeventlistener ("Domcontentloaded", Init,false); }        if(/webkit/i.test (navigator.useragent)) {            varTimer = SetInterval (function (){                if(/complete|loaded/. Test (Document.readystate))                    {Clearinteval (timer);                Init (); }            }, 10); }        /*@cc_on @*/        /*@if (@_win32) document.write ("<src id=__ie_loaded defer src=//:><\/script>");        var script = document.getElementById ("__ie_loaded"); Script.onreadystatechange = function () {if (This.readystate = = "complete") {This.onreadystatecha                Nge = null;            Init ();        }        }; /* @end @*/        return true; } lily[' addloadevent '] = addloadevent;

4. Get target object.

function Gettarget (event) {        = Event | | getEvent (event);         var target = Event.target | | event.srcelement;         if (Target.nodetype = = Lily.node.TEXT_NODE)            {= target.parentnode        ;        } return target;    }    lily[' gettarget '] = gettarget;

5. Determine which key of the mouse is pressed.

functionGetmousebutton (Event) {event= Event | |getEvent (event); varbuttons = {            ' Left ':false,            ' Middle ':false,            ' Right ':false        }; if(Event.tostring && event.tostring (). IndexOf (' MouseEvent ')!! =-1){            Switch(Event.button) { Case0: Buttons.left=true;  Break;  Case1: Buttons.middle=true;  Break;  Case2: Buttons.right=true;  Break; default:                     Break; }        }Else if(Event.button) {Switch(Event.button) { Case1: Buttons.left=true;  Break;  Case2: Buttons.right=true;  Break;  Case3: Buttons.left=true; Buttons.right=true;  Break;  Case4: Buttons.middle=true;  Break;  Case5: Buttons.left=true; Buttons.middle=true;  Break;  Case6: Buttons.middle=true; Buttons.right=true;  Break;  Case7: Buttons.left=true; Buttons.middle=true; Buttons.right=true;  Break; default:                     Break; }        }Else{            return false; }        returnbuttons; } lily[' Getmousebutton '] = Getmousebutton;
View Code

6. Get the position of the mouse in the document.

 function   Getpositionindocument (event) { Event  = Event | |         GetEvent (event);  var  x = Event.pagex | |  (Event.clientx  + document.documentElement.scrollLeft) | |  (Event.clientx  + Document.body.scrollL        EFT);  var  y = Event.pagey | |  (Event.clienty  + document.documentElement.scrollTop) | |  (Event.clienty  + Document.body.scrollTo        p);     return  {' x ': x, ' y ' : y}; } lily[ ' getpositionindocument '] = getpositionindocument; 

7. Gets the value of the key.

function getkeypressed (event) {        = Event | | getEvent (event);         var code = Event.keycode;         var value = String.fromCharCode (code);         return {' Code ': Code, ' Value ': value};    }    lily[' getkeypressed '] = getkeypressed;

"Write a JS library of your own" 4. Improve cross-browser event operations

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.