简体   繁体   中英

Jquery - $(selector).scroll is passing parameters to its callback function?

i was reading those few lines of code from a javascript(Jquery) file and I was wondering where those arguments "x" and "y" are coming from.Is the scroll event that is taking care peraphs?

$(window).scroll(function(x,y) {

        dosomething(withThis);
    });

thanks Luca

PS here is the jquery excerpt that made me ask this question Is this an elegant way to make elements fade in while scrolling?

Well, open Firebug console (or web dev. tools) and paste there this code:

 $(window).scroll(function(x,y,z) {
    console.log(x,y,z)
 });

After execution and scrolling you'll see the result (works on sites with jQuery).

As expected, first argument - event object, 2nd and 3rd - undefined;

But, you can trigger events manually, and pass any arguments.

More info here: http://api.jquery.com/trigger/

No, the handler for the scroll event takes only an event object, not x,y . That code is misleading and wrong. We can't help if you don't show the full code.

This is the same thing as if you define a method

function add(a,b) {
   return a + b;
}

// When called like this, b will be undefined, as is y in your example
// programmer error not caught by the compiler
add(7);

You could inspect the arguments variable on any function, so you can get an idea of the data that's passed in.

$(window).scroll(function() {
    console.log(arguments);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM