简体   繁体   English

jQuery如何在单击另一个事件时禁用滚动事件

[英]jquery how to disable an scroll event on click of another event

this is the script I have in place to show a popup and then on click disable it. 这是我准备显示弹出窗口,然后单击禁用它的脚本。 The popup show when scrolling past a certain limit and then stays until the close image is clicked. 滚动超过某个限制后,弹出窗口将显示,然后一直停留到单击关闭图像为止。

<script>
$(document).ready(function(){

  $('#popupbackground').hide();
  $('#popup').hide();
  $('img#close').hide();

    $(window).scroll(function(){
      if($(document).scrollTop() > 325){
        $('#popupbackground').fadeIn(750);
        $('#popup').fadeIn(750);
        $('img#close').fadeIn(750);
      }
    });  
    $("img#close").click(function(){
        $("#popupbackground").hide();
        $("#popup").hide();
        $("img#close").hide();
    });
});
</script>

This works fine but the problem I have now is that when the close img is clicked and the popup disappears, it re-appears when the user scrolls again. 这工作正常,但我现在遇到的问题是,当单击关闭img时,弹出窗口消失了,当用户再次滚动时,它会重新出现。

What is an easy and lightweight way to disable the re-appearance of the popup? 有什么简单又轻巧的方法可以禁止弹出窗口的再次出现?

If you no longer need to keep track of scroll events after showing the popup, you should "unbind" your handler. 如果在显示弹出窗口后不再需要跟踪滚动事件,则应“解除绑定”处理程序。 For example: 例如:

$(document).ready(function(){

  $('#popupbackground').hide();
  $('#popup').hide();
  $('img#close').hide();

  function handleWindowScroll() {
    if($(document).scrollTop() > 325){
      $(window).off('scroll', handleWindowScroll);
      $('#popupbackground').fadeIn(750);
      $('#popup').fadeIn(750);
      $('img#close').fadeIn(750);
    }
  }
  $(window).on('scroll', handleWindowScroll);

  $("img#close").click(function(){
    $("#popupbackground").hide();
    $("#popup").hide();
    $("img#close").hide();
  });
});

Note that object.on('click', handler) is the same as object.click(handler) in jQuery. 请注意,object.on('click',handler)与jQuery中的object.click(handler)相同。

If you still need to keep track of the scroll event and the unbind solution won't work, you can use a variable instead: 如果仍然需要跟踪滚动事件,并且取消绑定解决方案不起作用,则可以改用变量:

$(document).ready(function(){
  var popupShown = false;

  $('#popupbackground').hide();
  $('#popup').hide();
  $('img#close').hide();

  $(window).scroll(function(){
    if(!popupShown && $(document).scrollTop() > 325){
      popupShown = true;
      $('#popupbackground').fadeIn(750);
      $('#popup').fadeIn(750);
      $('img#close').fadeIn(750);
    }
  });
  $("img#close").click(function(){
    $("#popupbackground").hide();
    $("#popup").hide();
    $("img#close").hide();
  });
});

you could do this in a number of ways: 您可以通过多种方式执行此操作:

1) set a variable that will stop the popup from firing 1)设置一个变量,该变量将阻止弹出窗口触发

<script>
$(document).ready(function(){

  $('#popupbackground').hide();
  $('#popup').hide();
  $('img#close').hide();
  var hasClosed = false;

    $(window).scroll(function(){
      if($(document).scrollTop() > 325 && !hasClosed){
        $('#popupbackground').fadeIn(750);
        $('#popup').fadeIn(750);
        $('img#close').fadeIn(750);
      }
    });  
    $("img#close").click(function(){
        hasClosed = true;
        $("#popupbackground").hide();
        $("#popup").hide();
        $("img#close").hide();
    });
});
</script>

2) remove the scroll trigger after the popup has been opened (preferrable) 2)在弹出窗口打开后删除滚动触发器(首选)

<script>
$(document).ready(function(){

  $('#popupbackground').hide();
  $('#popup').hide();
  $('img#close').hide();
  var openPopup = function(){
      if($(document).scrollTop() > 325){
        $('#popupbackground').fadeIn(750);
        $('#popup').fadeIn(750);
        $('img#close').fadeIn(750);
      }
  }

    $(window).on('scroll', openPopup );  
    $("img#close").click(function(){
        $(window).off('scroll', openPopup );
        $("#popupbackground").hide();
        $("#popup").hide();
        $("img#close").hide();
    });
});
</script>

The second one works best as it prevents jQuery from triggering the check against scrollTop() after its happened once. 第二个效果最好,因为它防止jQuery发生一次后触发对scrollTop()的检查。 Where as the first option will continue to check, but will just not fire because of the false boolean 其中,第一个选项将继续检查,但由于false布尔值而不会触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM