简体   繁体   English

如何添加仅适用于特定 div 的滚动的 css animation 效果?

[英]How to add css animation effects that apply on scroll to only specific divs?

The idea is there are 5 sections named section1, section2 etc.这个想法是有 5 个部分,分别命名为 section1、section2 等。

Each section has its own image.每个部分都有自己的图像。

When the user scrolls, the image should 'fade out' and in by changing the opacity, and there is a slight zoom in/out effect using transform3D.当用户滚动时,图像应该通过改变不透明度来“淡出”和淡入,并且使用 transform3D 会有轻微的放大/缩小效果。

I'm trying to acheive results identical to this:我正在尝试获得与此相同的结果:

http://www.moxhe.com.au/ http://www.moxhe.com.au/

My attempt at starting something is this我开始做某事的尝试是这样的

  jQuery(document).ready(function() {
        jQuery(window).scroll(function(event) {
            let scroll = jQuery(this).scrollTop();
            let opacity = 1 - (scroll / 1000);
            var x = (scroll / 100);
            if (opacity >= 0) {
                jQuery('#section1').css({'opacity': opacity , 'transform': "scale(" + x + "," + x + ")"});
            }
        });
    });

Now, this does create the opacity changing on effect when you scroll - the issue is, it applies to the whole page on scroll, but i want the effects to happen only when you user scrolls in/around the div with ID named section1现在,这确实会在您滚动时创建不透明度更改效果 - 问题是,它适用于滚动时的整个页面,但我希望效果仅在您使用 ID 命名为 section1 的 div 中/周围滚动时发生

I know it;s applying to the whole window because the.scroll is being applied to jQuery(window).我知道它适用于整个 window,因为.scroll 被应用于 jQuery(window)。 I have tried using jQuery('#section1') but if i do this nothing happens.我曾尝试使用 jQuery('#section1') 但如果我这样做,什么也不会发生。

I have tried adding a transform3D effect like shown on http://www.moxhe.com.au/ but am struggling to get it work too, there should be a small decrease scrolling downwards and small increase scrolling upwards ONLY if user is scrolling around a specific div with the bg image.我已经尝试添加一个像http://www.moxhe.com.au/上所示的 transform3D 效果,但我也很难让它工作,只有当用户滚动时,向下滚动应该有一个小的减少和向上滚动的小增加带有 bg 图像的特定 div。

Long story short, i need to make css animations identical to http://www.moxhe.com.au/ but am struggling with applying the css effects to a specific part of the page / div with an ID as supposed to the whole page. Long story short, i need to make css animations identical to http://www.moxhe.com.au/ but am struggling with applying the css effects to a specific part of the page / div with an ID as supposed to the whole page .

I made a fiddle to understand where you are at.我做了一个小提琴来了解你在哪里。 I have wrapped the #section1 into #section1_container and applied overflow: hidden on it to restrict the place for the animation.我已将#section1包装到#section1_container中并在其上应用了overflow: hidden以限制 animation 的位置。

html html

<div id="section1_container">
<div id="section1">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloremque sapiente dolor ex magnam eveniet! Ipsum officia quidem possimus iste expedita temporibus nobis praesentiumconsectetur adipisicing elit. Doloremque sapiente dolor ex magnam eveniet! Ipsu
</div>
</div>

css css

#section1_container{
  overflow: hidden;
}

I think you also want to start animation where the section is visible or something.我想你也想在部分可见的地方启动 animation 。

so you have $("#section1_container").offset().top that will give you offset from the top of the document and if this property is higher than your scroll = jQuery(this).scrollTop();所以你有$("#section1_container").offset().top会给你从文档顶部的偏移量,如果这个属性高于你的scroll = jQuery(this).scrollTop(); then you can start animation.然后你可以启动 animation。 You can stop it by $("#section1_container").height() .您可以通过$("#section1_container").height()停止它。

You can try wrapping your transformation with a condition that only applies the transformation at specific scroll values:您可以尝试使用仅在特定滚动值处应用转换的条件来包装您的转换:

var sectionHeight = $('.section').height();
if(document.documentElement.scrollTop>0 && document.documentElement.scrollTop<sectionHeight){
    //apply transformation here
}
else if(document.documentElement.scrollTop>sectionHeight && document.documentElement.scrollTop<(sectionHeight*2)){
    //apply transformation here
}
...
//Number of if conditions = number of sections you have

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

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