Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-20-2012, 07:01 PM   PM User | #1
chopficaro
New Coder

 
Join Date: Dec 2009
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
chopficaro is an unknown quantity at this point
image appear to the right of cursor onmouseover

how do i make an image appear to the right of the user's cursor onmouseover?
luv u guys btw!
chopficaro is offline   Reply With Quote
Old 09-21-2012, 12:04 AM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
If you use onmouseover then the image will appear once in odd positions. This is because onmouseover happens just as the mouse moves over (into) an element.

The following example uses onmousemove so that the image follows the cursor as it moves within the element.
Code:
<!DOCTYPE html>
<html>
<head>
<title>Some Title</title>
<style type="text/css">
#follower { 
    position: absolute;
}
#thebox {
    width: 400px;
    height: 300px;
    outline: red solid thin;
}
</style>
</head>
<body>
<img id="follower" src="http://lorempixel.com/150/150/sports" 
    height="150" width="150" alt="follow cursor">
<div id="thebox">Big box</div>
<script type="text/javascript">

window.onload = function () {
    var follower;
    document.getElementById('thebox').onmousemove = function (e) {
        var evt = e || window.event;
        follower = follower || document.getElementById('follower');
        follower.style.left = parseInt(evt.pageX)-170+"px";
        follower.style.top = parseInt(evt.pageY)-40+"px";
    };
}
</script>
</body>
</html>
Use this as a guide. Otherwise, you'll need to clarify what you are hoping to achieve.

Added: You could set css display:none for the image and change this within the mousemove event so that it appears. You would then need to also add onmouseout to hide the image again.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 09-21-2012 at 12:13 AM..
AndrewGSW is offline   Reply With Quote
Old 09-21-2012, 10:09 AM   PM User | #3
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
X-Browser


Code:
<!DOCTYPE html>
<html>
<head>
<title>Some Title</title>
<style type="text/css">
#follower {
    position: absolute;
}

</style>
</head>
<body>
<img id="follower" src="http://lorempixel.com/150/150/sports" height="150" width="150" alt="follow cursor">
<script type="text/javascript">

window.onload = function () {
    document.onmousemove = function (e) {
        e = e || window.event,mse=Mse(e);
        var img = document.getElementById('follower');
        img.style.left = mse[0]+17+"px";
        img.style.top = mse[1]+"px";
    }
}

function Mse(e){
 if (window.event){
  var docs=[document.body.scrollLeft,document.body.scrollTop];
  if (!document.body.scrollTop){
   docs=[document.documentElement.scrollLeft,document.documentElement.scrollTop];
  }
  return [e.clientX+docs[0],e.clientY+docs[1]];
 }
 return [e.pageX,e.pageY];
}


</script>
</body>
</html>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 09-21-2012, 12:45 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
@Vic Thank you. Don't know why I put the image to the left, Doh!
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 09-24-2012, 11:32 PM   PM User | #5
chopficaro
New Coder

 
Join Date: Dec 2009
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
chopficaro is an unknown quantity at this point
TYVM! this is perfect!
chopficaro is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:17 AM.


Advertisement
Log in to turn off these ads.