fraxture
fraxture

Reputation: 5530

No Event object on Scroll Event in MooTools?

I am getting an undefined value for the event object that I think should be available in the callback for the addEvent() function. This is what I'm doing.

element.addEvent('scroll', callback(event) {
  console.log(event);
});
// on scroll, event is 'undefined'

I tested the same code attaching the callback to the click event instead and in that case the event variable was defined as you'd imagine. What am I missing here? My utlimate goal is to stop the normal effect of the scroll, i.e. prevent scrolling, so that I can scroll with an ease function using Fx.Scroll...

Upvotes: 2

Views: 1207

Answers (1)

Dimitar Christoff
Dimitar Christoff

Reputation: 26165

It works if you do it right, but it does not pass the event as it's not cancellable.

callback(event) makes no sense unless it returns a function that can run at scroll time - else, it's gonna throw due to the block scope after, which will be treated like an object literal....

http://jsfiddle.net/dimitar/V8fht/

to scroll on an element:

document.getElement('div').addEvent('scroll', function(event) {
  console.log('div scroll');
});

with

div {
    height: 300px;
    overflow-y: scroll;
}

to fire on document scrolling:

document.addEvent('scroll', function(event) {
  console.log('scroll document', this); // document
});

you can try the native one:

document.addEventListener('scroll', function(event) {
    event.stopPropagation();
    event.preventDefault();
    console.log(event);
    return false;
});

... it won't stop the scrolling so the event is just a token, really.

@kentaromiura who is a core dev said:

This behaviour is known and It's documented here: http://mootools.net/docs/core/Element/Element.Event#Element-NativeEvents

The reason to differentiate between 1 and 2 is that 1 is usually used for events that don't have interesting data like: onload, onscroll, and onresize, or it's more performant. The latter two, for example, are fired frequently.

Upvotes: 2

Related Questions