Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据当前鼠标移动位置,仅触发一次事件_Javascript_Jquery - Fatal编程技术网

Javascript 根据当前鼠标移动位置,仅触发一次事件

Javascript 根据当前鼠标移动位置,仅触发一次事件,javascript,jquery,Javascript,Jquery,如何在下面的代码片段中显示的每个条件/情况下仅触发一次事件 在我的示例中,如果鼠标位置的x位置值在25-50之间, 如果y位置值为0-25,它将触发一个事件。 但是,如果看到控制台,事件会重复激发 如何在每个条件下只记录一次控制台(如果以后添加,则记录多个事件)?如果鼠标离开“条件”,稍后返回,console.log会再次启动吗 $(文档).mousemove(函数(getCurrentPos){ var xCord=getCurrentPos.pageX; var yCord=getCurr

如何在下面的代码片段中显示的每个条件/情况下仅触发一次事件

在我的示例中,如果鼠标位置的x位置值在25-50之间, 如果y位置值为0-25,它将触发一个事件。 但是,如果看到控制台,事件会重复激发

如何在每个条件下只记录一次控制台(如果以后添加,则记录多个事件)?如果鼠标离开“条件”,稍后返回,console.log会再次启动吗

$(文档).mousemove(函数(getCurrentPos){
var xCord=getCurrentPos.pageX;
var yCord=getCurrentPos.pageY;
xPercent=xCord/$(document).width()*100;
yPercent=yCord/$(document).height()*100;
如果((xPercent>0&&xPercent<25)&&(yPercent>=0&&yPercent<25)){
console.log('1');/*如何只发射一次*/
/*稍后还有更多活动*/
}否则如果((X百分比>=25&&X百分比0&&Y百分比<25)){
console.log('2');/*如何只发射一次*/
/*稍后还有更多活动*/
}否则如果((X百分比>=50&&X百分比0&&Y百分比<25)){
console.log('3');/*如何只发射一次*/
/*稍后还有更多活动*/
} 
});

使用在
mousemove
事件之外定义的变量,如
entered

var输入=false;
$(文档).mousemove(函数(getCurrentPos){
var xCord=getCurrentPos.pageX;
var yCord=getCurrentPos.pageY;
var xPercent=xCord/$(document).width()*100;
var yPercent=yCord/$(document).height()*100;
如果(输入){
return;//如果已输入一次,则跳过其余代码
}
如果((xPercent>0&&xPercent<25)&&(yPercent=25&&xPercent 0&&yPercent<25)){
输入=真;
console.log('2');
/*如何只开火一次*/
/*稍后还有更多活动*/
}否则如果((X百分比>=50&&X百分比0&&Y百分比<25)){
输入=真;
console.log('3');
/*如何只开火一次*/
/*稍后还有更多活动*/
} 
});

您可以使用数组来记录已触发的事件。这样,您可以使用任意数量的事件。一旦触发事件,它将被记录到阵列中,下次我们检查是否已经运行该事件,如果已经运行,我们将忽略它

        <script>
        var logged = [];
        $(document).mousemove(function(getCurrentPos) {
            var xCord = getCurrentPos.pageX;
            var yCord = getCurrentPos.pageY;
            xPercent = xCord / $(document).width() * 100;
            yPercent = yCord / $(document).height() * 100;

            if ((xPercent > 0 && xPercent < 25) && (yPercent <= 0 && yPercent < 25) && ($.inArray(1, logged) == -1)) {
                logged.push(1);
                console.log('1'); /* how to fire only once?*/
                /* more events here later */
            } else if ((xPercent >= 25 && xPercent <= 50) && (yPercent > 0 && yPercent < 25) && ($.inArray(2, logged) == -1)) {
                logged.push(2);
                console.log('2'); /* how to fire only once?*/
                /* more events here later */

            } else if ((xPercent >= 50 && xPercent <= 75) && (yPercent > 0 && yPercent < 25) && ($.inArray(3, logged) == -1)) {
                logged.push(3);
                console.log('3');/* how to fire only once?*/
                /* more events here later */
            }
        });
    </script>

记录的风险值=[];
$(文档).mousemove(函数(getCurrentPos){
var xCord=getCurrentPos.pageX;
var yCord=getCurrentPos.pageY;
xPercent=xCord/$(document).width()*100;
yPercent=yCord/$(document).height()*100;
如果((xPercent>0&&xPercent<25)&&(yPercent=25&&xPercent 0&&yPercent<25)&&($.inArray(2,记录)=-1)){
记录。推送(2);
console.log('2');/*如何只发射一次*/
/*稍后还有更多活动*/
}如果((xPercent>=50&&xPercent 0&&yPercent<25)和($.inArray(3,记录)=-1)){
记录。推送(3);
console.log('3');/*如何只发射一次*/
/*稍后还有更多活动*/
}
});

给你!您只需避免为每个条件设置一些唯一的值,并在输入时再次检查它。。。如果没有,请重置它

var currentArea=false;
$(文档).mousemove(函数(getCurrentPos){
var xCord=getCurrentPos.pageX;
var yCord=getCurrentPos.pageY;
xPercent=xCord/$(document).width()*100;
yPercent=yCord/$(document).height()*100;
如果((xPercent>0&&xPercent<25)&&(yPercent=25&&xPercent 0&&yPercent<25)){
如果(当前区域!=“区域2”){
console.log('2');
currentArea=“区域2”;
//这样做
}
}否则如果((X百分比>=50&&X百分比0&&Y百分比<25)){
如果(当前区域!=“区域3”){
console.log('3');
currentArea=“区域3”;
//这样做
}
}否则{
console.log(“外部”)
currentArea=false
}

});
每个事件只发生一次
应该是每个条件下发生一次
我认为这样会更清晰。。您可以让一个变量将其设置为true,并在条件内将其设置为false。。将其添加到条件中。但如果你有很多情况,我不认为这是一个选项检查这一个和两个将是控制台一次,但这是使用变量嗯,它似乎不工作的mousemove?只有当我在conditionsah中刷新鼠标位置时才看到console.log,但当我的鼠标离开“conditions”并返回时,它不会再次“再次”触发?@wsgg当然不会导致标题说:“根据鼠标位置仅触发一次事件”但也因为我认为这可能是一个问题,我链接了jQuery mouseenter,如果你遵循这个例子,用我的答案,你应该能够解决所有问题,也许我应该用更好的措辞。我的错。事实上,一旦条件再次满足,我希望能再次拥有它。@wsgg nice title now:)不过我没有更多的时间了