【发布时间】:2017-04-29 19:44:09
【问题描述】:
嗨,我知道我们可以触发点击事件。但我想知道我们是否可以在没有用户实际移动鼠标的情况下触发 mousemove 事件。
说明:
我想在用户选择某些内容时显示一条消息。在画布上,我的画布是全高和全宽的,当用户单击一个按钮时,画布就会显示出来。当用户移动鼠标时,他会看到一条消息“单击并拖动网页的任何部分”。此消息跟随用户的鼠标移动。
我想做什么:
当用户单击按钮时,他应该会看到“单击并拖动网页的任何部分”的消息。并且消息必须跟随用户移动鼠标的位置。
问题:
用户在点击后无法看到消息,直到他/她移动鼠标。
代码:
function activateCanvas() {
var documentWidth = jQ(document).width(),
documentHeight = jQ(document).height();
jQ('body').prepend('<canvas id="uxa-canvas-container" width="' + documentWidth + '" height="' + documentHeight + '" ></canvas><form method="post" id="uxa-annotations-container"></form>');
canvas = new UXAFeedback.Canvas('uxa-canvas-container', {
containerClass: 'uxa-canvas-container',
selection: false,
defaultCursor: 'crosshair'
});
jQ(function() {
var canvas = jQ('.upper-canvas').get(0);
var ctx = canvas.getContext('2d');
var x,y;
var tooltipDraw = function(e) {
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.restore();
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
var str = 'Click and drag on any part of the webpage.';
ctx.fillStyle = '#ddd';
ctx.fillRect(x + 10, y - 60, 500, 40);
ctx.fillStyle = 'rgb(12, 106, 185)';
ctx.font = 'bold 24px verdana';
ctx.fillText(str, x + 20, y - 30, 480);
};
canvas.addEventListener('onfocus',tooltipDraw,0);
canvas.addEventListener('mousemove',tooltipDraw,0);
canvas.addEventListener('mousedown', function() {
canvas.removeEventListener('mousemove', tooltipDraw, false);
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.restore();
}, false);
});
}
jQ('body').on('click', '.mood_img_div', function() {
// alert("soemthing");
toggleOverlay();
activateCanvas();
});
我创建了一个在单击后调用但消息不可见的函数。有没有办法第一次用消息调用它,并且每次用户使用鼠标时都会显示。
我已经用 jQ 替换了 jQuery,因为我正在制作自己的插件(这不是问题的原因)
【问题讨论】:
-
什么是
jQUxarmy?应该是jQuery? -
这是什么
jQUxarmy?它是 jQuery 的编辑版本吗? -
我已经用我自己的变量 jQUxarmy 替换了 jquery。
-
为什么不在点击处理程序的末尾添加对
tooltipDraw的调用?你只需要传递 click 事件的参数就可以了,没有? -
实际上是在单击时调用该函数,但仅在用户移动鼠标时才绘制文本。我正在寻找任何内容,以便在用户单击后立即显示文本。所以我需要生成mousemove事件@Kaiido
标签: javascript jquery html canvas