简体   繁体   English

jQuery-元素穿越另一个元素

[英]jQuery - element crossing another element

I have a fixed div on the page which contains a logo and as the user scrolls and this logo passes over other divs I wnat to the change the colour of the logo. 我在页面上有一个固定的div,其中包含一个徽标,并且随着用户滚动,该徽标会越过我拥有的其他div,以更改徽标的颜色。

I have this working over a single div but need to it work across multiple so any help appreciated. 我在单个div上工作,但需要在多个div上工作,所以需要您的任何帮助。

The WIP site can be seen here... dd.mintfresh.co.uk - if you scroll down you'll (hopefully) see the logo change from black to white as it crosses an illustrated egg. 可以在此处看到WIP网站... dd.mintfresh.co.uk-如果向下滚动,您将(希望)看到徽标穿过黑色的鸡蛋时,徽标从黑色变为白色。 I need the same to happen when it crosses other divs further down the page. 当它越过页面的其他div时,我需要进行同样的操作。

The script so far... 到目前为止的脚本...

jQuery(window).scroll(function(){  

  var fixed = jQuery("logo");
  var fixed_position = jQuery("#logo").offset().top;  
  var fixed_height = jQuery("#logo").height();     

  var toCross_position = jQuery("#egg").offset().top;  
  var toCross_height = jQuery("#egg").height();

  if (fixed_position + fixed_height  < toCross_position) { 
    jQuery("#logo img").css({filter : "invert(100%)"}); 
  } else if (fixed_position > toCross_position + toCross_height) {  
    jQuery("#logo img").css({filter : "invert(100%)"});
  } else {    
    jQuery("#logo img").css({filter : "invert(0%)"});  
  }
}
);

Any help appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

you need to fire a div scroll event. 您需要触发div滚动事件。 you can assign 你可以分配

           $("div1").scroll(function(){ 
                   //change the color of the div1
                  } 
              }); 

           $("div2").scroll(function(){ 
                //change the color of the div2
              } 
               });

or you can assign a class to divs which you want to change the color 或者您可以为要更改颜色的div分配一个类

          $(".div").scroll(function(){ 
                //change the color of the div which you are scrolling now
                   } 
                   });

You can use like this :- 您可以这样使用:-

 $(window).scroll(function() { var that = $(this); $('.section').each(function() { var s = $(this); if (that.scrollTop() >= s.position().top) { if(s.hasClass('active')) { $('.logo').addClass('invert'); } else { $('.logo').removeClass('invert'); } } }); }); 
 body { padding: 0; margin: 0; } div { background: #f00; height: 400px; } .logo { position: fixed; top: 0; left: 0; width: 100px; } .logo.invert { filter: invert(100%); } div:nth-child(even) { background: #ff0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src="https://dd.mintfresh.co.uk/wp-content/uploads/2018/06/DD_logo.svg" class="logo" /> <div id="page1" class="section"></div> <div id="page2" class="section active"></div> <div id="page3" class="section"></div> <div id="page4" class="section active"></div> <div id="page5" class="section"></div> 

As your site code you can do like this : 作为您的站点代码,您可以这样:

$(window).scroll(function() {
   var that = $(this);
   $('#content > section').each(function() {
    var s = $(this);
    if (that.scrollTop() >= s.position().top) {
      if(s.hasClass('black')) {
        $('#logo img').css({filter: 'invert(0%)'});
      } else {
        $('#logo img').css({filter: 'invert(100%)'});
      }
      }
   });    
});

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

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