Hi, im new to javascript and CSS.
I have made a little drag and drop application.
Im basically drag and dropping a “DIV” or “TABLE” inside a TABLE with a background image.
My problem is, the dragging works, but its not fluid, its not smooth, it chunks up kinda as if its lagging, you know when your PC is working too hard and you try to move your icons too fast you see it move but it makes big jumps.
We’ll I checked other previous codes and mine isn’t bad, it’s probably not good either, but I don’t get why its so choppy.
Can anyone help?
I’ll paste the code here.
function setposition() {
if (cObject != null) {
cObject.style.left = lastX + event.clientX – curX;
cObject.style.top = lastY + event.clientY – curY;
}
}
function startdrag(object) {
cObject = document.getElementById(object);
curX = event.clientX;
curY = event.clientY;
}
function stopdrag(object) {
if (cObject != null) {
lastX = parseInt(cObject.style.left+0);
lastY = parseInt(cObject.style.top+0);
cObject = null;
}
}
See these are my 3 functions. I use the events:
onmousedown=”startdrag(‘element’);”
onmousemove=”setposition();”
onmouseup=”stopdrag(‘element’);”
If anyone can give me tips on how to improve it maybe, or just why its choppy I would appreciate it. Thanks.
Thanks for the fast reply. I appreciate your example and help.
window.captureEvents(event.MOUSEUP);
window.onmouseup= stopdrag();
Although I find it odd since it comes from an actual example, is there a reason I get this problem? Thanks.[/QUOTE]
you have a problem in your implementation
this line
[b]
window.onmouseup= stopdrag();
[/b]
should be
[b]
window.onmouseup= stopdrag;
[/b]
because you are telling the JS engine to refer to a function..[/QUOTE]
[b]"this object does not support this property."[/b]
This error means that you are trying to access an invalid property for an object. As you already have mentioned that code does work and is live for viewing on the page I linked above.
I would like to see your implementation of the code. I am 100% positive you implemented it wrong...[/QUOTE]
first off pu the following lines of code out side of the init function
window.captureEvents(event.MOUSEUP);
window.onmouseup= stopdrag;
then when I say show the code that means full code, better yet if you have the page a site that some one can access using internet is even better.[/QUOTE]
As I said problem is in your implementation. JavaScript is case sensitive language.
this line
window.captureEvents(event.MOUSEUP);
should be like this
window.captureEvents(Event.MOUSEUP);
and this line as I told u befor e
window.onmouseup= stopdrag();
should be like this
window.onmouseup= stopdrag;[/QUOTE]
[code=php]
<script type="text/javascript">
<!--
function init() {
if(!document.all){
window.captureEvents(Event.MOUSEUP);
}
}
function stopdrag(e){
alert("Clicked")
}
document.onmouseup= stopdrag;
//-->
</script>
</head>
<body class="body" onload="init();">
</body>
[/code]
Ok here is the deal.
this line here is only for older netscape type browsers.
window.captureEvents(Event.MOUSEUP);
As far as I know now adays the latest browser from mozila does not necessarily require that construct to capture event.
anyways here is what you are trying to do and its a working example for u
[code=php]
[/QUOTE]
<script type="text/javascript">
<!--
function init() {
if(!document.all){
window.captureEvents(Event.MOUSEUP);
}
}
function stopdrag(e){
alert("Clicked")
}
document.onmouseup= stopdrag;
//-->
</script>
</head>
<body class="body" onload="init();">
</body>
[/code]
Thanks, that example worked.
[/QUOTE]
Thanks, that example worked.
"if(!document.all){" < this line fixed the problem on load.
Is there a reason why if removed it don't work. Would you know why it is required in such a case?
[/QUOTE]
And also i'd like to know why you put type="text/javascript" instead of just language="javascript" which is better?
Thanks for the help.[/QUOTE]
[color=green] You are welcome[/color]
[color=green]if(!document.all) this condition makes sure that the piece of code that follows only executed when its not a modern or legacy IE browser(![/color]
[color=green] [/QUOTE]
language attribute is deprecated and should not be used any more
[/color]
[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
zxcDockAry=new Array();
function zxcInit() {
document.getElementById('Move').onmousedown=function(event) { zxcMseDown(event,this);}
document.getElementById('Move2').onmousedown=function(event) { zxcMseDown(event,this);}
// remove next 6 lines if docking not required
zxcobjs=document.getElementsByTagName('BODY')[0].getElementsByTagName('*');
for (zxc0=0;zxc0<zxcobjs.length;zxc0++){
if (zxcobjs[zxc0].className=='Dock'){
zxcDockAry[zxcDockAry.length]=zxcobjs[zxc0];
}
}
}
function zxcMseDown(event,obj) {
document.onmousemove=function(event){zxcDrag(event);}
document.onmouseup=function(event){zxcMseUp(event);}
zxcObj=obj;
zxcMse(event);
zxcDragX=zxcMseX-zxcObj.offsetLeft;
zxcDragY=zxcMseY-zxcObj.offsetTop;
}
function zxcMseUp(event){
document.onmousemove=null; zxcDragX=-1; zxcDragY=-1;
// remove next 7 lines if docking not required
for (zxc0=0;zxc0<zxcDockAry.length;zxc0++){
if (zxcPos(zxcObj)[0]>zxcPos(zxcDockAry[zxc0])[0]&&zxcPos(zxcObj)[0]+zxcObj.offsetWidth<zxcPos(zxcDockAry[zxc0])[0]+zxcDockAry[zxc0].offsetWidth&&zxcPos(zxcObj)[1]>zxcPos(zxcDockAry[zxc0])[1]&&zxcPos(zxcObj)[1]+zxcObj.offsetHeight<zxcPos(zxcDockAry[zxc0])[1]+zxcDockAry[zxc0].offsetHeight){
zxcObj.style.left=zxcPos(zxcDockAry[zxc0])[0]+'px';
zxcObj.style.top=zxcPos(zxcDockAry[zxc0])[1]+'px';
alert('Docked');
}
}
}
function zxcDrag(event){
zxcMse(event);
zxcObj.style.left=(zxcMseX-zxcDragX)+'px';
zxcObj.style.top=(zxcMseY-zxcDragY)+'px';
}
function zxcMse(event){
if(!event) var event=window.event;
if (document.all){ zxcMseX=event.clientX; zxcMseY=event.clientY; }
else {zxcMseX=event.pageX; zxcMseY=event.pageY; }
}
function zxcPos(zxc){
zxcObjLeft = zxc.offsetLeft;
zxcObjTop = zxc.offsetTop;
while(zxc.offsetParent!=null){
zxcObjParent=zxc.offsetParent;
zxcObjLeft+=zxcObjParent.offsetLeft;
zxcObjTop+=zxcObjParent.offsetTop;
zxc=zxcObjParent;
}
return [zxcObjLeft,zxcObjTop];
}
//-->
</script>
</head>
<body onload="zxcInit();" >
<div id=Move style="position:absolute;z-Index:2;width:100px;height:100px;background-Color:red;" >Move and Dock Me on another square
</div>
<table id=Move2 style="position:absolute;top:100px;z-Index:2;background-Color:red;" width="200" height="100" border="1">
<tr>
<td>Move Me</td>
</tr>
<tr>
<td>To where you want</td>
</tr>
</table><center><div class="Dock" style="position:relative;width:110px;height:110px;background-Color:blue;" >
</div>
<div class="Dock" style="position:relative;width:110px;height:110px;background-Color:YELLOW;" >
</div>
<div class="Dock" style="position:relative;width:110px;height:110px;background-Color:green;" >
</div>
</center>
<div class="Dock" style="position:relative;width:110px;height:110px;background-Color:silver;" >
</div>
</body>
</html>
[/CODE]
should not matter whats in the box[/QUOTE]
0.1.9 — BETA 5.5