简体   繁体   English

Firefox仅显示一次标题属性

[英]Firefox show title attribute only once

When you move the mouse over a canvas I want it to show the coordinates. 当您将鼠标移到画布上时,我希望它显示坐标。

When you change the mouse position (without move out of the canvas) I want it to show the new coordinates. 当您更改鼠标位置(不移出画布)时,我希望它显示新坐标。 A little delay would be nice. 稍加延迟会很好。

I'm using a tooltip but this code in firefox doesn't work: the title appears only when you enter the canvas and then you have to move out and re-entering the canvas to show it again. 我使用的是工具提示,但是firefox中的此代码不起作用:仅当您进入画布时才出现标题,然后您必须移出并重新进入画布才能再次显示它。

How can I solve? 我该如何解决?

document.getElementById('mycanvas').addEventListener(
    'mousemove', function(event) {
        mycanvas.title=event.layerX;
    });

You can't control how the browser chooses to display an element's title. 您无法控制浏览器如何选择显示元素的标题。 It's " usually displayed in a 'tool tip' popup when the mouse is over the displayed node " , but the delay to appear and how often it is updated is not under your control. 通常是“当鼠标悬停在显示的节点上时显示在“工具提示”弹出窗口中 ,但是显示的延迟以及更新的频率不受您控制。

Below is an example that tracks mouse position and shows coordinates in a popup that you have total control over (delay to appear, offsets, style etc). 下面是一个跟踪鼠标位置并在弹出窗口中显示坐标的示例,您可以完全控制该弹出窗口(显示延迟,偏移量,样式等)。 Move your mouse over the canvas, wait a second on a given position to let the popup appear, move elsewhere, wait, etc. 将鼠标移到画布上,在给定位置上等待一秒钟,以使弹出窗口出现,移至其他位置,等待,等等。

Noteworthy details: 值得注意的细节:

  • Uses setTimeout at each mousemove event, and clearTimeout to clear previous one. 在每个mousemove事件中使用setTimeout ,并使用clearTimeout清除上一个事件。
  • Uses jQuery to "normalize" the event, using pageX/pageY and $.offset() , to place the tooltip itself at appropriate coordinates (taking into account window scrolling), and make it show coordinates that are relative to the canvas. 使用jQuery通过pageX/pageY$.offset()来“规范化”事件,以将工具提示自身放置在适当的坐标处(考虑到窗口滚动),并使其显示相对于画布的坐标。 (I placed the canvas within a div that has a 20px margin to show that). (我将画布放在div内,边缘有20px的像素来显示)。

(I initially wanted to do it without jQuery, but browser quirks about mouse event properties made me abandon that idea, and after multiple edits I settled with jQuery to get something working) (我最初想在没有jQuery的情况下执行此操作,但是浏览器关于鼠标事件属性的怪癖使我放弃了这个想法,经过多次编辑后,我选择使用jQuery来使某些功能正常工作)

 var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); var background = document.getElementById('background'); if (background.complete) context.drawImage(background, 0, 0); else background.onload = function() { context.drawImage(background, 0, 0); } var caption = document.getElementById('caption'); var pos = {}; var timerID; $(canvas).mousemove(function(e) { pos = { x: e.pageX - $(this).offset().left, y: e.pageY - $(this).offset().top, captionX: e.pageX, captionY: e.pageY, }; caption.setAttribute('style', 'display:none'); if (timerID) { clearTimeout(timerID); } timerID = setTimeout(showPosition, 500); }); canvas.addEventListener('mouseleave', function(event) { caption.setAttribute('style', 'display:none'); if (timerID) { clearTimeout(timerID); } }); function showPosition() { caption.setAttribute('style', 'display: inline; left:'+(pos.captionX + 8)+"px; top: "+(pos.captionY + 8)+"px;"); caption.innerHTML = "(" + pos.x + "," + pos.y + ")"; } 
 #background {visibility:hidden;} #caption { display: none; position:absolute; text-align: center; margin: 3px; width: 120px; height: 24px; background: black; color: white; } 
 <div style="margin: 20px"> <canvas id="canvas" width="480" height="320"></canvas> </div> <div id="caption"></div> <img src="https://i.imgur.com/PWSOy.jpg" id="background" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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