javascript - 滚动到顶部按钮不会淡出

标签 javascript jquery html css

我无法使用滚动到顶部按钮。我知道这是另一个动画的问题,但我看不出是什么导致了这个问题。问题是当另一个动画“在滚动达到 500 时开始”时,滚动到顶部按钮将不再淡出和消失。

$.chain = function() {
    var promise = $.Deferred().resolve().promise();
    jQuery.each( arguments, function() {
      promise = promise.pipe( this );
    });
    return promise;
  };  

  function scrollTop(){
    if(typeof pageYOffset!= 'undefined'){
      return pageYOffset;
    }
    else{
      var b = document.body; //IE 'quirks'
      var d = document.documentElement; //IE with doctype
      d = (d.clientHeight)? d : b;
      return d.scrollTop;
    }
  }    

  $(window).on("scroll", function(){
    if(scrollTop() >= 600){
    $(window).off("scroll");
      var animations = $.chain(function() {
        return $('#animate1 img').fadeIn('slow').delay(400);
      }, function() {
        return $('#animate2 img').fadeIn('slow').delay(400);
      },  function() {
        return $('#animate3 img').fadeIn('slow');
      });
    };
});

jQuery(document).ready(function() {
var offset = 300;
var duration = 500;
jQuery(window).scroll(function() {
    if (jQuery(this).scrollTop() > offset) {
        jQuery('.scroll-top').fadeIn(duration);
    } else {
        jQuery('.scroll-top').fadeOut(duration);
    }
});
});

感谢您的帮助。

最佳答案

$(window).off("scroll"); 确实删除了您正在收听 fadeIn 和 fadeOut 的 scroll 事件。

虽然我还没有尝试过,但另一种方法是使用 event namespacing

类似下面的内容:

$.chain = function() {
    var promise = $.Deferred().resolve().promise();
    jQuery.each( arguments, function() {
      promise = promise.pipe( this );
    });
    return promise;
  };  

  function scrollTop(){
    if(typeof pageYOffset!= 'undefined'){
      return pageYOffset;
    }
    else{
      var b = document.body; //IE 'quirks'
      var d = document.documentElement; //IE with doctype
      d = (d.clientHeight)? d : b;
      return d.scrollTop;
    }
  }    

  $(window).on("scroll.foranimations", function(){ // event name
    if(scrollTop() >= 600){
    $(window).off("scroll.foranimations"); // event name
      var animations = $.chain(function() {
        return $('#animate1 img').fadeIn('slow').delay(400);
      }, function() {
        return $('#animate2 img').fadeIn('slow').delay(400);
      },  function() {
        return $('#animate3 img').fadeIn('slow');
      });
    };
});

jQuery(document).ready(function() {
    var offset = 300;
    var duration = 500;
    jQuery(window).on("scroll.forfading", function() { // event name
        if (jQuery(this).scrollTop() > offset) {
            jQuery('.scroll-top').fadeIn(duration);
        } else {
            jQuery('.scroll-top').fadeOut(duration);
        }
    });
});

注意不同的事件名称 scroll.foranimationsscroll.forfading

关于javascript - 滚动到顶部按钮不会淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21712996/

相关文章:

javascript - 如何修复 "My first scan value is not same as my second scan value and the value scan in HTML is not same as value scan in notepad?"

html - CSS 和 Flexbox 布局问题

AngularJS 高度均衡器隐藏的 div

jquery - 媒体查询在不同尺寸的屏幕上效果不佳

javascript - 在 Visual Studio 中使用 javascript

javascript - 有没有办法异步进行跨域javascript调用?

javascript - 如何根据值选择和检查这些类型的复选框?

javascript - jquery 选择器中包含+startswith

javascript - 需要在其他 div 悬停时显示一个 div

javascript - 如果子 div 包含特定数据属性,则删除父 div