Javascript events: about custom events

Source: Internet
Author: User

Javascript events: about custom events
The most impressive thing about JavaScript custom events is that jQuery is used for image lazy loading. Define an appear event for images that require lazy loading. This custom appear event is triggered when the page image appears. (Note that this event is triggered only once ). Now we can talk about custom events through JavaScript. The so-called event is to call a function at an appropriate time. Events are commonly used. A method bound to an element triggers a behavior, such as click, focus, mouseover, mouseout, load ......) this function is triggered. This is also true for custom events, but the trigger method can be determined by yourself. For example, the appear event we mentioned above triggers a custom method when this element is visible. We use JavaScript to simulate it. // Define two methods to add events to the element and trigger event 1 function add (el, type, fn) {2 el. listeners = el. listeners | {} 3 el. listeners [type] = el. listeners [type] | [] 4 el. listeners [type]. push (fn) 5 6 el. addEventListener (type, fn, false); 7} 8 9 function trigger (el, type) {10 if (el. listeners) {11 var triggerArr = el. listeners [type] | []; 12 if (triggerArr. length) {13 for (var I = 0; I <triggerArr. length; I ++) {14 triggerArr [I] (); 15} 16} 17} 18} bind the event and trigger 1 function doFn () {2 alert ("appear trigger pop-up! ") 3} 4 function doFn2 () {5 alert (" appear trigger pop-up 2! ") 6} 7 8 add ($ doTrigger," doTrigger ", doFn) 9 add ($ doTrigger," doTrigger ", doFn2) 10 trigger ($ doTrigger," doTrigger ") page loading, pop-up, "appear trigger pop-up! "," Appear trigger pop-up 2! ". The implementation principle is very simple. Add a listeners attribute for the element. The default value is {}. Add a type attribute with the default value [] to the object. Get el. listeners [type] directly when triggering. Trigger each method in it. Delete simpler 1 function remove (el, type, fn) {2 if (el. listeners & el. listeners [type]) {3 delete el. listeners [type] 4} 5 el. removeEventListener (type, fn, false) for default events, we also test add ($ clickTrigger, "click", clickFn) add ($ clickTrigger, "click", clickFn1) trigger ($ clickTrigger, "click") function clickFn () {alert ("click trigger pop-up! ")} Function clickFn1 () {alert (" click trigger pop-up! ")} Page 1 is displayed. Click "click Event. Remove ($ clickTrigger, "click", clickFn) remove ($ clickTrigger, "click", clickFn1) after removing, the page does not pop up when it is loaded or clicked. Note that the above Code is based on w3c annotations. We will talk about event compatibility later.

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.