473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

custom dragDrop using onmousedown, onmouseup

jon
Hello,
I'm trying to experiment with some javascript to do some custom
dragdrop handling. I wish to use the onMouseDown event to trigger the
start of my drag, and onMouseup for a potentential drop. I hope to
turn on the onmouseup only in onmousedown. And eventually go one step
further and incorporate onmousemove for a true drag workflow.

I'm running into a problem however, where after mousedown, mouseup will
not fire when I release the mouse button. It will however fire if I
click again. So the event isn't being tracked until I release the
mouse the first time apparently. Which ruins my dragDrop of course.

Below is the code. You can see a move() method that would incorporate
onmousemove, but for the most basic test I'm just using beginDrag and
endDrag.

Any ideas why onmouseup won't fire until I release the button, then
click again?

thanks


--------------

<table cellpadding="0" cellspacing="5" border="1">
<tr id="contentTr1">
<td id="image_1_td" >
<img id="image_1_img" src="cat1.jpg"
onMouseDown="beginDrag(event, this, this.id)">
</td>
<td id="image_2_td">
<img id="image_2_img" src="cat2.jpg"
onMouseDown="beginDrag(event, this, this.id)">
</td>
</tr>
</table>

--------------
var beginX = null;
var beginY = null;

function beginDrag(event, obj, id) {
beginX = event.clientX;
beginY = event.clientY;

window.status = 'beginDrag';
top.drag_id = id;

//document.onmousemove = function() { move(event, obj, id); };
document.onmouseup = function() {
window.status='onmouseup frame1';
endDrag(event);
top.frame1.document.onmousemove=null;
top.frame1.document.onmouseup=null;
top.drag_id = null;
};
}

function move(event, obj, id) {
if(beginX == null){ return; }

window.status = 'drag started: ' + event.clientX + ', ' +
event.clientY + ', onmouseup set and waiting...';
top.drag_id = id;

document.onmouseup = function() {
window.status='onmouseup frame1';
top.frame1.document.onmousemove=null;
top.frame1.document.onmouseup=null;
top.drag_id = null;
};
}

function endDrag(event) {
if(top.drag_id != null){
window.status = 'drop handled: ' + top.drag_id;
return true;
} else {
return false;
}
}

Dec 1 '06 #1
9 7562
jon wrote:
Hello,
I'm trying to experiment with some javascript to do some custom
dragdrop handling.
Don't reinvent the wheel. Use YAHOO's library: http://developer.yahoo.com/yui/
Dec 2 '06 #2
jon
I'm aware of the libs and have used yahoo's library on other sites. It
works well.

I'm trying to implement a multi-frame solution now, and yahoo's lib
doesn't easily convert to that. Before I undertake attempting to
convert it, I'd like to understand the basics a bit more.

My original question still stands.
On Dec 2, 7:08 am, TheBagbournes <n...@noway.comwrote:
jon wrote:
Hello,
I'm trying to experiment with some javascript to do some custom
dragdrop handling.Don't reinvent the wheel. Use YAHOO's library:http://developer.yahoo.com/yui/
Dec 2 '06 #3
jon
I investigated the original problem some more and have found that for
some reason dragging an image within a page fires only a mousedown
event. After the drag is moving, if the small "line through a circle"
cursor appears then there is no mousemove or mouseup event fired
subsequently. Any ideas how one might avoid that?

On Dec 2, 10:06 am, "jon" <jon.saj...@gmail.comwrote:
I'm aware of the libs and have used yahoo's library on other sites. It
works well.

I'm trying to implement a multi-frame solution now, and yahoo's lib
doesn't easily convert to that. Before I undertake attempting to
convert it, I'd like to understand the basics a bit more.

My original question still stands.

On Dec 2, 7:08 am, TheBagbournes <n...@noway.comwrote:
jon wrote:
Hello,
I'm trying to experiment with some javascript to do some custom
dragdrop handling.Don't reinvent the wheel. Use YAHOO's library:http://developer.yahoo.com/yui/- Hide quoted text -- Show quoted text -
Dec 2 '06 #4
jon wrote :
I investigated the original problem some more and have found that for
some reason dragging an image within a page fires only a mousedown
event. After the drag is moving, if the small "line through a circle"
cursor appears then there is no mousemove or mouseup event fired
subsequently. Any ideas how one might avoid that?
Why don't you try to put the image in a <div>, and make that <divdraggable,
instead of the image? :)

--
Naixn
http://fma-fr.net
Dec 2 '06 #5
jon wrote:
I'm running into a problem however, where after mousedown, mouseup will
not fire when I release the mouse button. It will however fire if I
click again. So the event isn't being tracked until I release the
mouse the first time apparently. Which ruins my dragDrop of course.
Your event handlers are not returning "false". When you set up a
handler the browser will call your code and if control is passed back to
the browser without the result being false the browser will continue its
default behavior. So whenever you actually DO something inside your
event handlers you MUST return false or the browser will continue to do
its own thing, at the expense of your sanity ;)
function beginDrag(event, obj, id) {
beginX = event.clientX;
beginY = event.clientY;

window.status = 'beginDrag';
top.drag_id = id;

//document.onmousemove = function() { move(event, obj, id); };
document.onmouseup = function() {
window.status='onmouseup frame1';
endDrag(event);
top.frame1.document.onmousemove=null;
top.frame1.document.onmouseup=null;
top.drag_id = null;
};
}
function beginDrag(event, obj, id) {
beginX = event.clientX;
beginY = event.clientY;

window.status = 'beginDrag';
top.drag_id = id;

//document.onmousemove = function() { move(event, obj, id); };
document.onmouseup = function() {
window.status='onmouseup frame1';
endDrag(event);
top.frame1.document.onmousemove=null;
top.frame1.document.onmouseup=null;
top.drag_id = null;
};

return false;
}
Hope that helps you out a bit.

---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Dec 2 '06 #6
jon
Thanks for the comments.
The returning false I think was a good around bet.

Some things I learned that helped along with the comments:
1) Dragging an image only fires an onmousedown event, not an
onmousemove or onmouseup after.
So I put the image as a background-image on a table cell, and drag
the table cell. This way we aren't dragging an image directly.
2) Dragging a table cell with a background image _sometimes_ still
thinks it's dragging an image, I think only on the 2nd+ times if it has
focus on the cell.
Putting an empty div, full height/width in the cell seems to mask
and prevent this.
3) Putting an empty div in the cell causes the table to highlight on
any drag and thus hides all the images with highlighting of empty
space.
Disabling of all text selection stops this nicely.
4) Sometimes onmouseup won't fire in an alternate frame than where the
onmousedown was set, and onmousemove event doesn't know it crossed into
the new frame until mouse is released.
Simple solution is to track global mouse coordinates and know that
if coordinates reach certain part of the window (ie, bottom 90% for
me), then it is in the secondary frame, despite the event's confusion.

On Dec 2, 10:39 am, pcx99 <x...@x.comwrote:
jon wrote:
I'm running into a problem however, where after mousedown, mouseup will
not fire when I release the mouse button. It will however fire if I
click again. So the event isn't being tracked until I release the
mouse the first time apparently. Which ruins my dragDrop of course.Your event handlers are not returning "false". When you set up a
handler the browser will call your code and if control is passed back to
the browser without the result being false the browser will continue its
default behavior. So whenever you actually DO something inside your
event handlers you MUST return false or the browser will continue to do
its own thing, at the expense of your sanity ;)
function beginDrag(event, obj, id) {
beginX = event.clientX;
beginY = event.clientY;
>
window.status = 'beginDrag';
top.drag_id = id;
>
//document.onmousemove = function() { move(event, obj, id); };
document.onmouseup = function() {
window.status='onmouseup frame1';
endDrag(event);
top.frame1.document.onmousemove=null;
top.frame1.document.onmouseup=null;
top.drag_id = null;
};
}

function beginDrag(event, obj, id) {
beginX = event.clientX;
beginY = event.clientY;

window.status = 'beginDrag';
top.drag_id = id;

//document.onmousemove = function() { move(event, obj, id); };
document.onmouseup = function() {
window.status='onmouseup frame1';
endDrag(event);
top.frame1.document.onmousemove=null;
top.frame1.document.onmouseup=null;
top.drag_id = null;
};

return false;

}Hope that helps you out a bit.

---------------------------------------------------------------------------http://www.hunlock.com-- Permanently under construction (And proud of it!)
$FA
Dec 2 '06 #7
jon wrote:
Thanks for the comments.
The returning false I think was a good around bet.

Some things I learned that helped along with the comments:
1) Dragging an image only fires an onmousedown event, not an
onmousemove or onmouseup after.
So I put the image as a background-image on a table cell, and drag
the table cell. This way we aren't dragging an image directly.
2) Dragging a table cell with a background image _sometimes_ still
thinks it's dragging an image, I think only on the 2nd+ times if it has
focus on the cell.
Putting an empty div, full height/width in the cell seems to mask
and prevent this.
3) Putting an empty div in the cell causes the table to highlight on
any drag and thus hides all the images with highlighting of empty
space.
Disabling of all text selection stops this nicely.
4) Sometimes onmouseup won't fire in an alternate frame than where the
onmousedown was set, and onmousemove event doesn't know it crossed into
the new frame until mouse is released.
Simple solution is to track global mouse coordinates and know that
if coordinates reach certain part of the window (ie, bottom 90% for
me), then it is in the secondary frame, despite the event's confusion.
You're hitting all the problems with #1-#4 because your event handlers
are not doing a "return false" before they end. That means the browser
continues with it's default behavior which interferes with what you want
to do with your code (forcing you to "hide" the image behind table-cells
for instance).

If you leave the browser completely alone (example: web page has a
single image, no javascript, no nothing) and you click on an image and
drag it the mouse will change into a no sign (circle with a slash
through it), if you keep dragging outside your browser the mouse will
change indicating you can drop the image on your desktop (or into an
application which can process images). This is the default behavior.

When you don't "return false" in your handlers this is what the browser
is trying to do. When your handlers start returning false the browser
will stop behaving this way and stop interfering with your code :)

---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Dec 2 '06 #8
jon
I wish this were the case, but in practice "return false" did not solve
most of these problems...

On Dec 2, 6:11 pm, pcx99 <x...@x.comwrote:
jon wrote:
Thanks for the comments.
The returning false I think was a good around bet.
Some things I learned that helped along with the comments:
1) Dragging an image only fires an onmousedown event, not an
onmousemove or onmouseup after.
So I put the image as a background-image on a table cell, and drag
the table cell. This way we aren't dragging an image directly.
2) Dragging a table cell with a background image _sometimes_ still
thinks it's dragging an image, I think only on the 2nd+ times if it has
focus on the cell.
Putting an empty div, full height/width in the cell seems to mask
and prevent this.
3) Putting an empty div in the cell causes the table to highlight on
any drag and thus hides all the images with highlighting of empty
space.
Disabling of all text selection stops this nicely.
4) Sometimes onmouseup won't fire in an alternate frame than where the
onmousedown was set, and onmousemove event doesn't know it crossed into
the new frame until mouse is released.
Simple solution is to track global mouse coordinates and know that
if coordinates reach certain part of the window (ie, bottom 90% for
me), then it is in the secondary frame, despite the event's confusion.You're hitting all the problems with #1-#4 because your event handlers
are not doing a "return false" before they end. That means the browser
continues with it's default behavior which interferes with what you want
to do with your code (forcing you to "hide" the image behind table-cells
for instance).

If you leave the browser completely alone (example: web page has a
single image, no javascript, no nothing) and you click on an image and
drag it the mouse will change into a no sign (circle with a slash
through it), if you keep dragging outside your browser the mouse will
change indicating you can drop the image on your desktop (or into an
application which can process images). This is the default behavior.

When you don't "return false" in your handlers this is what the browser
is trying to do. When your handlers start returning false the browser
will stop behaving this way and stop interfering with your code :)

---------------------------------------------------------------------------http://www.hunlock.com-- Permanently under construction (And proud of it!)
$FA- Hide quoted text -- Show quoted text -
Dec 3 '06 #9
jon wrote:
I wish this were the case, but in practice "return false" did not solve
most of these problems...
With the exception of #4, #1-#3 are all workarounds for not returning
false in your event handlers. (I haven't tested #4 so it may be fixed
as well), but as for moving images...

Here's a working example of a raw naked image being that can be dragged
and dropped on demand from the user.

http://www.hunlock.com/examples/dragdroppic.html
---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Dec 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: coolsti | last post by:
I am trying to let the user drag a rectangle over an image, which will then let the user zoom in on the dragged area. However, I am doing something wrong in implementing the onmousedown and...
1
by: sameth | last post by:
I wrote a script that uses onmousedown and onmouseup in javascript that works great in ie and firefox on windows2000. It looks as if the pocketpc 2003 ie browser dosn't support onmousedown or...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
3
by: weston | last post by:
I'm making a foray into trying to create custom vertical scrollbars and sliders, and thought I had a basic idea how to do it, but seem to be having some trouble with the implementation. My...
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
6
by: RC | last post by:
<a href="#" onmousedown="callMyFunction()">Press the mouse button</a> What I expect is when I or an user move mouse pointer over that link, and press the mouse button. Then will...
1
by: NeilCarmichael | last post by:
I am flummoxed, I'm relatively new to javascript and I can't get my onmousedown onmouseup script to work. simply, I want to fire one function when I click down on a graphic and a second when I...
2
by: Jay Dee | last post by:
I have created a container that will position 4 panels that has 15 different layouts to choose from. I have based it similar to a System.Windows.Forms.SplitContainer but that has 4 panels...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.