Home > Web Front-end > JS Tutorial > Latest collection of common JavaScript event types

Latest collection of common JavaScript event types

php是最好的语言
Release: 2018-08-10 17:21:11
Original
2417 people have browsed it

UI (User Interface) Event, triggered when the user interacts with elements on the page

Load event: When the page is fully loaded, The load event on the window will be triggered.

Unload event: Triggered after the document is completely unloaded. Fires whenever the user switches from one page to another.

Resize event: When the browser adjusts to a new height or width, the resize event is triggered. Be careful not to add calculation logic to this event to avoid performance problems.

        EventUtil.addHandler(window, "resize", function (event) {
                //防抖函数
                //每次调用函数都会清除之前的计数器,并重新计数,计数完成时,执行操作。
            var timer;
            return function () {
                if (timer) {
                    clearTimeout(timer);
                }
                timer = setTimeout(function () { //setTimeout返回唯一标识符,可以使用它来取消计数器
                    console.log("123");
                },500);
            }
        }());//此处的双括号表示,立即调用返回值
Copy after login

Scroll event: When the user scrolls the content in the element with a flow bar, it is triggered on the element.

        EventUtil.addHandler(window, "scroll", function (event) {
            //函数节流
            var timer;
            return function () {
                if (timer) {
                    clearTimeout(timer);
                }
                timer = setTimeout(function () { //setTimeout返回唯一标识符,可以使用它来取消计数器
                    if (document.compatMode == "CSS1Compat") {
                        //标准模式
                        console.log("Left:" + document.documentElement.scrollLeft);
                        console.log("Top:" + document.documentElement.scrollTop);
                    }
                    else {
                        //混杂模式
                        console.log("Left:" + document.body.scrollLeft);
                        console.log("Top:" + document.body.scrollTop);
                    }
                }, 500);
            }     
        }());
Copy after login

Focus event, Triggered when the page gains or loses focus

Blur event: Triggered when the element loses focus. This event does not bubble; all browsers support it;

Focus event: triggered when the element gains focus. This event does not bubble up and is supported by all browsers;

Focusin event: triggered when an element gains focus. This event is equivalent to the HTML event focus, but it bubbles. The browsers that support this event are IE5, safari5.1, Opera11.5 and Chrome;

Focusout event: Triggered when the element is lost. This event is a universal version of the HTML event blur. The browsers that support this event are IE5 safari5.1 Opera11.5 and Chrome;

Mouse and wheel events

Click event: Triggered when the user clicks the left mouse button or presses the Enter key.

DbClick event: Triggered when the user double-clicks the left mouse button.

MouseDown event: Triggered when the user presses any mouse button.

MouseEnter event: Triggered when the mouse moves into the element range for the first time from outside.

MouseLeave event: Triggered when the mouse moves out of the scope of the element.

MouseMove event: Fired repeatedly when the mouse pointer moves inside the element.

MouseOut event: Triggered when the mouse pointer is over an element and the user moves it into another element.

MouseOver event: Triggered when the mouse pointer is outside an element and then the user moves it within the boundaries of another element for the first time.

MouseUp: Triggered when the user releases the mouse pointer.

Several mouse coordinate positions:

1. Client area coordinate position

Mouse events occur at specific positions in the browser viewport. This position is saved in the clientX and clientY properties of the event object.

2. Page coordinate position

pageX and pageY can tell you where the event occurred on the page. In other words, these two properties represent the position of the cursor in the page, so the coordinates are calculated from the page itself rather than from the left or top edge of the viewport.

When the page is not scrolled, pageX and pageY are equal to clientX and clientY.

IE8 and earlier versions do not support page coordinate position, but it can be calculated using the client area coordinate position and scroll information.

        EventUtil.addHandler(document.getElementById("p1"), "click", function (event) {
            //兼容IE8及之前版本
            event = EventUtil.getEvent(event);
            var pageX = event.pageX,
                pageY = event.pageY;
            if (pageX === undefined) {
                pageX = event.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft);
            }
            if (pageY === undefined) {
                pageY = event.clientY + (document.body.scrollTop || document.documentElement.scrollTop);
            }
            //console.log("pageX:" + pageX);
            //console.log("pageY:" + pageY);
        });
Copy after login

3. Screen coordinate position

The screenX and screenY properties can determine the coordinate information of the mouse pointer relative to the entire screen when a mouse event occurs.

Related recommendations:

Compilation of common JavaScript error-prone knowledge points

Detailed interpretation of event flows and events in JavaScript Processing program (graphic tutorial)

The above is the detailed content of Latest collection of common JavaScript event types. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template