[英]Javascript mousedown and mouseup on different elements
I am trying to develop a word search game in JS which looks like this : https://jquery-wordsearch-game.googlecode.com/svn/trunk/demo.html 我正在尝试使用JS开发类似于以下内容的单词搜索游戏: https : //jquery-wordsearch-game.googlecode.com/svn/trunk/demo.html
BTW I am not using that guy's plugin and instead, trying to develop on my own. 顺便说一句,我没有使用那个家伙的插件,而是尝试自己开发。
I am using the following code to fire a handler to highlight the cells when a user clicks and moves over them. 我使用以下代码来触发处理程序,以在用户单击并在其上移动时突出显示这些单元格。
$('#puzzlecontainer').on('mousedown','.block',myHandler);
The problem is that the mousedown event is fired on the first cell only. 问题在于,仅在第一个单元格上触发mousedown事件。 I want handlers to fire on all cells in the path of the mouse. 我希望处理程序在鼠标路径中的所有单元格上触发。
Also how can I make this compatible with touch events ? 另外,如何使它与触摸事件兼容? I tried touchmove and touchdown. 我尝试了touchmove和触地得分。
Please help 请帮忙
With Shusl's help I added the following code : var ismosedown = false; 在Shusl的帮助下,我添加了以下代码:var ismosedown = false;
$('#puzzlecontainer').on('vmousedown','.block', function(){
globalvars.ismousedown =true;
$(this).addClass("active");
});
$('#puzzlecontainer').on('vmouseover','.block', function(){
if(globalvars.ismousedown){
$(this).addClass("active");
}
});
$('#puzzlecontainer').on('vmouseup','.block', function(){
globalvars.ismousedown = false;
});
vmouseover works as desired on a desktop browser. vmouseover可在桌面浏览器上按需工作。 But it is not working on my Android phone and tablet. 但这在我的Android手机和平板电脑上不起作用。 Please help. 请帮忙。
Fire a function to add and remove an event to be fired on mouseenter. 触发函数以添加和删除要在mouseenter上触发的事件。 Add this event when the mouse is down and remove it when mouse is up to stop highlighting... 在鼠标按下时添加此事件,在鼠标按下时将其删除以停止突出显示...
$('#puzzlecontainer').on('mousedown','.block',startSelect);// Run when down
$('#puzzlecontainer').on('mouseup','.block',stopSelect);// Run when up
function startSelect(){
$('#puzzlecontainer').on('mouseenter','.block',myHandler);//Add Handler on enter.
// Fire the mouseenter event for the current element or it will not highlight.
$(this).trigger('mouseenter');
}
function stopSelect(){
$('#puzzlecontainer').off('mouseenter','.block',myHandler);//Remove Handler because mouseup.
}
You can use mouseover
event on each cells and set a flag true in myHandler
. 您可以在每个单元格上使用mouseover
事件,并在myHandler
标志设置为true。 check if it is true and then treat it as mousedown
event 检查是否为真,然后将其视为mousedown
事件
var ismosedown = false;
$('#puzzlecontainer').on('mousedown','.block', function(){ ismosedown =true; } );
$('allcells').mouseover(function(){
if(ismosedown ){ // do works
}
}).mouseup(function(){
ismosedown = false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.