如何在点击后禁用JavaScript功能?

4

我有一个页面,当你滚动它时,对象(容器...)变得可见。 同时我还有项目。所以当你点击其中一个时,我的页面会向上滚动。我想禁用之前提到的那个功能(显示出来,而不是禁用滚动!)。

$(document).ready(function(){
var offsetActivity = (1);
var wScroll = ($(window).scrollTop());
var wHeight = ($(window).innerHeight());
var thirdHeight = (wHeight/1.3);
$('.secondic > .row > a > .one-half > p').click(function(offsetActivity){
  var offsetActivity = (0);
  var projBack = $(this).parent().css('background-image');
  var parent = $(this).parent().attr('id');
  var textAbout = $('#'+ parent +' > p.about-photo').text();
$('header').css('background-image', ''+ projBack +'');
$('header > .headerCont > h5').replaceWith('<h5>'+ textAbout +'</h5>');
    setTimeout(function(){
      $('.container').removeClass('offset-done');
      $('.container').children().removeClass('offset-done');
    }, 2000);
});
if(offsetActivity = (1)){
$(window).scroll(
  function(){
    var wScroll = ($(window).scrollTop());
    var wHeight = ($(window).innerHeight());
    var thirdHeight = (wHeight/1.3);
    console.log(wHeight, wScroll, thirdHeight);
      if(wScroll > (($('.container').offset().top)-thirdHeight)){
        $('.container').addClass('offset-done');
      }
      if(wScroll > (($('.window').offset().top)-thirdHeight)){
        $('.window').addClass('offset-done');
      }
  });
});

如何在点击我的 P 键时禁用该功能?并且我希望在关闭项目后能够再次启用它。


1
你能否请更清楚地解释一下,但如果你想禁用事件,$(window).off('scroll')将会在jQuery中禁用事件处理程序。 - Omidam81
关闭项目后,我想重新打开它! - Godje
请定义函数并在项目关闭后再次分配它。 - Omidam81
3个回答

3
您可以使用off()在jquery中取消事件处理程序。

.off()方法会删除使用.on()添加的事件处理程序。有关委派和直接绑定事件的讨论,请参见该页面。调用没有参数的.off()将删除附加到元素的所有处理程序。通过提供事件名称、命名空间、选择器或处理程序函数名称的组合,可以从元素上删除特定的事件处理程序。

来自jquery api:http://api.jquery.com/off/ 在您的情况下,您应该使用$(window).off('scroll');从窗口对象中删除滚动事件的事件处理程序。

2

在你的 click 函数中,使用 $window.off('scroll');$window.unbind('scroll');


1
要使用jQuery函数.off,您需要首先使用.on激活。 $(window).on('scroll', 'body', function(){ ... }); 点击时的函数: $(window).off('scroll', 'body');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接