search
HomeWeb Front-endJS TutorialOrganizing common event types in JavaScript

This article brings you relevant knowledge about javascript, which mainly introduces related issues about common JavaScript event types, including mouse events, keyboard events, etc. Let’s talk about it together Take a look, hope it helps everyone.

Organizing common event types in JavaScript

[Related recommendations: javascript video tutorial, web front-end

Mouse event

Event Type

  • click: The user clicks the main mouse button (usually the left button) or presses the Enter key while focusing Triggered when
  • dblclick: Triggered when the user double-clicks the main mouse button (frequency depends on the system configuration)
  • mousedown: Triggered when the user presses any mouse button
  • mouseup: Triggered when the user lifts Triggered when any mouse button is pressed
  • mousemove: Triggered when the mouse moves on the element
  • mouseover: Triggered when the mouse enters the element
  • mouseout: Triggered when the mouse leaves the element
  • mouseenter: Triggered when the mouse enters the element, the event will not bubble
  • mouseleave: Triggered when the mouse leaves the element, the event will not bubble

Difference:

  • over and out, regardless of the child element, moving from the parent element to the child element, for the parent element, is still counted as leaving
  • enter and leave, consider child elements, which are still part of the parent element
  • mouseenter and mouseleave will not bubble

Event object

All mouse events and event objects in the event handler are MouseEvent

  • altKey: When the event is triggered, whether the alt key of the keyboard is pressed
  • ctrlKey: When the event is triggered, whether the ctrl key of the keyboard is pressed
  • shiftKey: When the event is triggered, whether the shift key of the keyboard is pressed
  • button: When the event is triggered, Mouse button type
    • 0: Left button
    • 1: Middle button
    • 2: Right button

##Position

    page: pageX, pageY, the horizontal and vertical coordinates of the current mouse distance from the page
  • client: clientX, clientY, the mouse is relative to the viewport The coordinates
  • offset: offsetX, offsetY, the coordinates of the mouse relative to the padding of the event source
  • screen: screenX, screenY, the mouse relative to the screen
  • x, y , equivalent to clientX, clientY
  • movement: movementX, movementY, only valid in mouse movement events, relative to the last mouse position, offset distance
Keyboard events

Event type

    keydown: Triggered by pressing any key on the keyboard. If pressed and held down, this event will be triggered repeatedly
  • keypress: Triggered when pressing a
  • character key on the keyboard
  • keyup: Triggered by lifting any key on the keyboard
keydown, keypress if blocked Without the event's default behavior, the text will not be displayed.

Event object

KeyboardEvent

    code: Get the key string and adapt to the keyboard layout.
  • key: Get the key string, which does not fit the keyboard layout. Can get printed characters.
  • keyCode, which: get the keyboard code

Form event

    focus: the element is focused Triggered when the element can interact with the user (all elements that can interact with the user can be focused), the event will not bubble
  • blur: Triggered when the element loses focus, the event will not bubble.
  • submit: Submit form event, only valid in form elements.
  • change: text change event
  • input: text change event, trigger immediately
Other events

window global object

    load, DOMContentLoaded, readystatechange
window load: event that all resources in the page have been loaded

Image load: event that image resources have been loaded

The process of browser rendering the page:

    Get the page source code
  1. Create the document node
  2. Add elements to the dom tree in sequence from top to bottom , each time an element is added, pre-rendering
  3. Renders the child nodes in sequence according to the structure
DOMContentLoaded of document: Occurs after the dom tree is built

readystate (the page has three states

): loading (loading), interactive (interactive), complete (complete)

interactive: trigger DOMContentLoaded event

complete: trigger Window's load event

readystatechange (triggered when the page state changes //returns to the changed state)

js code should be written to the bottom of the page as much as possible

  • css should be written to the top of the page: avoid flickering (if placed at the bottom of the page, it will cause the element to have no style first, use the ugly default style, and then when the css is read file, re-change the style)

  • JS should be written to the bottom of the page: to avoid blocking subsequent rendering, and to avoid not getting the content in the page when running JS element.

  • unload, beforeunload

beforeunload: window event, runs when the window is closed, and can prevent the window from closing
unload: window event , runs when the window is closed

  • scroll

Events run when the window scrolls

Through scrollTop and scrollLeft, the scroll distance can be obtained and set.

  • resize

Events run when the window size changes, monitoring the viewport size

  • contextmenu

Right-click menu event

  • paste

Paste event

  • copy

Copy event

  • cut

A few distance pictures
Organizing common event types in JavaScript
Organizing common event types in JavaScript
Organizing common event types in JavaScript
Organizing common event types in JavaScript

##Element position

    offsetParent
Get the first positioned ancestor element of an element, if not, then Get the offsetParent of body

body as null

    offsetLeft, offsetTop //The distance from the positioned element is the distance from body
relative to the element Coordinates of offsetParent

If offsetParent is body, treat it as the entire web page

    getBoundingClientRect method
This method gets an object, the object Recorded the distance of the element relative to the viewport

Event simulation

    click simulated click
  • sumbit simulation Submit form
  • dispatchEvent simulation event
Other supplements

    window.scrollX, window.pageXOffset, window.scrollY, window.pageYOffset
window.scrollX, window.pageXOffset: equivalent to the scrollLeft of the root element

window.scrollY, window.pageYOffset: equivalent to the scrollTop of the root element

    scrollTo, scrollBy
scrollTo: Set the scroll bar position //window.scrollTo(x, y) All dom objects can use

scrollBy: means to increase the x and y axis distance from the window based on the original. scrollBy(x, y)

    resizeTo, resizeBy
[Related recommendations:

javascript video tutorial, web front-end]

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

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools