简体   繁体   English

div元素应向下滚动,直到与另一个元素接触为止

[英]div element should scroll down till it gets in contact with another element

I've an div element #1 which stick on top the screen while scrolling. 我有一个div元素#1,它在滚动时停留在屏幕顶部。 Thats working so far. 到目前为止,多数民众赞成在工作。 Now further down on the page there is another element #2 and the element #1 should stop scrolling when it gets in contact with #2. 现在,在页面的更下方,还有另一个元素#2,并且元素#1与#2接触时应该停止滚动。 To illustrate the scenario I made a sick picture with my paint skills. 为了说明这种情况,我用自己的绘画技巧制作了一张病态的照片。

在此处输入图片说明

Do somebody have an idea how to do it? 有人知道怎么做吗?

$el1.offset().top + $el1.outerHeight();

this will give you the bottom position of first div. 这将使您处于第一格的最低位置。 Now When you scroll check if taht bottom position of first div is less than top position of second div. 现在,当滚动时,请检查第一格的底部位置是否小于第二格的顶部位置。 if it is less its ok but if it is greater than you can do whatever you want with first div. 如果它没问题,但如果它大于它,您可以在first div上做任何您想做的事情。 You can hide it on make its position absolute. 您可以隐藏它,使其处于绝对位置。

$(window).scroll(function(){
var bott = $el1.offset().top + $el1.outerHeight();
   if(bott > $el2.offset().top){
   $el1.css('display','none');
   // OR
   $el1.css('position','absolute');
  }
});

hope this helps 希望这可以帮助

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

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