简体   繁体   English

Javascript mousedown和mouseup在不同的元素上

[英]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 请帮忙

UPDATE 更新

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.

相关问题 Javascript mouseup和mousedown事件 - Javascript mouseup and mousedown event Javascript mousedown和mouseup事件冲突 - Javascript mousedown and mouseup event conflict 当mousedown和mouseup发生在不同的元素上时,如何处理D3拖动事件? - How to handle D3 drag events when mousedown and mouseup happen on different elements? 在javascript上进行mousedown和mouseup需要多长时间? - How long it take to doing mousedown and mouseup on javascript? 在js中使用mousedown / mouseup,无法更改元素的位置? - Using mousedown/mouseup in js, not able to change the position of elements? 使用标准javascript而不是jquery中的mousedown,mousemove和mouseup的触摸事件句柄 - Touch event handle with mousedown, mousemove and mouseup in standard javascript not jquery javascript如何在mousedown上连续移动元素并在mouseup上停止移动 - javascript how to continuously move element on mousedown and stop moving on mouseup JavaScript-在echo3框架中禁用mouseup或在mousedown上触发? - JavaScript - Disable mouseup or fire on mousedown in echo3 framework? Javascript mousedown、mouseup 和 mouseover 事件的奇怪和意外行为 - Strange and unexpected behaviour with Javascript mousedown, mouseup, and mouseover events mouseup 在 mousedown 后不会被触发 - mouseup is not fired after mousedown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM