当前位置:网站首页>mouseEvent事件——mouse坐标描述——focus事件——input事件——节流(thorttle)——mouseWheel(滚轮事件)
mouseEvent事件——mouse坐标描述——focus事件——input事件——节流(thorttle)——mouseWheel(滚轮事件)
2022-07-25 21:42:00 【勇敢*牛牛】
mouseEvent事件
| mouseEvent | 事件 |
|---|---|
| click | 点击 //PointerEvent 继承MouseEvent |
| dblclick | 双击 |
| mousedown | 鼠标按下键 |
| mouseup | 鼠标释放键 |
| mouseover | 鼠标滑过 子元素会收到事件并且向上冒泡 |
| mouseout | 鼠标滑出 子元素会收到事件并且向上冒泡 |
| mouseenter | 鼠标进入 子元素不会收到事件也不会冒泡 |
| mouseleave | 鼠标离开 子元素不会收到事件也不会冒泡 |
| mousemove | 鼠标移动 |
| contextmenu | 右键菜单 |
.div1{
width: 100px;
height: 100px;
background-color: red;
position: relative;
top: 3000px;
}
.div2{
width: 50px;
height: 50px;
background-color: yellow;
margin: 50px;
margin: auto;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
/* left: 25px; top: 25px; */
}
</style>
</head>
<body>
<div class="div1">
<div class="div2"></div>
</div>
<script>
var div1=document.querySelector(".div1");
div1.addEventListener("click",clickHandler);
function clickHandler(e){
// e.type
console.log(e.type);//事件类型
console.log(e)
</script>
知识卡片
cancelBubble: false取消冒泡,设置为true取消冒泡 兼容写法currentTarget事件监听的对象 等同于thistarget srcElement事件触发的目标对象
关于mouse位置的几个坐标描述
| 方法 | 描述 |
|---|---|
| clientX: 63clientY: 60: | 相对视口距离 视口就是当前可视窗口左上角 |
| x: 63 y: 60 | 相对视口距离 视口就是当前可视窗口左上角 |
| layerX: 30 | 如果目标元素没有定位时,这个是相对父级的定位元素的左上角距离 |
| layerY: 27 | 如果目标元素定位时,相对目标元素的左上角距离 |
| offsetX: 30offsetY: 28 | 相对e.target 目标元素的左上角距离 |
| movementX,movementY | 鼠标相对上次移动的距离 向上和左都是负数,右和下都是正数 |
| pageX,pageY | 鼠标相对页面的左上角距离 |
| screenX,screenY | 鼠标相对屏幕左上角距离 |
focus聚焦事件
表单元素和超链接 可以接受聚焦事件
form不接受input的focus冒泡事件,也就是说点击哪个就是那个
<form action="#">
<input type="text">
<input type="password">
</form>
<script>
var input=document.querySelector("input");
var pass=document.querySelector("[type=password]");
var form=document.querySelector("form");
input.addEventListener("focus",focusHandler);
pass.addEventListener("focus",focusHandler);
function focusHandler(e){
console.log(e)
// e.relatedTarget 上一次聚焦的对象
}
</script>
blur 失焦事件,也没有冒泡发生
<form action="#">
<input type="text">
<input type="password">
</form>
<script>
var input=document.querySelector("input");
var pass=document.querySelector("[type=password]");
var form=document.querySelector("form");
pass.addEventListener("blur",focusHandler)
input.addEventListener("blur",focusHandler)
function focusHandler(e){
console.log(e)
// e.relatedTarget 上一次聚焦的对象
}
</script>
focusin focusout 表单的聚焦事件,一般支持冒泡
<form action="#">
<input type="text">
<input type="password">
</form>
<script>
var input=document.querySelector("input");
var pass=document.querySelector("[type=password]");
var form=document.querySelector("form");
form.addEventListener("focusin",focusHandler);
form.addEventListener("focusout",focusHandler);
function focusHandler(e){
console.log(e)
// e.relatedTarget 上一次聚焦的对象
}
</script>
input事件
| 方法 | 描述 |
|---|---|
| e.data: “s” | 当前输入的内容 |
| e.inputType:“insertCompositionText” | 输入类型 |
| e.isComposing:true | 是否开启了输入法 |
节流(thorttle):
高频事件触发
每次触发事件时设置一个延迟调用方法
并且取消之前的延时调用方法。
概述:每次触发事件时都会判断是否等待执行的延时函数。
<form action="#">
<input type="text">
<input type="password">
</form>
<script>
var ids; //开关
var input=document.querySelector("input");
input.addEventListener("input",inputHandler);
// 节流
// 每次高频触发事件时,设置一个延迟执行,在这段时间内部继续触发,
// 直到上次的延迟执行完成后,再次触发
function inputHandler(e){
// ids默认是undefined,进入后转换为布尔值不为真,所以不跳出
if(ids) return;
// setTimeout会返回一个非0的数值,这个数值是针对这次setTimeout的堆中的回调函数索引
// 通过clearTimeout删除这个索引所对应的函数
ids=setTimeout(function(){
// 根据索引删除setTimeout对应的回调函数
clearTimeout(ids);
// 将ids变量重新设置为undefined,这样就可以再次进入这个input事件了
ids=undefined;
// 打印input的结果
console.log(input.value);
},500)
}
</script>
mouseWheel(滚轮事件)
<script>
document.addEventListener("DOMMouseScroll",mousewheelHandler);//火狐浏览器
document.addEventListener("mousewheel",mousewheelHandler);//谷歌浏览器
function mousewheelHandler(e){
// console.log(e)
/*统一向上是1,向下是-1*/
var detail;
if(e.deltaY){
detail=e.deltaY/Math.abs(e.deltaY);
}else{
detail=e.detail/Math.abs(e.detail);
}
console.log(detail)
/* mousewheel 向上 deltaX: -0 deltaY: 2 deltaZ: 0 detail: 0 wheelDelta: -6 wheelDeltaX: 0 wheelDeltaY: -6 DOMMouseScroll 向上 detail: 1 */
}
</script>
边栏推荐
- Web3 entrepreneurship has all the elements of explosive growth of innovation
- 字节一面:TCP 和 UDP 可以使用同一个端口吗?
- QT | learn about QT creator by creating a simple project
- I'm also drunk. Eureka delayed registration and this pit!
- How to store pictures in the database "suggested collection"
- MPI learning notes (II): two implementation methods of matrix multiplication
- Protobuf的简单使用
- 开源协议是否具有法律效力?
- Ability to choose
- 【面试:并发篇24:多线程:综合练习】顺序控制
猜你喜欢
![[MAIXPY]kpu: load error:2005, ERR_ READ_ File: read file failed problem solving](/img/0b/da67b5a361a2cdfaf81568d34cf5f7.png)
[MAIXPY]kpu: load error:2005, ERR_ READ_ File: read file failed problem solving

【饭谈】那些看似为公司着想,实际却让人无法理解的事(二:面试时的软素质“眼缘”)

Pyqt5 use pyqtgraph to draw multiple y-value scatter plots

How to solve the problem of high concurrency and large traffic with PHP

工作面试总遇秒杀? 看了京东 T8 大咖私藏的秒杀系统笔记, 已献出膝盖

浅谈web性能优化(一)

Oxford University: many common insomnia drugs lack long-term safety data

When facing complex problems, systematic thinking helps you understand the essence of the problem

919. Complete binary tree inserter: simple BFS application problem

字节一面:TCP 和 UDP 可以使用同一个端口吗?
随机推荐
五、品达通用权限系统__pd-tools-xxs(防跨站脚本攻击)
MySQL master-slave configuration
Detailed explanation of JVM memory model and structure (five model diagrams)
选择的能力
零基础学习CANoe Panel(17)—— Panel CAPL Function
GPON introduction and Huawei OLT gateway registration and configuration process
Record the transfer of domain names from Alibaba cloud service providers to Huawei cloud
Detailed explanation of several ideas for implementing timed tasks in PHP
Guys, how can Flink SQL submit tasks in per job mode?
【饭谈】如何设计好一款测试平台?
PE format: analyze and implement IATHOOK
【面试:并发篇25:多线程:volatile】可见性
Huawei occupies half of the folding mobile phone market, proving its irreplaceable position in the high-end market
Apple estimates that iPhone will give up the Chinese market, and the Chinese industrial chain needs to consider living a hard life
PE格式: 分析IatHook并实现
Why do independent sellers like to do e-mail marketing? The original conversion rate can be improved so much!
JMeter distributed pressure measurement
Sentinel vs Hystrix 限流对比,到底怎么选?
Autojs learning - realize 3D perspective
【饭谈】测试平台为什么有组件化?模块化?很多看不到的地方设计的很好是种浪费么?