onwheel 事件
当用户在 <div> 元素上滚动鼠标滚轮时执行 JavaScript:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
#myDIV {
border: 1px solid black;
}
</style>
</head>
<body>
<div id="myDIV">该实例演示了如何使用 addEventListener() 方法向 DIV 元素添加 "wheel" 事件。在这个区域上下滚动鼠标滚轮!</div>
<p>当你在 div 元素中滚动鼠标滚轮时,脚本函数会设置 div 的字体大小为 35 像素。</p>
<p><strong>注意:</strong> Safari 浏览器不支持 wheel 事件</p>
<script>
document.getElementById("myDIV").addEventListener("wheel", myFunction);
function myFunction() {
document.getElementById("myDIV").style.fontSize = "35px";
}
</script>
</body>
</html>
定义和用法
onwheel 事件在鼠标滚轮在元素上下滚动时触发。
onwheel 事件同样可以在触摸板上滚动或放大缩小区域时触发(如笔记本上的触摸板)。
浏览器支持
事件 | |||||
---|---|---|---|---|---|
onwheel | 31.0 | 9.0 | 17.0 | 不支持 | 18.0 |
注意: 在 IE 浏览器中,只能通过 addEventListener() 方法支持 wheel 事件。 在 DOM 对象中没有 onwheel 属性。
语法
HTML 中:
<element onwheel="myScript">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
#myDIV {
border: 1px solid black;
}
</style>
</head>
<body>
<div id="myDIV">该实例演示了如何向 DIV 元素添加 "wheel" 事件。在这个区域上下滚动鼠标滚轮!</div>
<p>当你在 div 元素中滚动鼠标滚轮时,脚本函数会设置 div 的字体大小为 35 像素。</p>
<p><strong>注意:</strong> Internet Explorer 和 Safari 浏览器不支持 wheel 事件</p>
<script>
function myFunction() {
document.getElementById("myDIV").style.fontSize = "35px";
}
</script>
</body>
</html>
JavaScript 中:
object.onwheel = function() {
myScript
};
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
#myDIV {
border: 1px solid black;
}
</style>
</head>
<body>
<div id="myDIV">该实例演示了如何使用 HTML DOM 向 DIV 元素添加 "wheel" 事件。在这个区域上下滚动鼠标滚轮!</div>
<p>当你在 div 元素中滚动鼠标滚轮时,脚本函数会设置 div 的字体大小为 35 像素。</p>
<p><strong>注意:</strong> Internet Explorer 和 Safari 浏览器不支持 wheel 事件</p>
<script>
document.getElementById("myDIV").onwheel = function() {
myFunction()
};
function myFunction() {
document.getElementById("myDIV").style.fontSize = "35px";
}
</script>
</body>
</html>
JavaScript 中, 使用 addEventListener() 方法:
object.addEventListener("wheel", myScript);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
#myDIV {
border: 1px solid black;
}
</style>
</head>
<body>
<div id="myDIV">该实例演示了如何使用 addEventListener() 方法向 DIV 元素添加 "wheel" 事件。在这个区域上下滚动鼠标滚轮!</div>
<p>当你在 div 元素中滚动鼠标滚轮时,脚本函数会设置 div 的字体大小为 35 像素。</p>
<p><strong>注意:</strong> Safari 浏览器不支持 wheel 事件</p>
<script>
document.getElementById("myDIV").addEventListener("wheel", myFunction);
function myFunction() {
document.getElementById("myDIV").style.fontSize = "35px";
}
</script>
</body>
</html>
注意: Internet Explorer 8 及更早 IE 版本不支持 addEventListener() 方法。
技术细节
是否支持冒泡: | Yes |
---|---|
是否可以取消: | Yes |
事件类型: | WheelEvent |
支持的 HTML 标签: | All HTML elements |
