简体   繁体   English

使用javascript在700px之后显示div后,如何隐藏它?

[英]after using javascript to show a div after 700px how do i hide it?

I have a div in a wordpress site, which is set via css to display: none; 我在wordpress网站中有一个div,该站点通过CSS设置为显示:无; on page load. 页面加载。

I then use: 然后,我使用:

$(window).scroll(function(){
  if($(window).scrollTop()>700){
     $("#logo-scroll").fadeIn();
  }
});

to fade it in after 700px. 在700px后淡入 I cannot work out how to hide it again after 2200px. 2200px之后,我无法解决如何再次隐藏它。

I have tried: 我努力了:

$(window).scroll(function(){
  if($(window).scrollTop()>700){
     $("#logo-scroll").fadeIn();
  }
  else if($(window).scrollTop()>2200){
     $("#logo-scroll").fadeOut();
  }
});

but it shows it at the right time but doesnt effect the fade out at all. 但它会在正确的时间显示它,但完全不会影响淡出效果。

Any ideas? 有任何想法吗?

The problem is the first if statement is always evaluating to true after the window has been scrolled beyond 700px ... The second else if statement is never executed. 问题是,在窗口滚动到700px以上之后,第一个if语句始终评估为true ...第二个else if语句永远不会执行。 So you need to make it so it only evaluates to true between the 700px and 2200px scroll-positions. 因此,您需要进行设置,使其仅在700px和2200px滚动位置之间计算为true。

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

  if(scroll_position > 700 && scroll_position <= 2200){
    $("#logo-scroll").fadeIn();
  }
  else if(scroll_position > 2200){
    $("#logo-scroll").fadeOut();
  }
});

Just add && $(window).scrollTop() < 2200 to get: 只需添加&& $(window).scrollTop() < 2200即可:

$(window).scroll(function(){
  if($(window).scrollTop() > 700 && $(window).scrollTop() < 2200){
     $("#logo-scroll").fadeIn();
  }
  else if($(window).scrollTop()>2200){
     $("#logo-scroll").fadeOut();
  }
});

Your first if statement evaluates as true, even if you want to hide it, you immediatly show it again. 您的第一个if语句的评估结果为true,即使您要隐藏它,也要立即将其再次显示。

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

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