JS中监听鼠标滚轮事件

  • 使用jQuery监听鼠标滚轮事件:
<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>监听鼠标滚轮事件</title>
	</head>

	<body>
		<script type="text/javascript" src="js/jquery-1.11.3.js"></script>
		<script type="text/javascript">
			$(document).bind('mousewheel DOMMouseScroll', function(event) {
				// IE/Chorme
				var wheel = event.originalEvent.wheelDelta;
				// Firefox
				var detal = event.originalEvent.detail;
				// 判断浏览器IE,谷歌滚轮事件
				if(event.originalEvent.wheelDelta) {
					// 当滑轮向上滚动时
					if(wheel > 0) {
						console.log("up");
					}
					// 当滑轮向下滚动时
					if(wheel < 0) {
						console.log("down");
					}
				}
				// Firefox滚轮事件  
				else if(event.originalEvent.detail) {
					// 当滑轮向下滚动时
					if(detal > 0) {
						console.log("down");
					}
					// 当滑轮向上滚动时
					if(detal < 0) {
						console.log("up");
					}
				}
			});
		</script>
	</body>

</html>

如您在阅读中发现不足,欢迎留言!!!

发布了63 篇原创文章 · 获赞 66 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40065776/article/details/99235725