Mouse Location (x,y) Tracking with Left Button Down

G

gsb

I track the mouse location like this code:
function mousePos(e) {
var p = new Object();
if(e) { p.x = e.pageX; p.y = e.pageY; }
else { p.x = event.x; p.y = event.y; }
... (show)
}
document.onmousemove=mousePos;

Seems to do fine on many browsers.
However, this does not track the mouse position when the left mouse button
is held down.

What changes will I need to track the mouse position when the left mouse
button is depressed and held down while moving the mouse?

Your help is appreciated.

Thanks,

gsb
 
G

gsb

Thanks for the quick response Andy.
And if I am not mistaken, I have learned from some of your earlier works in
the DHTML area on the web. Thanks.

I agree and these codes are activated via a mouse down on an element.
They are to be removed upon the mouse release.
So tracking is only during the "dragging" state.

And that is why my question for my attempt at tracking, currently for
testing, worked with mouse over/out but not mouse down/up.

And as you guessed, it is a dragging scheme I have in mind but was trying to
distill the question down for the post.

IE is nice but I am also trying to be as "cross-browser" compliant as
possible.

Again thanks and any further suggestions will be appreciated.

gsb
 
R

Richard Cornford

Andrew Scott wrote:
//Set Focus
document.body.onmousemove = function (e)
{
if( window.attachEvent ){ e = event; } // For IE only
<snip>

Why this stupidly indirect test? IE is not the only browser that
implements window.attachEvent, and browsers following IE's event model
may not necessarily implement it (and IE 4 doesn't).

There is an obvious subject available for a test that will tell you
exactly what you want to know, reliably and directly, and that subject
is the functions - e - parameter. If it is a reference to an object then
the event object has been passed to the function as an argument, and if
it is not a reference to an object then the event object must be sought
elsewhere. So:-

if(!e){
e = window.event;
}
if(e){
... // We have our event object.
}
- or simply:-

e = e || window.event;
if(e){
... // We have our event object.
}

- or (potentially more obscurely):-

if((e = e || window.event)){
... // We have our event object.
}
- and no dubious inferences are necessarily in determining what to do.

All feature testing should be done as close as possible to the feature
that is the subject of the test, preferably with a direct one-to-one
relationship to that feature. When the feature is that a function may or
may not be passed a particular parameter the right test is applied
directly to the nature of that parameter.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top