views:

307

answers:

2

Hello, I'm relatively new to jQuery, so I apologize if I'm asking a rudimentary question, but I have a problem with event triggering. Right now I'm trying to design a kind of Image Panning tool, where the user can pan an image in any direction within a viewing window (a fixed div). On mouse down, the function will start to perform a couple calculations, and on mouse move, perform some more calculations before passing values into the

$(this).css('background-position', both);

line, where both is the newly calculated position.

It works up until this point, but I am having problems releasing from the mousemove event. After the initial on mouse down and on mouse move, I can't seem to release the panning with an on mouse up, and the user can pan the image without clicking.

Here is the basic skeleton (#test refers to the viewing div):

$("#test").mousedown( function(e) {

/* Various Calculations*/

$(this).mousemove( function(f) {

/* More Calculations, variable 'both' is assigned new coord value */

$(this).css('background-position', both);
    });

});

I've tried attaching a mouseup event in various places, but I'm not sure exactly where the right place is, and how to release. Any help is greatly appreciated!

+1  A: 

to remove the event you need to unbind it from the object:

$("#test").unbind("mousemove");

the rest of your question is not clear to me

mkoryak
Sorry if it sounds confusing. What I'm trying to do is basically create an image pan tool. I have a div about 200px x 200px in size, with it's background image set as the image I want to pan. When the user holds down a click and moves, the image is supposed to move as well. When the user let's go (on mouse up), the image should release and stay still (similar to panning in Google Books or Adobe Acrobat)Thanks for the advice, it did just the trick!
+1  A: 

As an alternative, there is a draggable in jQuery UI that may suit your needs. It includes events for start, stop, and drag.

It all depends on whether you want another dependency.

At the very least, you can examine how they perform their event sequence to simulate the same behavior.

Marc
Interesting, I'll take a look. Thanks for the advice!Do you know if ui.draggable.js is a popular library? I've only seen one or two pages that use this kind of functionality. It feels like such a cool library would only be avoided due to some kind of bug or problem =\
The library has more than just draggable, but you can customize the included plugins to reduce the footprint during download. As for bugs/problems, it has nearly the same attention (I realize I'm painting with a broad brush in this statement) as jQuery core, imo, which you seem to accept as acceptable.
Marc
As a followup, you may want to look at the following, save you the effort of starting from scratch:http://plugins.jquery.com/search/node/pan+and+zoom
Marc