如何检查鼠标是向上滚动还是向下滚动
How to check if mouse scroll top or bottom direction?
我正在尝试获取鼠标方向功能。我想知道如何检查鼠标是向上滚动还是向下滚动。
每次检查机身是否向上或向下滚动:
if(scroll top... ) {
do something.....
}
if(scroll bottom... ) {
do something.....
}
CSS:
body {
height:1400px;
margin:0;
padding:0;
position:relative;
}
.get-direction-info-here {
width:150px;
height:30px;
background:red;
display:block;
font-size:14px;
line-height:30px;
color:#fff;
position:fixed;
top:0;
}
HTML:
<div class="get-direction-info-here">
Scroll direction: <!-- Scroll TOP --> / <!--Scroll Bottom-->
</div>
<div class="direction-info"> </div>
您可以使用HTML5中的鼠标滚轮事件来检测鼠标滚轮动作。上/下由增量是正(向上滚动)还是负(向下滚动)来确定。
window.onload = function() {
var directionInfo = document.getElementById("directionInfo");
if (directionInfo.addEventListener) {
directionInfo.addEventListener("mousewheel", MouseWheelHandler, false);
directionInfo.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
}
else directionInfo.attachEvent("onmousewheel", MouseWheelHandler);
function MouseWheelHandler(e) {
// cross-browser wheel delta
var e = window.event || e;
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
if(delta > 0)
directionInfo.innerText = 'Up';
else
directionInfo.innerText = 'Down';
return false;
}
}
.direction-info {
width:100px;
height:100px;
border:1px solid green;
}
<div id="directionInfo" class="direction-info"> </div>
window.onload = function() {
var directionInfo = document.getElementById("directionInfo");
if (directionInfo.addEventListener) {
directionInfo.addEventListener("mousewheel", MouseWheelHandler, false);
directionInfo.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
}
else directionInfo.attachEvent("onmousewheel", MouseWheelHandler);
function MouseWheelHandler(e) {
// cross-browser wheel delta
var e = window.event || e;
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
if(delta > 0)
directionInfo.innerText = 'Up';
else
directionInfo.innerText = 'Down';
return false;
}
}
.direction-info {
width:100px;
height:100px;
border:1px solid green;
}
<div id="directionInfo" class="direction-info"> </div>
相关文章:
- JQuery UI可拖动潜水与滚动棒到鼠标
- 如何查明鼠标按下事件是否发生在滚动条上或元素中的其他任何位置
- fullpage.js和物化.js;使用实体侧导航禁用鼠标滚动
- 如何避免鼠标水平滚动选项卡面板溢出
- 如何在鼠标滚轮滚动上设置剪切遮罩的动画
- 如何使用鼠标滚动控制fullPage.js垂直滑块
- 使用鼠标滚轮水平滚动页面
- 网站跟随鼠标滚动
- 用鼠标逐个滚动页面
- 当用户将鼠标移到屏幕/图表上时,自动滚动将暂停.如果鼠标移动停止,自动滚动将再次恢复
- jQuery UI 对话框和滚动块竞争鼠标
- JQuery 自动滚动到底部(但在鼠标滚动时禁用)
- 我想要一个类似于网站 https://onlycoin.com/ 的jQuery鼠标滚动动画效果
- 禁用预测滚动 - 鼠标滚轮 (OnScroll) 事件触发过于频繁(触摸板)
- 在动态创建的 svg 元素上滚动鼠标时放大/缩小
- 垂直滚动鼠标时图像移动
- 停止滚动鼠标
- carouFredSel滚动鼠标按下
- 气泡滚动/鼠标滚轮事件
- 如何获取 CKEDITOR 文本区域的滚动/鼠标/光标的 X Y 位置