Link to home
Start Free TrialLog in
Avatar of Moiz Saifuddin
Moiz Saifuddin

asked on

Move control

How can i move a control up or down or on an (x,y)-axis on my page using javascript..




Moiz
ASKER CERTIFIED SOLUTION
Avatar of archrajan
archrajan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you mean move along with the mouse?

<html>
<head>
<meta http-equiv="imagetoolbar" content="no">

<script language="Javascript">

var m=false;
function Mc_move(){
   if(m){
       document.images["loader"].style.left=event.clientX-offX;
       document.images["loader"].style.top=event.clientY-offY;
       return false
   }
   return true
}
function Mc_down(){
   if(!m){
      m=true;
      offX=event.clientX-document.images["loader"].offsetLeft;
      offY=event.clientY-document.images["loader"].offsetTop;
      return false
   }
   return true
}
function Mc_up(){
   if(m){
      m=false;
      x=document.images["loader"].offsetLeft;
      y=document.images["loader"].offsetTop;
      return false
   }
   return true
}

</script>
</head>
<body onmousemove="Mc_move()" onmouseup="m=false;" oncontextmenu="return true" ondragstart="return false">

<img src="http://gladstone.uoregon.edu/~reubanks/Cars%20&%20Bikes/Bugatti%20Veyron.jpg" width="350" height="250" name="loader" onmousedown="Mc_down()"  onmouseup="Mc_up()" style="position:absolute;left:20px;filter:progid:DXImageTransform.Microsoft.Shadow(color='#444444', Direction=135, Strength=6) alpha(opacity=94); -moz-opacity:0.9;;cursor:pointer">

</body>
</html>
pixelLeft and pixelTop are IE only.

offsetLeft and offsetTop are cross-browser, but not what you want to update.  For styled element what yo want to update are the style properties and you have to inclue the unit of measure to be standards compliant:

function resetPosBy(obj,x,y)
{
   newx = obj.offsetLeft+x;
   newy = obj.offsetTop+y;
   obj.style.left=newx+'px';
   obj.style.top=newy+'px';
}
and you can fir it from any event:

onclick="resetPosBy('xxx',20,-50)"

Cd&
speaking of cross-browser, the code I provided above was suppose to be good for IE only and here's the cross-browser version


<html>
<head>
<meta http-equiv="imagetoolbar" content="no">

<script language="Javascript">

var m=false, offX, offY, y, x;

function Mc_move(e){
      x=e.clientX;
      y=e.clientY;
   if(m){
       document.images["loader"].style.left=(x-offX)+"px";
       document.images["loader"].style.top=(y-offY)+"px";
       return true;
   }
   return true;
}
function Mc_down(e){
      m=true;
   if(m){
      offX=x-document.images["loader"].offsetLeft;
      offY=y-document.images["loader"].offsetTop;
      return false;
   }
   return true
}
function Mc_up(e){
   if(m){
      m=false;
      return false;
   }
   return true;
}

</script>
</head>
<body onmousemove="Mc_move(event)" oncontextmenu="return true" onselectstart="return false" ondragstart="return false">

<img src="http://gladstone.uoregon.edu/~reubanks/Cars%20&%20Bikes/Bugatti%20Veyron.jpg" width="350" height="250" name="loader" onmousedown="Mc_down(event)"  onmouseup="Mc_up(event)" style="position:absolute;left:20px;filter:progid:DXImageTransform.Microsoft.Shadow(color='#444444', Direction=135, Strength=6) alpha(opacity=94); -moz-opacity:0.9;;cursor:pointer">

</body>
</html>
>  you have to inclue the unit of measure to be standards compliant...

good to keep in mind but not necessary
Moiz, any luck so far?
Avatar of Moiz Saifuddin
Moiz Saifuddin

ASKER

yes, i tried archarians solution, worked for me...