简体   繁体   English

将元素固定在顶部的特定偏移处,然后在滚动1000px后释放

[英]Hold element in place at specific offset to top then release after 1000px scroll

I'm trying to do something that I think has to do with parallax or sticky libraries but having a bunch of trouble. 我正在尝试做一些我认为与视差或粘性库有关的操作,但遇到了很多麻烦。

I'd like to let the user scroll normally, and then when a particular div is a particular value (lets say 232px) from the top of the screen, I want to hold the element in place (ie give it position:fixed). 我想让用户正常滚动,然后当特定的div是屏幕顶部的特定值(例如232px)时,我想将元素固定在适当的位置(即,将其位置固定)。 This part is easy to get the offset: 这部分很容易获得偏移量:

$('element')product1[0].getBoundingClientRect().top;

Then, let the user continue to scroll with the element in place so they can see the background continue to move with the scroll (I have a large height background with a nice linear-gradient). 然后,让用户继续在适当位置滚动元素,以便他们可以看到背景继续随着滚动而移动(我的背景高度很高,线性渐变也不错)。

Then after the user scrolled for about 1000px I want to release the element and let it continue to move up with the scroll. 然后,在用户滚动大约1000像素之后,我要释放该元素,并使其继续随着滚动向上移动。 The same should happen when the user scrolls back up - it holds for a bit then releases. 当用户向后滚动时,也会发生同样的情况-按住并释放。

I've tried a bunch of different things and combinations of libraries, pure css, JS, etc. Nothing works well. 我尝试了很多不同的东西,以及库,纯css,JS等的组合。没有什么效果很好。 Everything has an issue. 一切都有问题。 Would love to see a clean solution. 希望看到一个干净的解决方案。

Things I've tried: -JQuery sticky libraries (could not find one that does or is customizable to what I need) -Vanilla JS tracking position (had jumping issues on release - element would fly far away on release, not continue from release position) -CSS, specifically translateZ 我尝试过的事情:-jQuery粘性库(找不到能满足我需要的库或可根据我的需要进行自定义的库)-香草JS跟踪位置(在发布时发生了跳转问题-元素在发布时会飞得很远,而不是从发布位置继续)-CSS,特别是

The ideal setup is: 理想的设置是:

<div style="height: 230vh; background: linear-gradient(coolOne)">
   <div class="containerToStickTemporarily">
      <p class="textOnLeft">Hi</p>
      <img src="myImage.png"/>
   </div>
</div>

And I'd like to have multiple of these in a row. 我想连续多个。

Something like that.. I guess 这样的东西..我猜

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

     $('element').css({ position: top > 230 && top < 1000 ? 'fixed' : 'relative' });
})

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

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