473,466 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

when draging a <div>, how can I detect what I'm moving over?

I'm playing with dragable <DIV>s, being inspired by google.com/ig,
where you can move items on the page around and have items you move
over change position if necessary.

I have 3 div's setup, one for each dragable item (boxes about
100x150px), inside another div that sets up a border.

my script is instigated by an onMouseOver event and works great for
moving items, setting the opacity, etc. The only trouble is (and I
guess I should have expected this) is that when I'm dragging one div,
the onMouseOver events for the other 2 divs are not initialized. In
retrospect that's probably a good thing, or I'd end up dragging
everything at the same time.

What I'm looking for is an idea where to go next in order to find out
what I'm dragging a div over. I purposely didn't include any code as
I'm really looking for just hints rather than solutions, as this
project is very much a learning excercise for myself, and I've already
cheated enough by downloading dom-drag.js as the base code.

any ideas how I can find out what I'm dragging over? My thoughts were
listeners (though I don't know much about them but they seemed to make
sense here) or not worrying about what I'm over, but more where I am in
the list and then work out if something needs to be move or not (seems
cumbersome and potentially a resource hog while I check mouse movements
and positions while dragging)

any hints and/or tips would be appreciated.

Nov 11 '05 #1
12 2361
Kevin Blount wrote:
What I'm looking for is an idea where to go next in order to find out
what I'm dragging a div over. I purposely didn't include any code as
I'm really looking for just hints rather than solutions, as this
project is very much a learning excercise for myself, and I've already
cheated enough by downloading dom-drag.js as the base code.


I really like your attitude :) However, it does not hurt to
read source code of others, instead it helps you to understand
to write it yourself, and probably better.

Look into the `ondragover' event handler for IE:

<http://msdn.microsoft.com/workshop/author/dhtml/reference/events/ondragover.asp>

I don't know if it is supported in the Gecko DOM as well, probably
not as the reference does not mention it. However, there are the
onmouse* event handlers which could be and are probably used for this.

<http://developer.mozilla.org/en/docs/DOM:element#Event_Handlers>
HTH

PointedEars
Nov 23 '05 #2
thanks for the response.. I'll definitely check out the ondragover..
sounds ideal (though no clue exactly what it does yet <g>), and thanks
for the DOM references too.

Nov 23 '05 #3
> I really like your attitude :) However, it does not hurt to
read source code of others, instead it helps you to understand
to write it yourself, and probably better.


Its a pritty tough act to follow, very criptic, I've tried three times to
look at it so far.

Anyone got a JavaScript reformatter or pritty printer of any kind ?

I'll have another butchers at it at some point.

Aaron
Nov 23 '05 #4
Aaron Gray wrote:

Please provide proper attribution.
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
I really like your attitude :) However, it does not hurt to
read source code of others, instead it helps you to understand
to write it yourself, and probably better.


Its a pritty tough act to follow, very criptic, I've tried three times to
look at it so far.

Anyone got a JavaScript reformatter or pritty printer of any kind ?


When Pretty Printing is needed, I use the Format feature of the JavaScript
Editor PlugIn for Eclipse from <http://sourceforge.net/projects/jseditor/>.

IIRC Macromedia Dreamweaver MX also includes such a feature and probably
many other JS editors.
PointedEars
Nov 23 '05 #5
>> Anyone got a JavaScript reformatter or pritty printer of any kind ?

When Pretty Printing is needed, I use the Format feature of the JavaScript
Editor PlugIn for Eclipse from
<http://sourceforge.net/projects/jseditor/>.


Ah, Eclipse, I keep meaning to download it and try it out, never seem to get
round to it though.

Aaron
Nov 23 '05 #6
Scriptaculous does it at this page: http://script.aculo.us/demos/shop ,
yet I still can't understand how they do it.

If someone can find out, please tell...

Thanks.

Thomas 'PointedEars' Lahn wrote:
Kevin Blount wrote:
What I'm looking for is an idea where to go next in order to find out
what I'm dragging a div over. I purposely didn't include any code as
I'm really looking for just hints rather than solutions, as this
project is very much a learning excercise for myself, and I've already
cheated enough by downloading dom-drag.js as the base code.


I really like your attitude :) However, it does not hurt to
read source code of others, instead it helps you to understand
to write it yourself, and probably better.

Look into the `ondragover' event handler for IE:

<http://msdn.microsoft.com/workshop/author/dhtml/reference/events/ondragover.asp>

I don't know if it is supported in the Gecko DOM as well, probably
not as the reference does not mention it. However, there are the
onmouse* event handlers which could be and are probably used for this.

<http://developer.mozilla.org/en/docs/DOM:element#Event_Handlers>
HTH

PointedEars

Nov 23 '05 #7
> Scriptaculous does it at this page: http://script.aculo.us/demos/shop ,
yet I still can't understand how they do it.

If someone can find out, please tell...


Have a look at this :-

http://openrico.org/rico/demos.page?...nd_drop_simple

There's a bug in it though, if you scroll down the drag object appears in
the wrong place.

There's more goodies at :-

http://en.wikipedia.org/wiki/AJAX

Hope that helps.

Aaron
Nov 23 '05 #8
Very good link, this. Plenty of ideas there one can take. I'm still
trying to figure out though how it detects which dropzone you're over.
Aaron Gray wrote:
Have a look at this :-

http://openrico.org/rico/demos.page?...nd_drop_simple

There's a bug in it though, if you scroll down the drag object appears in
the wrong place.

Nov 23 '05 #9
John Talbot wrote:
Very good link, this. Plenty of ideas there one can take. I'm still
trying to figure out though how it detects which dropzone you're over.
Aaron Gray wrote:
Have a look at this :-
http://openrico.org/rico/demos.page?...nd_drop_simple

There's a bug in it though, if you scroll down the drag object appears
in the wrong place.


When dragging, the position of the cursor is monitored and the
coordinates used to move the element being dragged and to and see if the
cursor is over a drop-zone.

Search for _updateDropZonesHover.

The function loops through all the dropzones and uses
_mousePointInDropZone to determine if the cursor is over a particular
drop zone.

The algorithm is not optimal - if there are lots of drop zones,
performance will degrade significantly. A better algorithm is to load
the drop zones into a btree or (better) a quadtree and just keep track
of the ones the cursor is over.

It may not make any difference for a small number of zones.

The demos at the link do not work properly in IE if it isn't
full-screen. If you are going to manually track what the cursor is
over, you have a lot of work ahead and it will likely only work in a
small subset of browsers.

A small plaything:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>Drag Drop simple</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<style type="text/css">
body {margin:0;padding:0;}
..dropZone {width:100px;height:100px;border:2px solid blue;}
#dropZone-01 {position:absolute;top:100px;left:200px;}
#dropZone-02 {border-color:red;}
</style>

<script type="text/javascript">

var DragDrop = ( function() {
var docBody = document.body || document.documentElement;
var dZoneIndex = []; // References to drop zones

function dropZone(el){
this.el = el;
this.coords = getElRect(el);
}

function registerDZ(el){
dZoneIndex[dZoneIndex.length] = new dropZone(el);
}

function getElOffset(el){
var elXY = [0,0];
if (el.offsetParent) {
while (el.offsetParent) {
elXY[0] += el.offsetLeft;
elXY[1] += el.offsetTop;
el = el.offsetParent;
}
} else if (el.x){
elXY[0] = el.x;
elXY[1] = el.y;
}
return elXY;
}

function getElRect(el){
var pos = getElOffset(el);
pos[2] = pos[0] + el.offsetHeight;
pos[3] = pos[1] + el.offsetWidth;
return pos;
}

function cursorPos(e){
var e = e || window.event;
var posXY = [0,0];
if (e.pageX || e.pageY) {
posXY[0] += e.pageX;
posXY[1] += e.pageY;
} else if (e.clientX || e.clientY){
posXY[0] += e.clientX + document.body.scrollLeft;
posXY[1] += e.clientY + document.body.scrollTop;
}
return posXY;
}

function trackCursor(e){
var e = e || window.event;
var posXY = cursorPos(e);
var dz;
for (var i=0, n=dZoneIndex.length; i<n; ++i){
dz = dZoneIndex[i];
if ( pointInRect(posXY, dz.coords) ) {
dz.el.innerHTML = 'over';
dz.outline && (dz.outline.style.display = '');
} else {
dz.el.innerHTML = 'out';
dz.outline && (dz.outline.style.display = 'none');
}
}
}

function pointInRect(pos, rect){
return (
pos[0] > rect[0] && pos[0] < rect[2]
&& pos[1] > rect[1] && pos[1] < rect[3] );
}

return ({

registerZonesByClassname : function(cName){
var allEls = document.getElementsByTagName('*');
var el, i = allEls.length;
while (i--){
el = allEls[i];
if (el.className && el.className == cName){
registerDZ(el);
}
}
},

startTrackCursor : function() {
docBody.onmousemove = trackCursor;
},

stopTrackCursor : function() {
if (/\btrackCursor\b/.test(docBody.onmousemove)){
docBody.onmousemove = null;
}
var dz, i=0;
while( (dz = dZoneIndex[i++]) ){
dz.el.innerHTML = '';
}
},

showDropZones : function() {
var dz, i=0;
while( (dz = dZoneIndex[i++]) ){
dz.el.innerHTML += '<br>' + dz.coords;
}
}
});
})();
</script>
</head>

<body onload="
DragDrop.registerZonesByClassname('dropZone');
">
<input type="button" value="Start Track cursor" onclick="
DragDrop.startTrackCursor();
">

<input type="button" value="Stop track cursor" onclick="
DragDrop.stopTrackCursor();
">
<input type="button" value="Show zones" onclick="
DragDrop.showDropZones();
">
<div class="dropZone" id="dropZone-01"></div>
<div class="dropZone" id="dropZone-02"></div>
</body>
</html>

--
Rob
Nov 23 '05 #10
re4:Aaron - thanks for the link to that example. It looks just what I
need to rip apart for clues :)

re4: RobG - and thanks for pointing out the part to look for, and for
the example of mouse tracking

Nov 23 '05 #11
RobG: just to let you know, and anyone else that wants to use this
incredibly useful example, I found a small bug in the code above.

I made some change for testing, putting dropzone 1 directly above
dropzone 2, as you might see in a websites side menu. When the width
and height were the same (100px in your code) this works perfectly, but
if I increased the width to 150px (height still at 100px) then it was
possible to have both dropzones as 'over' when the cursor was hoving
around the top of dropzone 2.

I found the fix in the function below:

function getElRect(el){
var pos = getElOffset(el);
pos[2] = pos[0] + el.offsetHeight;
pos[3] = pos[1] + el.offsetWidth;
return pos;
}

I had to transpose offsetHeight and offsetWidth to get the right
results, i.e.

function getElRect(el){
var pos = getElOffset(el);
pos[2] = pos[0] + el.offsetWidth;
pos[3] = pos[1] + el.offsetHeight;
return pos;
}

I hope you don't mind me pointing this out publically. I personally
found this example to be extremely useful, and would definitely
recommend people come here if they are trying to do what I'm working
on, and figured they should know about this too.

cheers

Nov 23 '05 #12
Kevin Blount wrote:
RobG: just to let you know, and anyone else that wants to use this
incredibly useful example, I found a small bug in the code above.

I made some change for testing, putting dropzone 1 directly above
dropzone 2, as you might see in a websites side menu. When the width
and height were the same (100px in your code) this works perfectly, but
if I increased the width to 150px (height still at 100px) then it was
possible to have both dropzones as 'over' when the cursor was hoving
around the top of dropzone 2.

I found the fix in the function below:

function getElRect(el){
var pos = getElOffset(el);
pos[2] = pos[0] + el.offsetHeight;
pos[3] = pos[1] + el.offsetWidth;
return pos;
}

I had to transpose offsetHeight and offsetWidth to get the right
results, i.e.

function getElRect(el){
var pos = getElOffset(el);
pos[2] = pos[0] + el.offsetWidth;
pos[3] = pos[1] + el.offsetHeight;
return pos;
}

I hope you don't mind me pointing this out publically.


Eeee gad - me, a mistake? Aggghh!! Crikey, *that* has never happened
before!! :=x

I posted it, so I'll happily cop consequences.

I hadn't tested with non-square shapes (but I have now). The use of an
array makes it much more likely that such a mistake would be made, an
object is probably more appropriate (I'm not using any of an array's
specialness) so slap-my-wrist.

An object with named properties would be less prone to such error (but
not foolproof):

function getElRect(el){
var elTL = getElOffset(el);
var elRect = {left:elTL.x, top:elTL.y, bottom:0, right:0};
elRect.right = elRect.left + el.offsetWidth;
elRect.bottom = elRect.top + el.offsetHeight;
return elRect;
}
With appropriate changes to other bits as well.

[...]
--
Rob
Nov 23 '05 #13

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

Similar topics

3
by: Philip | last post by:
I am trying to make a bunched of left-floated divs that will be contained in a larger div. the floated divs all contain a left-floated img and text of varying sizes. If I don't set a height (or...
6
by: Marcus Otmarsen | last post by:
Ok, the situation is as follows: I defined a <DIV> like: <div id="myid" style="position: absolute; height: 10; width: 10;"><img src="images/object.gif" height=10 width=10></div> This pane is...
44
by: Jim M | last post by:
I have had great success with using <iframe> with overflow-y set to auto. I can get a similar look with the <iframe> tag. BUT... In all cases I need to have fixed heights. Is there a way to...
1
by: Steve Klett | last post by:
Hi- I was just looking at te source for a site that I really liked and I noticed they are using DIV tags where I assumed that would be using a mess of nested tables. Now, this looks like a much...
1
by: sid | last post by:
Is it possible to float a <divon top of a <frameset ...? I need it to overlap parts of the frames. Thanks Sid.
5
nathj
by: nathj | last post by:
Hi All, I'm working on a new site that has a two column layout underneath a title bar. If you check out: http://www.christianleadership.org.uk/scratch/mainpage.php using IE or Opera you will...
2
by: bgold12 | last post by:
Are there any non-semantic (i.e. actual) differences between the <p> tag and the <divtag?
4
by: harryusa | last post by:
I am trying to center 2 images concentrically which are z-indexed to lay on top of each other making an image with a border from another image that has a transparent center. I need the images to be...
8
prino
by: prino | last post by:
Hi all, I've written code (in REXX) that takes files in legacy languages (PL/I, COBOL, z/OS assembler, etc) and converts them into HTML in a format similar to what's displayed in the z/OS ISPF...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.