简体   繁体   English

在滚动条上显示父节时对div进行动画处理

[英]Animate div when parent section is visible on scroll

I had this working but seem to have messed it up along the way. 我已经完成了这项工作,但似乎一路搞砸了。

I am using a scroll highjack that will bring the user to a new section/card each time they scroll. 我使用的是滚动高手,每次滚动时都会将用户带到新的部分/卡片。

It is adding a visible class each time the user scrolls to a new section/card. 每次用户滚动到新的部分/卡片时,它都会添加一个可见的类。

I used this as the base https://codyhouse.co/gem/page-scroll-effects 我以此为基础https://codyhouse.co/gem/page-scroll-effects

<section class="cd-section visible">
  <div>
    <h2>Page Scroll Effects</h2>
  </div>
</section>

Then when the user scrolls to a new section it removes and adds the visible to the next section. 然后,当用户滚动到新的部分时,它将删除并将可见的内容添加到下一个部分。

I am animating basic content at the moment like hero text etc for each section. 目前,我正在为每个部分设置基本内容的动画,例如英雄文字等。

<section class="cd-section visible">
  <div class="home__content-slide-right">
    <h2>Page Scroll Effects</h2>
  </div>
</section>

I am using the class name; 我正在使用班级名称; home__content-slide-right here to animate this text using transform for now, which you can see below; 这里的home__content-slide-right可以使用以下转换为文本设置动画,您可以在下面看到;

.home__content-slide-right {
   transform: translateX(-50px);
}

How I was doing it was buy just appending the .visible to the CSS which you can see below; 我的操作方式是购买,只需将.visible附加到CSS(您可以在下面看到);

.visible .home__content-slide-right {
  transform: translateX(0);
}

This was working so when I scrolled to each page the animation played but now it seems to only work once when the whole page is loaded and that's it. 这样做是可行的,所以当我滚动到播放的每个页面时,动画都可以播放,但是现在似乎只有在加载整个页面时才可以运行一次。

I have tried to remove somethings but have had no luck so far, just wondering here if anyone else had a reason for it not working. 我曾经尝试删除一些东西,但到目前为止还没有运气,只是想知道这里是否有人有理由不工作。

------ Edit ------ ------ 编辑 ------

I have added a few images below so you can see what my issue is. 我在下面添加了一些图像,以便您可以了解我的问题。

This first image is with the section having the .visible class so the animation should be played. 该第一张图片的节带有.visible类,因此应播放动画。

在此处输入图片说明

Though as you can see when I leave the section and the .visible class is removed the css stays the same. 虽然如您所见,当我离开本节并删除 .visible类时,css保持不变。

在此处输入图片说明

It looks like you have the "visible" class being applied to a parent element of your target section. 看起来您已将“ visible”类应用于目标部分的父元素。

Your css: 您的CSS:

.visible .home__content-slide-right {
  transform: translateX(0);
}

is written so that any parent of that element with a .visible class will apply this css rule. 进行编写,以便具有.visible类的该元素的任何父级都将应用此CSS规则。 If you want to ensure that this fires only when visible is added to the same section, re-write the css like this: 如果要确保仅在将可见部分添加到同一部分时才触发此事件,请像这样重新编写CSS:

section.visible .home__content-slide-right {
  transform: translateX(0);
}

Or make sure that no parent element has the visible class applied if it is unnecessary. 或者,如果不需要,请确保没有父元素应用可见类。

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

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