Gif image that follows the mouse position

Hello,

I want to change the script below.

The Gif-image should only move if the left mouse
button is pressed.

How can I do it?


<html>
<head>
<script type="text/javascript">

function aa(viking)
{
web=window.event.button
var ex=window.event.x;
var ey=window.event.y;
//var oL=e.offsetLeft;
viking.style.left=ex-33;
viking.style.top=ey-55;
BN5.value=web+'=button X='+ex+' Y='+ey;
}
</script>
</head>

<body>
<p>
<input name="BN5" type="button" value="Klick">
<img src="http://files.opera-info.de/bilder/forum/user/viking.gif" name="snow"
style="position:absolute" width="70" height="99" onmousemove="aa(this)">
</p>
</body>
</html>
LVL 17
vb_elmarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason ScolaroCommented:
Hi vb_elmar,

Try replacing these parts:

<script type="text/javascript">
var active=false;
function aa(viking)
{
if (!active) return;
web=window.event.button
var ex=window.event.x;
var ey=window.event.y;
//var oL=e.offsetLeft;
viking.style.left=ex-33;
viking.style.top=ey-55;
document.getElementById('BN5').value=web+'=button X='+ex+' Y='+ey;
}
</script>

<img src="http://files.opera-info.de/bilder/forum/user/viking.gif" name="snow" style="position:absolute" width="70" height="99" onclick="active=!active;" onmousemove="aa(this)">

-- Jason
Hans LangerCommented:
Try this:

<HTML>
<HEAD>
<TITLE>GERENTE EXAMPLE, MOVE OBJECT</TITLE>
</HEAD>
<SCRIPT>

xm = 0;
ym = 0;
M  = false;
window.onload = function(){
      document.onmousedown= function(e){
            if (!e) e = window.event;
            tg = (e.target) ? e.target : e.srcElement
            if(tg.className =="d" && e.button==2){
                  M = document.getElementById('im');
                  with(M.style){
                        xZ = pixelLeft-xm;
                        yZ = pixelTop-ym;            
                  }
                  return false;      
            }
                  
      }
      document.onmouseup= function(){
            if(M){M = false;}
      }
      document.onmousemove=function(e){            
            if (!e) e = window.event;
            tg = (e.target) ? e.target : e.srcElement
                  xm = (e.x || e.clientX);
                  ym = (e.y || e.clientY);
                  if(M){
                        M.style.pixelLeft=xm+xZ;
                        M.style.pixelTop=ym+yZ;            
                        return false
                  }                  
      }      
}

</SCRIPT>

<BODY oncontextmenu="return false">
<img id="im" class="d" width="100" height="100" style="position:absolute">
</BODY>
</HTML>



Regards,

GERENTE

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.