Two gif images following the mouse position

Hello,

I want to change the script below.

Each of the 2 Gif-images should move, but only if the gif image
is under the mouse cursor, and 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;
viking.style.left=ex-33;
viking.style.top=ey-55;
}
</script>
</head>

<body>
<p>
<img src="http://files.opera-info.de/bilder/forum/user/viking.gif" style="position:absolute" onmousemove="aa(this)">
<img src="http://files.opera-info.de/bilder/forum/user/viking.gif" style="position:absolute;left=170" 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.

LakioCommented:
<html>
<head>
<script type="text/javascript">
var on;
function aa(viking){
if(viking==on){
web=window.event.button
var ex=window.event.x;
var ey=window.event.y;
viking.style.left=ex-33;
viking.style.top=ey-55;
}
}
</script>
</head>

<body>
<p>
<img src="http://files.opera-info.de/bilder/forum/user/viking.gif" style="position:absolute" onclick="on=(on!=this)?this:0;" onmousemove="aa(this)">
<img src="http://files.opera-info.de/bilder/forum/user/viking.gif" style="position:absolute;left=170" onclick="on=(on!=this)?this:0;" onmousemove="aa(this)">
</p>
</body>
</html>
LakioCommented:
ups, this is better:
<img src="http://files.opera-info.de/bilder/forum/user/viking.gif" style="position:absolute" onclick="on=(on==this)?0:this;" onmousemove="aa(this)">
<img src="http://files.opera-info.de/bilder/forum/user/viking.gif" style="position:absolute;left=170" onclick="on=(on==this)?0:this;" onmousemove="aa(this)">
LakioCommented:
or this:
<html>
<head>
<script type="text/javascript">
var on;
function aa(){if(on){
var ex=window.event.x;
var ey=window.event.y;
on.style.left=ex-33;
on.style.top=ey-55;
}}
document.onmousemove=aa
</script>
</head>

<body
<p>
<img src="http://files.opera-info.de/bilder/forum/user/viking.gif" style="position:absolute" onclick="on=(on==this)?0:this;">
<img src="http://files.opera-info.de/bilder/forum/user/viking.gif" style="position:absolute;left=170" onclick="on=(on==this)?0:this;">
</p>
</body>
</html>
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

vb_elmarAuthor Commented:
Thanks,

your sample works, but it should not be a "toggle" function for the left mouse button.

The image should move, during the left mouse button is pressed.
Hans LangerCommented:
Again ? =)


<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">
<div id="im"  style="position:absolute">
<img  width="100" class="d" height="100" title="image 1">
<img  width="100" class="d" height="100" title="image 2">
</div>
</BODY>
</HTML>


regards,

GERENTE
Michel PlungjanIT ExpertCommented:
Firefox:
Error: pixelLeft is not defined
Line: 18
vb_elmarAuthor Commented:
Your sample works, but NOT BOTH images should move,
during the mouse button is pressed.

Only the gif-image, what currently is under the mouse cursor,
should move.
Hans LangerCommented:
<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 = tg;
               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  width="100" class="d" height="100" title="image 1" style="position:absolute;top:150px">
<img  width="100" class="d" height="100" title="image 2" style="position:absolute">

</BODY>
</HTML>

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.