Monday, September 18, 2006

portable and rewrote onmousewheel function

I did a simple version of onmousewheel, then I add JSL and $DOM objects dependencies, then I've came back to single portable version without dependencies that's simpler than first one :D

Concept:
onmousewheel isn't a window or document event, onmousewheel is a generic Element event. While a generic event as onclick, onmouseover, onmouseout is called only above the element, onmousewheel will be called only above the element too, that's all!

No more double events (onmouseover that activates onmousewheel and onmouseout that deactivates onmousewheel) ... just the event.


/**
* function onmousewheel,
* onmousewheel(element:Object [, callback:Function]):Void
* @param Object window, document or DOM.element to use with callback
* @param Function callback function with element scope (.call(...)) and delta wheel value as single parameter
* @return Void
*/
function onmousewheel(element, callback) {

// @author Andrea Giammarchi [http://www.devpro.it/]
// @license MIT [http://www.opensource.org/licenses/mit-license.php]
// @credits Adomas Paltanavicius [http://adomas.org/javascript-mouse-wheel/]

function __onwheel(event) {
var delta = 0;
if(event.wheelDelta) {
delta = event.wheelDelta / 120;
if(window.opera)
delta = -delta;
}
else if(event.detail)
delta = -event.detail / 3;
if(delta)
callback.call(element, delta);
if(event.preventDefault)
event.preventDefault();
event.returnValue = false;
return false;
};

if(element.addEventListener && !window.opera)
element.addEventListener("DOMMouseScroll", __onwheel, false);
else
element.onmousewheel = (function(base){return function(evt){
if(!evt) evt = window.event;
if(base) base.call(element, evt);
return __onwheel(evt);
}})(element.onmousewheel);
};


And here you can view the always updated version or the example page.

No comments:

Post a Comment