我發現一個解決方案,檢測用戶是否使用觸摸板。它工作的很好,只有兩個小缺點。
首先它會在第一次觸發事件後檢測到mousescroll,所以只需用鼠標滾輪單擊一下即可。但這只是第一次。然後我們可以緩存導致第二個小問題的值。當用戶有一個觸控板和一個帶輪子的鼠標時,它會檢測先使用的任何東西。對我來說,這些問題可以忽略不計。這裏的代碼
var scrolling = false;
var oldTime = 0;
var newTime = 0;
var isTouchPad;
var eventCount = 0;
var eventCountStart;
var mouseHandle = function (evt) {
var isTouchPadDefined = isTouchPad || typeof isTouchPad !== "undefined";
console.log(isTouchPadDefined);
if (!isTouchPadDefined) {
if (eventCount === 0) {
eventCountStart = new Date().getTime();
}
eventCount++;
if (new Date().getTime() - eventCountStart > 50) {
if (eventCount > 5) {
isTouchPad = true;
} else {
isTouchPad = false;
}
isTouchPadDefined = true;
}
}
if (isTouchPadDefined) { // in this if-block you can do what you want
// i just wanted the direction, for swiping, so i have to prevent
// the multiple event calls to trigger multiple unwanted actions (trackpad)
if (!evt) evt = event;
var direction = (evt.detail<0 || evt.wheelDelta>0) ? 1 : -1;
if (isTouchPad) {
newTime = new Date().getTime();
if (!scrolling && newTime-oldTime > 550) {
scrolling = true;
if (direction < 0) {
// swipe down
} else {
// swipe up
}
setTimeout(function() {oldTime = new Date().getTime();scrolling = false}, 500);
}
} else {
if (direction < 0) {
// swipe down
} else {
// swipe up
}
}
}
}
document.addEventListener("mousewheel", mouseHandle, false);
document.addEventListener("DOMMouseScroll", mouseHandle, false);
https:// github。com/jnicol/trackpad-scroll-emulator,應該幫助原因':)' –
@Ethan Brown - 嗯就像我擔心的那樣 - 那麼,得找一些其他的解決方案然後 - 是啊,我會明白:) – Cody
@Tats_innit - 酷! - 但不是我在找什麼:) – Cody