滑鼠滾輪事件介紹

2022-02-11 10:21:07 字數 1199 閱讀 8831

ie6實現了滑鼠滾輪事件mousewheel,這個事件包含了乙個滑鼠事件所沒有的屬性,wheeldelta,這

個屬性值始終是120的倍數,而且向上滑動一單位,wheeldelta值為120,向下為-120.隨後chrome,opera,

safari等也都實現了該事件。opera10之前的版本對wheeldelta值的符號處理錯誤,需要修改。但是firefox沒

有該事件,而是用dommousescroll。這個事件只有firefox支援,所以可以單獨針對firefox做相容。

另外,html5為了相容這兩個事件,另外規定了乙個標準版本wheel事件,比較新的瀏覽器基本實現了該事件。

chrome31+,firefox 17+,ie9+,safari 7.0.5以及新版使用webkit核心的opera,使用carakan核心的opera

並不支援該事件。wheel事件有deltax、deltay和deltaz屬性,預設為100.

綜上,目前wheel事件的相容性其實並不理想,mousewheel的相容性最佳,適用於除firefox外的所有瀏覽器,對於

firefox則單獨做相容處理。

1 addevent =function(type,callback,usecapture)else

if(window.attachevent)

7return

callback;8}

9 wheel =function(el,callback)catch

(e){}

15 el.addevent =addevent;

16el.addevent(type,function(e)

24 e.delta = parseint(wheeldelta) / 120

;25 }else

if('

detail'in

e)29

callback.call(el,e);

30//

阻止向上冒泡

31 e.stoppropagation &&e.stoppropagation();

32if("

stopbubble

"in e) e.stopbubble = true;33

return

false

;34 },false

);35 };

滑鼠滾輪事件

新增事件 有相容性 註冊事件 if document.addeventlistener window.onmousewheel document.onmousewheel scrollfunc ie opera chrome detail與wheeldelta 判斷滾輪向上或向下在瀏覽器中也有相容性...

滑鼠滾輪事件

新增事件 有相容性 註冊事件 if document.addeventlistener window.onmousewheel document.onmousewheel scrollfunc ie opera chrome detail與wheeldelta 判斷滾輪向上或向下在瀏覽器中也有相容性...

滑鼠滾輪事件

滑鼠滾輪事件和鍵盤事件以及滑鼠左右鍵沒本質的區別。2 if document.addeventlistener window.onmousewheel document.onmousewheel scroll ie opera chrome safari 像上面這樣就可以給整個文件繫結滾輪事件。fu...