PDA

View Full Version : Modifying drag script for drag limit: howto??



shachi
09-17-2006, 03:32 PM
Hello all,
I was playing around this:



<html>
<head>
<script type="text/javascript">
var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;

function movemouse(e)
{
if (isdrag)
{
dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
dobj.style.top = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
return false;
}
}

function selectmouse(e)
{
var fobj = nn6 ? e.target : event.srcElement;
var topelement = nn6 ? "HTML" : "BODY";
while (fobj.tagName != topelement && fobj.className != "dragme")
{
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
}
if (fobj.className=="dragme")
{
isdrag = true;
dobj = fobj;
tx = parseInt(dobj.style.left+0,10);
ty = parseInt(dobj.style.top+0,10);
x = nn6 ? e.clientX : event.clientX;
y = nn6 ? e.clientY : event.clientY;
document.onmousemove=movemouse;
return false;
}
}
document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");
</script>
<style type="text/css">
.dragme{position:relative;}
#test {
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div id="test" class="dragme"></div>
</body>
</html>


drag script and then I thought of something. How do I limit the drag, for example how do I stop the element being dragged from dragging if the element reaches 100px far from the left?? I tried :


if(!dobj.offsetLeft > 100){
dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
dobj.style.top = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
} else {
return false;
}

but it doesn't seem to work. Any help here PLEASE!!!!

Thanks. :)

mburt
09-17-2006, 04:56 PM
Set the onmousemove event handler to null, instead of trying to set the width when the user drags past a certain point.

shachi
09-17-2006, 05:07 PM
instead of trying to set the width when the user drags past a certain point.


What do you mean??:confused:

mburt
09-17-2006, 05:08 PM
I meant to say (in place of width): the pixels from the left of the screen

Sorry about that. :)

shachi
09-17-2006, 05:19 PM
mburt: never mind about that but I still can't get what you are trying to say. If I set the onmousemove to null then the drag will stop completely. Here's my code so far(if I understood what you meant).



<html>
<head>
<script type="text/javascript">
var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;

function movemouse(e){
if (isdrag){
if(dobj.offsetLeft < 500){
dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
// dobj.style.top = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
} else {
document.onmousemove = null;
}
return false;
}
}

function selectmouse(e){
var fobj = nn6 ? e.target : event.srcElement;
var topelement = nn6 ? "HTML" : "BODY";
while (fobj.tagName != topelement && fobj.className != "dragme"){
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
}
if (fobj.className=="dragme"){
isdrag = true;
dobj = fobj;
tx = parseInt(dobj.style.left+0,10);
ty = parseInt(dobj.style.top+0,10);
x = nn6 ? e.clientX : event.clientX;
y = nn6 ? e.clientY : event.clientY;
document.onmousemove=movemouse;
return false;
}
}
document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");
</script>
<style type="text/css">
.dragme{position:relative;}
#test {
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div id="test" class="dragme"></div>
</body>
</html>

mburt
09-17-2006, 05:22 PM
Yes. You want it to stop completely when the value is more or less then the desired value, then you set the "else" statement to:

document.onmousemove = movemouse;

shachi
09-17-2006, 05:25 PM
mburt: actually that's not what I meant. I need it to stop completely *but* it must be abled to be dragged again. If I set it to document.onmousemove = null it can't be dragged once it exceedes it's limit. Well, let me try what you suggested, here's my code:



<html>
<head>
<script type="text/javascript">
var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;

function movemouse(e){
if (isdrag){
if(dobj.offsetLeft < 500){
dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
// dobj.style.top = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
} else {
document.onmousemove = mousemove;
}
return false;
}
}

function selectmouse(e){
var fobj = nn6 ? e.target : event.srcElement;
var topelement = nn6 ? "HTML" : "BODY";
while (fobj.tagName != topelement && fobj.className != "dragme"){
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
}
if (fobj.className=="dragme"){
isdrag = true;
dobj = fobj;
tx = parseInt(dobj.style.left+0,10);
ty = parseInt(dobj.style.top+0,10);
x = nn6 ? e.clientX : event.clientX;
y = nn6 ? e.clientY : event.clientY;
document.onmousemove=movemouse;
return false;
}
}
document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");
</script>
<style type="text/css">
.dragme{position:relative;}
#test {
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div id="test" class="dragme"></div>
</body>
</html>



Gives out an error: Error: mousemove is not defined
Source File: ----:///----/------/testdrag.html
Line: 16

mburt
09-17-2006, 05:31 PM
I meant to say:

document.onmousemove = movemouse

The point is to use whatever your function is for actually moving the object and calling it again.

mburt
09-17-2006, 05:32 PM
I see your point though. I'm not really sure why you wouldn't be able to continue dragging, I'll try to work it out.

shachi
09-17-2006, 05:33 PM
mburt: did you even bother to read the initial code?? The function to move the object *is* movemouse, I apologize if it was a little harsh.

EDIT: You've posted before me.:p

mburt
09-17-2006, 05:57 PM
Try a "return false" in place of the document.onmousemove = null

shachi
09-17-2006, 06:07 PM
Tried already, same results.