Advanced events
1, Registration event (binding event)
- Traditional registration events are unique. Only one function can be set, and the later registered function will overwrite the new function
- Method listening registration method: addEventListener(type,listener[,useCapture])
(1) The event type inside is a string, which must be quoted without on
(2) Multiple listeners can be added to the same element and event
① Type: event type string, such as click and mouseover. Note that it does not take on
② listener: event handling function, which will be called when an event occurs
③ useCapture: optional parameter. It is a Boolean value. The default value is false.
2, Delete event (unbind event)
1. Add div.onclick = null inside the function for traditional deletion events
2. Register event deletion: the removeEventListener(type, function) function does not need to add parentheses
<div>1</div> <script> var div1 = document.querySelector('div'); div1.addEventListener = ('click', fn) function fn() { alert('hi~'); div1.removeEventListener('click', fn); } </script>
3, DOM event flow -- event propagation process
- Capture phase
- Current target stage
- bubbling phase
※ attention
① js code can only execute one of the capture or bubble phases
② onclick and attachEvent can only get the bubbling phase
③ If the third parameter of addEventListener is true, it means to call the event handler in the event capture phase; If false, it means that the event handler is called during the event bubbling phase
④ Event capture is rarely used in actual development, and event bubbling is more concerned
⑤ There is no bubble in some time, such as onblur, ommouseover, ommouseleave
4, Event object
div.onclick = function(event){}
(1) Concept: event means that the event object is written into the parentheses of the listening function when the form is referenced
(2) Usage:
- The event object exists only when there is an event. It is automatically created by the system. We don't need to pass parameters to the event listener in turn
- The event object is the collection of a series of related data of our event, and the information related to the event
- This event object can be named by ourselves, such as event, evt, e, etc
(3) Common properties and methods
- e.target: returns the element that triggers the event, and this returns the element that binds the event.
- e.type: returns the event type, such as click, mouseover, without on
- e.preventDefault(): prevents default events, such as not allowing links to jump
<a href="https://www.baidu. COM / "> Baidu</a> <script> var aa = document.querySelector('a'); aa.addEventListener('click', function(e) { e.preventDefault(); }) </script>
- e.stopPropagation(): stop bubbling
<div>me</div> <script> var me = document.querySelector('div'); me.addEventListener('click', function(e) { alert('I played it out'); e.stopPropagation(); }); document.addEventListener('click', function(e) { alert('I played it out, too'); }); </script>
5, Event delegation
- Principle: instead of setting the event listener separately for each child node, the event listener is set on its parent node, and then the bubble principle is used to affect the setting of each child node
<ul> <li>I have a pop-up window</li> <li>I have a pop-up window</li> <li>I have a pop-up window</li> <li>I have a pop-up window</li> <li>I have a pop-up window</li> </ul> <script> var ul = document.querySelector('ul'); ul.addEventListener('click', function(e) { alert('I played it out'); // e.target can get the clicked object, so it can be used to set a separate style e.target.style.backgroundColor = 'red'; // If you want to achieve a change in color, you can review the idea of exclusivity }) </script>
6, Common mouse events
(1)
- contextmenu: disable context menu
- selectstart: disable mouse selection
- mousemove: mouse movement event
(2) Mouse event object MouseEvent
- clientX, clientY: coordinates relative to the viewable area of the browser window
- pageX, pageY: coordinates relative to the edge of the document
- screenX, screenY: the coordinates of the mouse on the computer screen
7, Common keyboard events
(1) Three event execution sequences: Keydown keypress Keyup
- keyup: triggered when the keyboard pops up. It is not case sensitive
- keydown: triggered when the key is pressed. It is not case sensitive
- keypress: triggered when the key is pressed (function keys are not recognized, such as ctrl, shift, etc.), which is case sensitive
(2) keyCode: ASCII code value to judge which key the user presses
case
Picture follow mouse
Knowledge points: mouse move event mousemoveCore idea
- Using mouse event mousemove to get the position of mouse in real time
- Set two variables, x and y, and assign the real-time coordinates of the mouse to x and y
- Set relative positioning for the picture (if there is no absolute positioning, the whole browser will be used for positioning, and the relative positioning does not occupy the position). y and x are assigned to top and left respectively
Note: when assigning the values of x and y to top and left, the unit px should be added in the way of splicing strings. Otherwise, there is no effect.
code
html partcss part<img src="image/arrow.png" alt="">
javascript part<style> img { position: absolute; top: 0; left: 0; } </style>
<script> var jian = document.querySelector('img'); document.addEventListener('mousemove', function(e) { // As long as the mouse moves 1px, the mousemove event is triggered // Each time we move the mouse, we need to obtain the coordinates of the mouse in real time and assign them to the top and left of img var x = e.pageX; var y = e.pageY; jian.style.left = x + 'px'; // Both the top value and the left value have units (e.g. top: 2px). Therefore, it is necessary to add units in the form of concatenated strings in order to operate normally jian.style.top = y + 'px'; // To center the picture, just subtract half the size of the picture after x and y }) </script>
Express order No. query
Knowledge points: keyboard eventsCore idea
- When entering content, the font box (con) above displays (larger font)
- Form detection user input: add keyboard event
- Assign the value of the document number to the font box
- If the document number is empty, the font box is hidden
code
html partcss part<div class="bg"> <div class="con"></div> <input type="text" placeholder="123"> </div>
javascript part<style> * { margin: 0; padding: 0; border: 0; box-sizing: border-box; } .bg { position: relative; width: 300px; margin: 100px auto; } .con { display: none; width: 200px; height: 30px; line-height: 30px; font-size: 25px; border: 1px solid #ccc; } .con::before { content: ''; width: 0; height: 0; position: absolute; top: 29px; left: 15px; border: 8px solid #ccc; border-style: solid dashed dashed; border-color: #ccc transparent transparent; } input { position: absolute; top: 36px; left: 0; width: 200px; outline: none; border: 1px solid #999; } </style>
<script> var con = document.querySelector('.con'); var ipt = document.querySelector('input'); ipt.addEventListener('keyup', function(e) { // When the form is not empty, the upper box is displayed and the value is assigned to the box if (ipt.value != '') { con.style.display = 'block'; var v = ipt.value; con.innerHTML = v; } else { con.style.display = 'none'; } }) // If the form loses focus, the box is hidden ipt.addEventListener('blur', function(e) { con.style.display = 'none'; }) </script>
effect
By default, the top is not displayed
Lose focus and don't show
JD key input
Knowledge points: keyboard eventsCore idea
- Judge whether the user presses the s key. If it is pressed, it will be located in the search
- Use keycode to judge the key pressed
- The search box gets the focus, and the focus() method
code
html partjavascript part<input type="text" placeholder="mobile phone">
<script> var ipt = document.querySelector('input'); document.addEventListener('keyup', function(e) { // Here, both keyup and keydown are OK, but keydown is triggered when pressed. s will be input into input. It will be better to use keyup if (e.keyCode == 83) { ipt.focus(); } }) </script>