Dev : Extensions : Example Code : Using the scrollwheel: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
No edit summary
 
(moved to devmo)
 
Line 1: Line 1:
==Using the ScrollWheel==
See [http://developer.mozilla.org/en/docs/Code_snippets:Miscellaneous#Detecting_mouse_wheel_events Code snippets: Miscellaneous - Detecting mouse wheel events] at [http://developer.mozilla.org MDC].
This code will show you how to use the scrollwheel and listen for scrolling. This will only work on mozilla browsers.


===DOMMouseScroll===
[[Category:Redirects]]
DOMMouseScroll can be used with event listeners just like all other events. It will pass an object with a variable called detail that specifies the number of lines to scroll and in what direction.
 
<pre>
var elm = document.getElementById("...");
elm.addEventListener("DOMMouseScroll",scroll,true);
function scroll(target){
  if(target.detail>0){
    alert("up "+tanget.detail);
  }else{
    alert("down "+tanget.detail);
  }
}
</pre>
===MiddleClick===
You can also listen for clicks on the middle mouse button. This is done like a normal button, but in the function called you check what button has been pressed. The middle mouse button is number 1 (the left is 0 and the right is 2).
<pre>
var elm = document.getElementById("...");
elm.addEventListener("click",click,true);
function click(event){
  if(event.button==1){
    alert("Click");
  }
}
</pre>
==Appendix==
 
[[Category:Example code]]

Latest revision as of 17:36, 24 May 2007