標籤:js js滑鼠滾輪事件 滑鼠滾輪上下滾動事件
最近做項目涉及到多個DIV轉場效果,包括普通按鈕點擊切換和滑鼠滾輪上下滾動切換等;其主要涉及到的內容就是滑鼠滾輪上下切換的事件監聽,此處是第一次接觸到,故在此記錄一下;也希望能幫到各位有需要的朋友。
以下為項目執行個體:
$(function() {
var count = $("#sysCount").val();
var isUp = false;
// 初始化子系統模組
if (count > 6) {
$("#panel").css("width", "1200px");
$(".line2").css("width", "550px");
$(".hoverBtn").css("display", "block");
for ( var i = 6; i < count; i++) {
$("#sys" + i).css("display", "none");
}
$("#lastBtn").hover(function() {
if (start != 0) {
$(this).toggleClass("hoverBtnTog");
}
});
$("#nextBtn").hover(function() {
if (count - start != 6) {
$(this).toggleClass("hoverBtnTog");
}
});
//滑鼠滾輪事件
var isFirefox = navigator.userAgent.indexOf("Firefox") != -1;
//Firefox事件:DOMMouseScroll、IE/Opera/Chrome事件:mousewheel
var mousewheel = isFirefox ? "DOMMouseScroll" : "mousewheel";
//滑鼠滾動事件
var scrollFunc = function(e) {
e = e || window.event;
//當滑鼠在子系統地區內時:屏蔽視窗滾輪-滾動捲軸事件;
//此時才調用子系統滾動方法
if(isUp){
e = e || window.event;
if (e.stopPropagation) e.stopPropagation();
else e.cancelBubble = true;
if (e.preventDefault) e.preventDefault();
else e.returnValue = false;
if(isFirefox){
if (e.detail == -3) {
// 向上滾動
getLast();
} else {
// 向下滾動
getNext();
}
}else{
if (e.wheelDelta == 120) {
// 向上滾動
getLast();
} else {
// 向上滾動
getNext();
}
}
}
};
document.addEventListener(mousewheel, scrollFunc, false);
$("#hor").mouseover(function(){
isUp = true;
});
$("#hor").mouseleave(function(){
isUp = false;
});
}
});
js滑鼠滾輪上下滾動監聽事件應用執行個體(跨瀏覽器,親測)