简体   繁体   English

如何使元素在特定点后停止滚动?

[英]How to make an element stop scrolling after an certain point?

I want to replicate a part of this page, if you scroll down a bit you see a picture of a graph and some text. 我要复制页面的一部分,如果向下滚动一点,您会看到图形图片和一些文本。 Does anyone know how they did the scrolling effect on the photo and text? 有谁知道他们是如何对照片和文字进行滚动效果的? And the text appears after a certain point, how did they do that? 文本出现在特定点之后,他们是如何做到的?

It sounds like there are two questions being asked here. 听起来好像这里有两个问题。 One being how to only display items once they are already within the viewport, and the other being how to apply a parallax effect. 一种是如何仅在项目已经在视口中时显示它们,另一种是如何应用视差效果。

For displaying items only once they are within viewport, ScrollReveal looks like a tool that would took the trick. 对于仅在项目进入视口后才显示项目的情况,ScrollReveal看起来像是一种可以骗人的工具。 You can check it out here: https://scrollrevealjs.org/ 您可以在这里查看: https : //scrollrevealjs.org/

As for the parallax effect, I made a super lightweight Javascript class that you can use. 至于视差效果,我制作了一个可以使用的超轻量级Javascript类。 Check out the source code here: 在此处查看源代码:

http://codepen.io/rodomeista/pen/vGqWXr http://codepen.io/rodomeista/pen/vGqWXr

There are a few things you will need to update in your code to get this working. 要使此功能正常运行,您需要在代码中进行一些更新。

  1. Copy all of Javascript code into your main JS file. 将所有Javascript代码复制到您的主JS文件中。
  2. Add the 'parallax-content' class to any items you wish to apply the effect to. 将“视差内容”类添加到要对其应用效果的任何项目。
  3. Specify the 'parallax-offset' and the 'parallax-axis' attributes on the DOM element in question. 在相关的DOM元素上指定“视差偏移”和“视差轴”属性。

Exampe: Exampe:

Let's say you were to have an image element that looks like so 假设您要拥有一个看起来像这样的图像元素

<img src = "./path/to/image.png" />

And I want to apply a subtle parallax effect along the y axis. 我想沿y轴应用微妙的视差效果。 All I would need to do is adjust attribute like so: 我需要做的只是调整属性,如下所示:

<img src = "./path/to/image.png" class = "parallax-content" data-parallax-offset = "-50" data-parallax-axis = "y" />

Now run your code and see it in action! 现在运行您的代码,看看它的作用! Feel free to play around with the data-parralax-offset number to get the speed right for your needs. 随意使用data-parralax-offset数字来获得满足您需求的速度。 I'm happy to answer any questions about the code. 我很高兴回答有关代码的任何问题。

Good luck! 祝好运!

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

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