473,396 Members | 1,940 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,396 software developers and data experts.

Ending drag&drop operation on mouse out of browser?

I have a bit of JavaScript code (see below) that handles drag-and-drop
of elements on a webpage, simply moving them around the page.

The problem I'm having occurs when the user drags the object outside
the browser window and releases. The JavaScript onmouseup event fails
to fire (as is consistent with the language spec), and the element is
left in a "dragging limbo" - it follows the mouse cursor once it
returns inside the window (despite the button being up).

Can anyone think of any good ways of getting around this problem?
Ideally I'd want to fire the event attached to onmouseup when the
cursor left the browser window. Either that, or a more creative
solution if anyone can think of one

Your help would be much appreciated

Chris Beach

The following code is abridged:

var objPhoto;
var objDragged = null;
var mousePos = new Coords( 0, 0 );

function Photo( obj )
{
this.obj = obj;
var objOffset = obj;
this.coords = new Coords( 0, 0 );
while ( objOffset.offsetParent )
{
this.coords.y += objOffset.offsetTop;
this.coords.x += objOffset.offsetLeft;
objOffset = objOffset.offsetParent;
}
this.height = obj.height;
this.width = obj.width;
}

function Coords( x, y )
{
this.x = x;
this.y = y;
this.toString = function(){ return "x=" + this.x + ", y=" + this.y; }
}

function Arrow( user, coords )
{
var objArrowContainer = document.createElement( 'table' );
objArrowContainer.onmousedown = function(){ arrowMouseDown(
objArrowContainer );return false; }
document.body.appendChild( objArrowContainer );
}

function init()
{
objPhoto = new Photo( document.getElementById( "photo" ) );
var position = new Coords(
objPhoto.coords.x + objPhoto.width / 2,
objPhoto.coords.y + objPhoto.height / 2 );
var newArrow = new Arrow( new User( "Chris", "Chris", "cbicon.jpg" ),
position );
document.onmousemove = docMouseMove;
document.onmouseup = docMouseUp;
//document.onmouseover = function(){return false;}();
}

function arrowMouseDown( sender )
{
objDragged = sender;
return false;
}

function docMouseUp( e )
{
if ( objDragged == null ) return false;
docMouseMove( e );
var personCoords = new Coords(
parseInt( objDragged.style.left ) - ARROW_OFFSET.x -
objPhoto.coords.x,
parseInt( objDragged.style.top ) - ARROW_OFFSET.y - objPhoto.coords.y
);
objDragged = null;
objCoords = document.getElementById( "coords" );
objCoords.innerHTML = personCoords;
return false;
}

function docMouseMove( e )
{
if ( document.all )
{
mousePos.x = window.event.x;
mousePos.y = window.event.y;
if ( document.body && ( document.body.scrollLeft ||
document.body.scrollTop ) )
{
mousePos.x += document.body.scrollLeft;
mousePos.y += document.body.scrollTop;
}
else if ( document.documentElement &&
( document.documentElement.scrollLeft ||
document.documentElement.scrollTop ) )
{
mousePos.x += document.documentElement.scrollLeft;
mousePos.y += document.documentElement.scrollTop;
}
}
else if ( document.getElementById || document.layers )
{
mousePos.x = e.pageX;
mousePos.y = e.pageY;
}
if ( objDragged != null )
{
x = mousePos.x + ARROW_OFFSET.x;
y = mousePos.y + ARROW_OFFSET.y;

if ( x < objPhoto.coords.x + ARROW_OFFSET.x )
x = objPhoto.coords.x + ARROW_OFFSET.x;
else if ( x >= objPhoto.width + objPhoto.coords.x + ARROW_OFFSET.x )
x = objPhoto.width + objPhoto.coords.x + ARROW_OFFSET.x;

if ( y < objPhoto.coords.y + ARROW_OFFSET.y )
y = objPhoto.coords.y + ARROW_OFFSET.y;
else if ( y >= objPhoto.coords.y + objPhoto.height + ARROW_OFFSET.y )

y = objPhoto.height + objPhoto.coords.y + ARROW_OFFSET.y;

objDragged.style.left = x + 'px';
objDragged.style.top = y + 'px';
}
return false;
}

Oct 28 '05 #1
5 2868
Perhaps you could put a margin around the edge of the page with a
handler clearing the drag...? That's an old Flash trick. With Flash, if
you're fast enough, you can get past a reasonably sized margin without
the event firing, but JS is generally a bit faster.

In article <11*********************@f14g2000cwb.googlegroups. com>,
ch***@chrisbeach.co.uk wrote:
I have a bit of JavaScript code (see below) that handles drag-and-drop
of elements on a webpage, simply moving them around the page.

The problem I'm having occurs when the user drags the object outside
the browser window and releases. The JavaScript onmouseup event fails
to fire (as is consistent with the language spec), and the element is
left in a "dragging limbo" - it follows the mouse cursor once it
returns inside the window (despite the button being up).

Can anyone think of any good ways of getting around this problem?
Ideally I'd want to fire the event attached to onmouseup when the
cursor left the browser window. Either that, or a more creative
solution if anyone can think of one

Your help would be much appreciated

Chris Beach

The following code is abridged:

var objPhoto;
var objDragged = null;
var mousePos = new Coords( 0, 0 );

function Photo( obj )
{
this.obj = obj;
var objOffset = obj;
this.coords = new Coords( 0, 0 );
while ( objOffset.offsetParent )
{
this.coords.y += objOffset.offsetTop;
this.coords.x += objOffset.offsetLeft;
objOffset = objOffset.offsetParent;
}
this.height = obj.height;
this.width = obj.width;
}

function Coords( x, y )
{
this.x = x;
this.y = y;
this.toString = function(){ return "x=" + this.x + ", y=" + this.y; }
}

function Arrow( user, coords )
{
var objArrowContainer = document.createElement( 'table' );
objArrowContainer.onmousedown = function(){ arrowMouseDown(
objArrowContainer );return false; }
document.body.appendChild( objArrowContainer );
}

function init()
{
objPhoto = new Photo( document.getElementById( "photo" ) );
var position = new Coords(
objPhoto.coords.x + objPhoto.width / 2,
objPhoto.coords.y + objPhoto.height / 2 );
var newArrow = new Arrow( new User( "Chris", "Chris", "cbicon.jpg" ),
position );
document.onmousemove = docMouseMove;
document.onmouseup = docMouseUp;
//document.onmouseover = function(){return false;}();
}

function arrowMouseDown( sender )
{
objDragged = sender;
return false;
}

function docMouseUp( e )
{
if ( objDragged == null ) return false;
docMouseMove( e );
var personCoords = new Coords(
parseInt( objDragged.style.left ) - ARROW_OFFSET.x -
objPhoto.coords.x,
parseInt( objDragged.style.top ) - ARROW_OFFSET.y - objPhoto.coords.y
);
objDragged = null;
objCoords = document.getElementById( "coords" );
objCoords.innerHTML = personCoords;
return false;
}

function docMouseMove( e )
{
if ( document.all )
{
mousePos.x = window.event.x;
mousePos.y = window.event.y;
if ( document.body && ( document.body.scrollLeft ||
document.body.scrollTop ) )
{
mousePos.x += document.body.scrollLeft;
mousePos.y += document.body.scrollTop;
}
else if ( document.documentElement &&
( document.documentElement.scrollLeft ||
document.documentElement.scrollTop ) )
{
mousePos.x += document.documentElement.scrollLeft;
mousePos.y += document.documentElement.scrollTop;
}
}
else if ( document.getElementById || document.layers )
{
mousePos.x = e.pageX;
mousePos.y = e.pageY;
}
if ( objDragged != null )
{
x = mousePos.x + ARROW_OFFSET.x;
y = mousePos.y + ARROW_OFFSET.y;

if ( x < objPhoto.coords.x + ARROW_OFFSET.x )
x = objPhoto.coords.x + ARROW_OFFSET.x;
else if ( x >= objPhoto.width + objPhoto.coords.x + ARROW_OFFSET.x )
x = objPhoto.width + objPhoto.coords.x + ARROW_OFFSET.x;

if ( y < objPhoto.coords.y + ARROW_OFFSET.y )
y = objPhoto.coords.y + ARROW_OFFSET.y;
else if ( y >= objPhoto.coords.y + objPhoto.height + ARROW_OFFSET.y )

y = objPhoto.height + objPhoto.coords.y + ARROW_OFFSET.y;

objDragged.style.left = x + 'px';
objDragged.style.top = y + 'px';
}
return false;
}

Oct 28 '05 #2
Thanks for your suggestion. How would you design such a margin (in
terms of HTML) such that it wouldn't affect the current look of the
page?

Oct 28 '05 #3
In article <11*********************@f14g2000cwb.googlegroups. com>,
ch***@chrisbeach.co.uk wrote:
Thanks for your suggestion. How would you design such a margin (in
terms of HTML) such that it wouldn't affect the current look of the
page?


That doesn't affect the look of the page? Geez, I have no clue. I start
thinking about it and it gets very kludgy in my head very fast -- stuff
like a div with no background, but you'd need something to hold it open,
like a transparent gif. And then you'd need to calculate the height of
the page so you'd know how big the gif should be... etc.

Maybe I'll just stand aside and wait for someone to offer a better
solution.
Oct 28 '05 #4
put something similar to the following code in the IE branch of your
mouseMove handler:

if(window.event.button != 1)
docMouseUp(evt);

Sorry, but I have no solution for the other browsers.

Please, note also that my drag & drop code attaches/detaches the mouseMove
handler on mouseDown/mouseUp. I guess you have to check the existance of
objDragged as condition expression in the above code fragment.
Oct 28 '05 #5
Thanks for your suggestion. Cross-browser support is quite important to
me, especially as I developing on a Mac.

I have solved the problem temporarily by setting a timeout on the drop
operation

Chris

Oct 31 '05 #6

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

Similar topics

2
by: Ivo Tcholakov | last post by:
Is it possible to drag and drop controls in an aspx page at runtime ? Meaning i have developed a ASP.NET web form, the web form is now downloaded in IE - now can i have this form to detect mouse...
3
by: Goldwind | last post by:
Hi, I"m trying to use drag & drop of text from one text box to another but without suceess. Microsoft presented an example in "101 code samples" BUT in this example the code select and drag all...
2
by: Philippe Meunier | last post by:
Hi, I would like to code kind of Form designer. I would like it to act like the VB.NET designer and the problem I am actually facing is about the drag & drop of controls on the form. I add them...
1
by: Terry Olsen | last post by:
My first time using TreeViews. I have TreeView1 set up to display my directory structure just like Windows Explorer. I can drag & drop files and folders over to TreeView2. I can re-arrange the...
3
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
0
by: haegens | last post by:
I am making a .NET Application which has a TreeView Control in it. I have 3 levels of nodes. The toplevel is a rootnode which contains all other nodes. The second level holds one kind of nodes that...
1
by: Sim | last post by:
Hello NG, I try to use drag and drop function between two list views. For this I found following code: ...
2
by: sebastian.janoschka | last post by:
Hi, I build my first Drag & Drop with JavaScript and I would like to drag the pictures when I click on it. When I create a normal div tag with some text the script works, but when I put a...
2
by: deccio | last post by:
I have create an activex Control with Visual studio 2005 and framework 2.0 in c# to add drag & drop functionality to upload multi file. When I use it in a windows form it work fine. Infact if I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.