Home >Web Front-end >JS Tutorial >Detailed explanation of focus, mouse and wheel events in JavaScript event types_javascript skills

Detailed explanation of focus, mouse and wheel events in JavaScript event types_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:18:051511browse

This article organizes the key points of "focus, mouse and wheel events" under "Event Type" in JavaScript events and shares them with you for your reference. The specific content is as follows

1. Focus events

Generally, these events are used in conjunction with the document.hasFocus() method and the document.activeElement property. Mainly include:

  • blur: The element loses focus and will not bubble;
  • DOMFocusIn: Same as HTML event focus, abandoned in DOM3, use focusin;
  • DOMFocusOut: Same as HTML event blur, abandoned in DOM3, use focusout;
  • focus: The element gets focus and will not bubble back;
  • focusin: Get focus, which is equivalent to the HTML event focus, but will bubble;
  • focusout: lose focus, equivalent to HTML event blur;

For example:

window.onfocus = function () {
  console.log('focus'); //focus
  console.log(document.hasFocus()); //True
}
window.onblur = function () {
  console.log('blur'); //blur
  console.log(document.hasFocus()); //False
}

When the focus moves from one element to another on the page, the following event will be triggered:

focusout --> focusin --> blur --> DOMFocusOut --> focus --> DOMFocusIn

2. Mouse events

9 mouse events are defined in DOM level 3 events:

  • click
  • dblclick
  • mousedown: Triggered when the user presses any mouse button. This event cannot be triggered through the keyboard;
  • mouseup: triggered when the user releases the mouse button. This event cannot be triggered through the keyboard;
  • mousemove: This event cannot be triggered through the keyboard;
  • mouseenter: does not bubble, and will not trigger when the cursor moves to descendant elements;
  • mouseleave: does not bubble, and will not trigger when the cursor moves to descendant elements;
  • mouseover: Triggered when the cursor moves to descendant elements;
  • mouseout: Triggered when the cursor moves to descendant elements;

For example:

div.onmouseover = function() {
  console.log('mouseover'); //在子元素上会触发
}
div.onmouseout = function() {
  console.log('mouseout'); //在子元素上会触发
}
div.onmouseenter = function() {
  console.log('mouseenter'); //在子元素上不会触发
}
div.onmouseleave = function() {
  console.log('mouseleave'); //在子元素上不会触发
}

Only when the mousedown and mouseup events are divided successively on the same element, the click event will be triggered; only when the click event is triggered twice, the dblclick event will be triggered in sequence.

The order is as follows:

mousedown --> mouseup --> click --> mousedown --> mouseup --> click --> dblclick

There is a bug in versions before IE8. In the double-click event, the second mousedown and click events will be skipped

3. Roller event

1. Client area coordinate position clientX and clientY attributes

For example:

window.onmousemove = function() {
    clickX = event.clientX;
    clickY = event.clientY;
    var div = document.createElement("img");
    div.src = "hhh.gif"
    div.style.position = "absolute";
    div.style.width = '100px';
    div.style.left = clickX + "px";
    div.style.top = clickY + "px";
    document.body.appendChild(div);
};

2. Page coordinate positions pageX and pageY;

window.onclick = function() {
    clickX = event.pageX;
    clickY = event.pageY;
    var div = document.createElement("img");
    div.src = "ppp.png"
    div.style.position = "absolute";
    div.style.width = '100px';
    div.style.left = clickX + "px";
    div.style.top = clickY + "px";
    document.body.appendChild(div);
};

This page coordinate position is not supported in IE8 and earlier versions. It can be calculated by using the scrollLeft and scrollTop attributes in document.body in mixed mode and document.documentElement in standard mode:

if (clickX === undefined) {
  clickX = event.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft);
};
if (clickY === undefined) {
  clickY = event.clientY + (document.body.scrollTop || document.documentElement.scrollTop);
};

3. Screen coordinate positions screenX and screenY;

Through this attribute, you can get the coordinates relative to the screen.

4. Modification keys

Modification keys include Shift, Ctrl, Alt, and Meta (Windows key on Windows, Cmd key on Mac); the corresponding modifier key states are shiftKey, ctrlKey, altKey, and metaKey. These attributes contain Boolean values. , true if the corresponding key is pressed, false otherwise. Such as:

var array = [];
var timing = setTimeout(showArray, 100);

window.onclick = function() {
  if (event.shiftKey) {
    array.push("shift");
  };
  if (event.ctrlKey) {
    array.push("ctrl");
  };
  if (event.altKey) {
    array.push("alt");
  };
  if (event.metaKey) {
    array.push("meta");
  };
};

function showArray() {
  var str = array.toString();
  var newP = document.createElement("p");
  newP.appendChild(document.createTextNode(str));
  document.body.appendChild(newP);
  timing = setTimeout(showArray, 1000);
}

5. Related elements

event.relatedTarget and event.target;

The relatedTarget attribute provides information about related elements. This attribute only contains values ​​for mouseover and mouseout events; for other events, the value is null; versions before IE8 do not support the relatedTarget attribute. When the mouseover event is triggered, the relevant elements are saved in the fromElement attribute of IE; when the mouseout event is triggered , the relevant elements are stored in IE's toElement attribute.

For example:

var dot = document.getElementById("dot");
dot.onmouseout = function (){
  console.log("mouse out from" + event.target.tagName + "to" + event.relatedTarget.tagName);
};

For example:

function getRelatedTarget() {
  if (event.ralatedTarget) {
    return event.relatedTarget;
  } else if (event.toElement) {
    return event.toElement;
  } else if (event.fromElement) {
    return event.fromElement;
  } else {
    return null;
  }
}

四、鼠标按钮

1、button属性

DOM的event.button属性有三个值:0为主鼠标按钮、1为中间鼠标按钮、2为次鼠标按钮。在常规设置中,主鼠标按钮就是鼠标左键;次鼠标按钮就是鼠标右键。

IE8及之前的版本也提供了button属性,但这个属性的值与DOM的button属性有很大差异:

  • 0:没有按下鼠标按钮;
  • 1:主鼠标按钮;
  • 2:次鼠标按钮;
  • 3:同时按下主鼠标按钮和次鼠标按钮;
  • 4:中间鼠标按钮;
  • 5:同时按下主鼠标按钮和中间鼠标按钮;
  • 6:同时按下次鼠标按钮和中间鼠标按钮;
  • 7:同时按下三个鼠标按钮

兼容版:

function getButton() {
  if (document.implementation.hasFeature("MouseEvents", "2.0")) {
    return event.button;
  } else {
    switch (event.button) {
      case 0:
      case 1:
      case 3:
      case 5:
      case 7:
        return 0;
      case 2:
      case 6:
        return 2;
      case 4:
        return 1;
    }
  }
}

另外,如果要屏蔽鼠标右键,应该使用:

document.oncontextmenu = function(event) {
  // if (window.event) {
  //   event = window.event;
  // }
  // try {
  //   var the = event.srcElement;
  //   if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
  //     return false;
  //   }
  //   return true;
  // } catch (e) {
  //   return false;
  // }
  return false;
}

这个事件是HTML5定义的,以后再讨论

2、更多的事件信息

detail属性

对于鼠标事件来说,detail包含了一个数值,表示在给定位置上发生了多少次单击(一次mousedown和一次mouseup),次数从1开始计数,如果mousedown和mouseup之间移动了位置,detail会被重置0,如果单击间隔太长也会被重置为0.

3、鼠标滚轮事件

mousewheel事件和wheelDelta属性

在垂直放向上滚动页面时,就会触发mousewheel,event对象里面的wheelDelta属性则表示当用户向前滚动滚轮时,wheelDelta是120的倍数;当向后滚动滚轮时,wheelDelta是-120的倍数。如:

var clientHeight = document.documentElement.clientHeight;
var divs = document.getElementsByTagName("div");
for (var i = 0; i < divs.length; i++) {
  divs[i].style.height = clientHeight + "px";
};

window.onmousewheel = function () {
  if (event.wheelDelta <= -120) {
    window.scrollBy(0,clientHeight);
  }else if(event.wheelDelta >= 120){
    window.scrollBy(0,-clientHeight);
  };
}

另外,在Opera 9.5之前的版本中,wheelDelta值的正负号是颠倒的。

此外,Firefox支持一个名为DOMMouseScroll的类似事件,也是在鼠标滚动滚轮时除法。有关鼠标滚轮的信息保存在detail属性中。向前滚动这个属性的值为-3的倍数,向后滚动,这个属性的值是3的倍数。

通用版:

function getWheelDelta() {
  if (event.wheelDelta) {
    return (client.engine.opera && client.engine.opera < 9.5 &#63; -event.wheelDelta : event.wheelDelta);
  } else {
    return -event.detail * 40;
  };
}

4、触摸设备

iOS和Android设备中:

  • 不支持dblclick;
  • 轻击可单击元素会触发mousemove;如果此操作会导致内容变化,将不再有其他事件发声;如果屏幕没有发生变化,那么依次发生mousedown、mouseup和click事件;
  • mousemove事件也会触发mouseover和mouseout事件;
  • 两个手指操作会触发mousewheel和scroll;

5、无障碍性问题

  • 使用click事件执行代码;
  • 不要使用onmouseover向用户显示新的信息;
  • 不要使用dblclick执行重要的操作;

以上就是本文的全部内容,希望对大家的学习有所帮助。

Statement:
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