JQuery bind mouse wheel

clickdata-bindingjquery

I need to pass a variable inside a "bind mouse wheel" function and trigger that function when a user clicks on a link.

I just don't know how to do this as I can't put a bind inside an event handler. It just doesn't work.

This is how I tried:

$('.link').click(function(e)
{
   e.preventDefault();

   var nbr=$(this).data('article_nbr');

   $("#overlay").bind("mousewheel", function (event,delta,nbr) 
   {
        ...
        scrollTop = $("#article"+nbr).scrollTop();
        scrollTopNew = scrollTop - (delta * 30);
        ...
   }
});

I can imagine this is totally wrong but I just can't figure out the proper way to get what I need.

So far I only am able to use bind when the document is ready, but here it should be fired according to some events or event results .

Best Answer

You need to define delta and nbr outside as globals, and then you can use it inside.

var delta = 10;
var nbr = 'some_value';

$('.link').click(function(e)
{
   e.preventDefault();

   var nbr=$(this).data('article_nbr');

   $("#overlay").bind("mousewheel", function (event,delta,nbr) 
   {
        ...
        scrollTop = $("#article"+article_nbr).scrollTop();
        scrollTopNew = scrollTop - (delta * 30);
        ...
   }
});