简体   繁体   English

在单击时将div浮动到画布上方的代码

[英]Code to float a div above canvas at point of click

I've got an HTML5 canvas element and an event listener for clicks, what I need is to link this up to a floating div that appears right where the user clicks on the canvas. 我有一个HTML5 canvas元素和一个单击事件监听器,我需要将它链接到一个浮动div,该div出现在用户单击画布的位置。 Code suggestions for best way to do this? 最佳做法的代码建议?

I'm tracking the mouse position with: 我正在跟踪鼠标位置:

        var pos = getMousePos(canvas, evt);
        mousePos.x = pos.x;
        mousePos.y = pos.y;

This should do the trick: 这应该可以解决问题:

HTML: HTML:

<div style="position: relative">
    <canvas />
    <div style="position: absolute" id="floatingDiv">Floating div</div>
</div>

Javascript: Javascript:

var pos = getMousePos(canvas, evt);
mousePos.x = pos.x;
mousePos.y = pos.y;
var floatingDiv = document.getElementById('floatingDiv');
floatingDiv.style.top = pos.y + 'px';
floatingDiv.style.left = pos.x + 'px';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM