javascript - Make image follow cursor when hovering over an element - Stack Overflow

I was wondering whether it is possible with JavaScript to create a <a> element which at hover wou

I was wondering whether it is possible with JavaScript to create a <a> element which at hover would display an image that follows the position of the mouse.

I found a lot of solutions for the hover part, but I haven't found any which would also follow the mouse.

In other words, on hover, the image doesn't follow the mouse but just appears and dissapears on hover, how can I modify my code in order to make it follow the mouse? This is what I have tried so far :

<a href="link.html" onmouseover="document.getElementById('hoverimage').style.display = 'block';" onmouseout="document.getElementById('hoverimage').style.display = 'none';">hoverlink</a>

<div style="display: none;" id="hoverimage">
   <img src="image.png" />
</div>

I was wondering whether it is possible with JavaScript to create a <a> element which at hover would display an image that follows the position of the mouse.

I found a lot of solutions for the hover part, but I haven't found any which would also follow the mouse.

In other words, on hover, the image doesn't follow the mouse but just appears and dissapears on hover, how can I modify my code in order to make it follow the mouse? This is what I have tried so far :

<a href="link.html" onmouseover="document.getElementById('hoverimage').style.display = 'block';" onmouseout="document.getElementById('hoverimage').style.display = 'none';">hoverlink</a>

<div style="display: none;" id="hoverimage">
   <img src="image.png" />
</div>

Share Improve this question edited Dec 3, 2018 at 22:42 Jack Bashford 44.2k11 gold badges55 silver badges82 bronze badges asked Aug 24, 2018 at 9:08 CodingMageCodingMage 1352 silver badges17 bronze badges 2
  • Have a look at this codepen codepen.io/redspiderfish/pen/MYmeYz – Remco Commented Aug 24, 2018 at 9:15
  • Using the codepen above i have editted to do the job you was looking for. Check my codepen out: codepen.io/HeeneyOG/pen/PoJgmNz – Jack H Commented Jan 18, 2022 at 13:18
Add a ment  | 

2 Answers 2

Reset to default 4

Use the clientX and clientY positions of the mouse, and set the top and left style positions of the image to those.

<a href="#" onmouseover="interval()">Link</a>

function interval() {
    while (true) {
        setInterval(showImage, 1);
    }
}

function showImage() {

    var x = clientX;
    var y = clientY;
    var image = document.getElementById("hoverimage");
    image.style.left = x;
    image.style.top = y;
 }

Once you hover on the link, the image will move to the mouse's position once a millisecond, forever.

It is possible. You need to get mouse's clientX and clientY position when hovering over certain element (body, div, etc.) and add that coordinates to your created image

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745237748a4617991.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信