简体   繁体   English

jQuery-如何在滚动时淡入和淡出元素(div)

[英]jquery - How to fade in and fading out back an element (div) as we scroll

I am so new to Jquery and Javascript. 我对Jquery和Javascript很陌生。 I know there are bunch of exact same post or question but non of it works or I'm just to blind to use it. 我知道有一堆完全相同的帖子或问题,但都不起作用,或者我只是盲目使用。

My aim is to fade in a div as we scroll down, as we scroll down at a certain point I want to fade out back the div and another div fade in. Of cause there is no problem to fade in but to fadeout back is a problem. 我的目标是在向下滚动时在div中淡入,在某个点向下滚动时,我要向后淡出div,而另一个div逐渐淡入。当然,淡入是没有问题的,但是淡出是问题。 There are 6 sequence of div that i want to apply. 我要应用6个div序列。 The first div works fine as I scroll down on 2100 i want to fade it out and the next div will fade in which work pretty well. 当我向下滚动2100时,第一个div效果很好,我想将其淡出,而下一个div则将淡入效果,效果很好。

the sequence div id is #header1 and so on and I set the #header2 to #header6 display to none in CSS. 序列div ID是#header1,依此类推,我在CSS中将#header2设置为#header6 display。 Is there any way to make it fade in just on the range of the windows or something. 有什么方法可以使其仅在窗户范围内褪色。 thanks a lot 非常感谢

here is some of the script that i try but it doesn't work 这是我尝试的一些脚本,但是它不起作用

$(window).bind('scroll', function() {
 if ($(window).scrollTop() > 2100) {
     $('#header1').fadeOut();
 }
 else {
     $('#header1').fadeIn();
 } });

$(window).bind('scroll', function() {
 if ($(window).scrollTop() > 2100) {
     $('#header2').fadeIn();
 }
 else if ($(window).scrollTop() > 3500) {
     $('#header2').fadeOut();
 }
 else {
     $('#header2').fadeOut();
 } });

$(window).bind('scroll', function() {
 if ($(window).scrollTop() > 3500) {
     $('#header3').fadeIn();
 }
 else {
     $('#header3').fadeOut();
 } });

我认为, 如果我对这种方式有误可以执行以下操作来纠正我 ,而无需计算所需的.scrollTop()。

if ($(window).scrollTop() > $( "#header" ).scrollTop())

the reason is this: 原因是这样的:

just picture the scroll even just fired , and you are past 3500 , 只是想像一下卷轴,甚至刚刚发射,您已经超过3500岁了,

$('#header1').fadeOut();
 $('#header2').fadeIn();
$('#header2').fadeOut();
 $('#header3').fadeIn();

all execute at the exact same time , is that intended? 所有人都在同一时间执行,这是故意的吗? , you should have conditions checking if the scroll position is between 2 numbers ,您应该有条件检查滚动位置是否在2个数字之间

if your 2100 > scrollTop < 3500 then: 如果您的2100> scrollTop <3500,则:

 $('#header1').fadeOut();
 $('#header2').fadeIn();
  $('#header2').fadeOut();
   $('#header3').fadeOut();

all fire at the exact same time , and if you're below 2100 then: 全部在同一时间触发,如果您的年龄低于2100,则:

   $('#header1').fadeIn();
  $('#header2').fadeOut();
    $('#header3').fadeOut();

all execute at the same time 全部同时执行

if ($(window).scrollTop() > 2100 <3500) { $('#header2').fadeIn(); }

will not work. 不管用。 You haven't written the if statement correctly. 您尚未正确编写if语句。 You should be using code looking something like this: 您应该使用看起来像这样的代码:

$( document ).ready(function(){
if ( scrollTop > (2100) && scrollTop < (3500) ){
     $('#header1').fadeIn('slow');
     $('#header2').fadeout('slow');
     $('#header3').fadeOut('slow');
 }else{
     $('#header1').fadeOut('slow');
     $('#header2').fadeIn('slow');
     $('#header3').fadeIn('slow');
   }
});

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

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