3 Replies - 1194 Views - Last Post: 10 May 2012 - 09:22 AM

#1 fallenreaper   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 240
  • Joined: 19-June 10

How does mousemove work for a selection?

Posted 10 May 2012 - 07:49 AM

I was looking at the jQuery website, but when i was implementing it, it was not working as expected. Maybe I defined it differently?

it says:
$(selector).mousemove(function(e){
   //what happens while the mouse moves.
});



but i was trying to do something ALMOST The same, but doesnt do what i was expecting.

$(selector).click(function(){ method1(key, e); });
function method1(key, e){
  //some stuff
  $(document).mousemove( method2(key, e) );
}
function method2(key, e){
  //some stuff
  $(item).offset({top:e.pageY,left:e.pageX});
}



The above code runs only once, where i was thinking it would run indefinately until it is unbound.

Is This A Good Question/Topic? 0
  • +

Replies To: How does mousemove work for a selection?

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: How does mousemove work for a selection?

Posted 10 May 2012 - 08:10 AM

Hey.

That code should not work at all, seeing as you neither define the e nor the key variable for your initial .click callback. Where are those values coming from?

However, the main problem there seems to be the way you are passing the callback function for the .mousemove function.
$(document).mousemove( method2(key, e) );


You see, you aren't actually telling it to use the method2 function and pass along the two parameters, you are telling it to execute the function with the two parameters and use the return value of that as the callback function. That's why it's only executing once, because you are in fact only telling it to execute it once. - This should be wrapped into a function, like you do with the .click handler.
$(document).mousemove(function(mouseEvent) {
    method2(key, mouseEvent);
});


Also note the mouseEvent variable I use there. Both the .click and .mousemove callback functions take an event parameter, and it is popular to use e as the name of that parameter. However you need to be sure of which event parameter you are using when you nest callbacks like that, because if you use the one from the .click event, you'll only get the mouse coordinates from the original .click callback. You need to make sure you are using the one coming from the .mousemove callback.
Was This Post Helpful? 1
  • +
  • -

#3 fallenreaper   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 240
  • Joined: 19-June 10

Re: How does mousemove work for a selection?

Posted 10 May 2012 - 09:09 AM

So, if i define Key as a global variable and then pass it in, it should work like this?

$(document)mousemove(function(mouseEvent){
var key = "id_to_have_adjust";
method2(key,mouseEvent);
});


Was This Post Helpful? 0
  • +
  • -

#4 fallenreaper   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 240
  • Joined: 19-June 10

Re: How does mousemove work for a selection?

Posted 10 May 2012 - 09:22 AM

Sweet, it works now. Thanks. I knew i was doing something wrong, and you pointed it out like a champ.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1