Javascript 如何在Jquery中调用.off()后重置事件处理程序

Javascript 如何在Jquery中调用.off()后重置事件处理程序,javascript,jquery,mousewheel,Javascript,Jquery,Mousewheel,为什么我的第二个身体不工作?我添加了.off(),因为如果不是两个mousewheel事件都只触发了一个mousewheel down事件。。。第二个body.on()不是应该重置.off()吗?我应该如何编程 $(document).ready(function() { $("body").on('mousewheel', function(event, delta) { if (event.deltaY < 0) { if (!$(".both").hasCla

为什么我的第二个身体不工作?我添加了.off(),因为如果不是两个mousewheel事件都只触发了一个mousewheel down事件。。。第二个body.on()不是应该重置.off()吗?我应该如何编程

$(document).ready(function() {
  $("body").on('mousewheel', function(event, delta) {
    if (event.deltaY < 0) {
      if (!$(".both").hasClass('rotated')) {
        $(".both").css('transform', 'rotate(180deg)');
        setTimeout(function() { $(".both").addClass('rotated') }, 1000);
      } 
    }
    $("body").off();
  });
  $("body").on('mousewheel', function(event, delta) {
    if (event.deltaY < 0) {
      if ($(".both").hasClass('rotated')) {
        alert("a");
      }
    }
  });
});
$(文档).ready(函数(){
$(“body”)。在('mousewheel',函数(事件,增量){
if(event.deltaY<0){
if(!$(“.both”).hasClass('rotated')){
$(“.both”).css('transform','rotate(180度));
setTimeout(function(){$(“.both”).addClass('rotated')},1000);
} 
}
$(“正文”).off();
});
$(“body”)。在('mousewheel',函数(事件,增量){
if(event.deltaY<0){
if($(“.both”).hasClass('rotated')){
警报(“a”);
}
}
});
});

我为我的问题添加了一个有效的解决方案,以防有人需要,这一切都要感谢选择的答案

$(document).ready(function() {

  function handleWheel(event) {
    if (event.deltaY < 0) {
      if (!$(".both").hasClass('rotated')) {
        $(".both").css('transform', 'rotate(180deg)');
        setTimeout(function() { $(".both").addClass('rotated') }, 1000);
      }
    }
    // disengage just this event handler and no others
    $("body").off('mousewheel', handleWheel);
  };
  function handleWheelNoche(event) {
    if (event.deltaY < 0) {
      if ($(".both").hasClass('rotated')) {
        setTimeout(function() { $(".black").addClass('noche') }, 1000);
      }
    }
  };
  $("body").on('mousewheel', handleWheel);
  $("body").on('mousewheel', handleWheelNoche);
});
$(文档).ready(函数(){
功能手柄轮(事件){
if(event.deltaY<0){
if(!$(“.both”).hasClass('rotated')){
$(“.both”).css('transform','rotate(180度));
setTimeout(function(){$(“.both”).addClass('rotated')},1000);
}
}
//仅断开此事件处理程序,而不断开其他事件处理程序
$(“body”).off(“鼠标轮”,手柄轮);
};
函数handleWheelNoche(事件){
if(event.deltaY<0){
if($(“.both”).hasClass('rotated')){
setTimeout(function(){$(“.black”).addClass('noche')},1000);
}
}
};
$(“主体”)。在(‘鼠标轮’、手柄轮)上;
$(“主体”)。在(‘鼠标轮’、把手上;
});

您的代码在
主体
对象上注册两个
鼠标滚轮
事件处理程序。当发生
mouseweel
事件并调用事件处理程序回调函数时,第一个事件处理程序随后调用
$(“body”).off()
取消注册这两个事件处理程序,这样以后就不会得到任何事件

此时,
body
对象上不再有任何事件处理程序

如果希望只调用一次事件处理程序,则可以使用
.one()
。除此之外,不清楚为什么您有两个独立的事件处理程序,因此不清楚还有什么建议

通常,没有理由对同一事件使用两个单独的事件处理程序。无论您想做什么工作,您都可以在单个事件处理程序中执行该工作。如果您只想在某些事件期间执行某些工作,那么只需在单个事件处理程序中实现逻辑,以决定在调用事件处理程序时执行哪些工作(使用
If
语句等)

如果您只想注销其中一个事件处理程序,则必须将命名函数与事件处理程序
$('body')一起使用
这样您就可以调用
$('body').off('mousewheel',funcName)
来注销特定的事件处理程序


使用命名函数的工作原理如下:

$(document).ready(function() {

  function handleWheel(event) {
    if (event.deltaY < 0) {
      if (!$(".both").hasClass('rotated')) {
        $(".both").css('transform', 'rotate(180deg)');
        setTimeout(function() { $(".both").addClass('rotated') }, 1000);
      } 
    }
    // disengage just this event handler and no others
    $("body").off('mousewheel', handleWheel);
  }

  $("body").on('mousewheel', handleWheel);

});
$(文档).ready(函数(){
功能手柄轮(事件){
if(event.deltaY<0){
if(!$(“.both”).hasClass('rotated')){
$(“.both”).css('transform','rotate(180度));
setTimeout(function(){$(“.both”).addClass('rotated')},1000);
} 
}
//仅断开此事件处理程序,而不断开其他事件处理程序
$(“body”).off(“鼠标轮”,手柄轮);
}
$(“主体”)。在(‘鼠标轮’、手柄轮)上;
});

您的代码在
主体
对象上注册两个
鼠标滚轮
事件处理程序。当发生
mouseweel
事件并调用事件处理程序回调函数时,第一个事件处理程序随后调用
$(“body”).off()
取消注册这两个事件处理程序,这样以后就不会得到任何事件

此时,
body
对象上不再有任何事件处理程序

如果希望只调用一次事件处理程序,则可以使用
.one()
。除此之外,不清楚为什么您有两个独立的事件处理程序,因此不清楚还有什么建议

通常,没有理由对同一事件使用两个单独的事件处理程序。无论您想做什么工作,您都可以在单个事件处理程序中执行该工作。如果您只想在某些事件期间执行某些工作,那么只需在单个事件处理程序中实现逻辑,以决定在调用事件处理程序时执行哪些工作(使用
If
语句等)

如果您只想注销其中一个事件处理程序,则必须将命名函数与事件处理程序
$('body')一起使用
这样您就可以调用
$('body').off('mousewheel',funcName)
来注销特定的事件处理程序


使用命名函数的工作原理如下:

$(document).ready(function() {

  function handleWheel(event) {
    if (event.deltaY < 0) {
      if (!$(".both").hasClass('rotated')) {
        $(".both").css('transform', 'rotate(180deg)');
        setTimeout(function() { $(".both").addClass('rotated') }, 1000);
      } 
    }
    // disengage just this event handler and no others
    $("body").off('mousewheel', handleWheel);
  }

  $("body").on('mousewheel', handleWheel);

});
$(文档).ready(函数(){
功能手柄轮(事件){
if(event.deltaY<0){
if(!$(“.both”).hasClass('rotated')){
$(“.both”).css('transform','rotate(180度));
setTimeout(function(){$(“.both”).addClass('rotated')},1000);
} 
}
//仅断开此事件处理程序,而不断开其他事件处理程序
$(“body”).off(“鼠标轮”,手柄轮);
}
$(“主体”)。在(‘鼠标轮’、手柄轮)上;
});

你的问题不清楚。如果你不想让“鼠标滚轮”同时运行,为什么要为“鼠标滚轮”添加两个处理程序?我没有找到在同一个主体内运行的方法。on()考虑到我必须执行主体。off()停止执行下一步你的问题不清楚。如果不想让“鼠标滚轮”同时运行,为什么要为“鼠标滚轮”添加两个处理程序?我没有找到在同一个主体内运行的方法。on()考虑到我必须运行body.off()要停止ExecutionGIF的下一步,我想使用这样一个命名函数,如何将事件和增量参数传递给要分析鼠标的函数