jQuery:将不透明度设置为0然后为fadeOut

时间:2016-01-09 13:31:23

标签: javascript jquery html css

我有一个函数设置,在你滚动时将元素的不透明度设置为0,我想要的是在完成此函数之后(不透明度为0),然后元素淡出(即你永远不会看到)它再次)。虽然这个功能有点儿笨拙而且过早消失,我有一个jsFiddle:http://jsfiddle.net/6hcm4qg4/

我的标记如下:

$(window).scroll(function() {
  var scrollTop = $(window).scrollTop();
  var height = $(window).height();



  var myEvent = function() {
    $('.logo_container, .slogan').css({
      'opacity': ((height - scrollTop) / height)
    });
  };
  $.when(myEvent()).done(function() {
    $('.logo_container, .slogan').fadeOut();
  });

});

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以使用jquery animate功能通过动画更改opacity。在动画函数中,如果不透明度达到0,则回调隐藏元素。

$(window).scroll(function() {
  var scrollTop = $(window).scrollTop();
  var height = $(window).height();

    $('.logo_container, .slogan').animate({
      'opacity': ((height - scrollTop) / height)
    }, function() {
       if (this.style.opacity <= 0)
           $(this).hide();
    });
});