从右到左淡化背景

Fade background from right to left

我在 ScrollMagic 库的帮助下设法根据滚动位置更改我的部分 three-section-container 的背景 img。此外,我设法添加了一个仅当我位于页面的特定部分时才会出现的叠加层。

我现在的问题是我想为背景图像的变化设置动画(我想从右到左并保持在中间位置/正如您在代码中看到的那样,背景变化了 2 次)。我尝试了`transform: translateY(40px); 属性 in CSS 但结果不一致,因为图像不会 100% 占据我的屏幕。另外,我希望我的叠加层从左到右,但我很困惑。

HTML
  <div id="bigSection" class="three-section-container  ">
          <div id="target-overlay" class=" "></div>
            <div  class="sec1  " id="project01"></div>
            <div class="sec2 "  id="project02"></div>
            <div class="sec3" id="project03"></div>
          
        </div>

CSS
.three-section-container{
  position: relative;
background-color: black;
transition: all 3s ease;
background-image: url('../../Assets/Images/graphic/last/poza-augmented-reality.png');
background-repeat: no-repeat;

background-color: black;
background-size: 100% 100vh;
background-attachment: fixed;
 
  
}

.fade-img1{
  transition: all 1s ease;

  background-image: url('../../Assets/Images/graphic/last/poza-augmented-reality.png');
  background-repeat: no-repeat;
 
  background-color: black;
  background-size: 100% 100vh;
  background-attachment: fixed;
  // transform: translatey(20px);
  opacity: 1;
 margin: 0;
 z-index: 999;
}

.fade-img2{
 
  background-image: url('../../Assets/Images/graphic/last/2.png');
  background-repeat: no-repeat;
 
  background-color: black;
  background-size: 100% 100vh;
  background-attachment: fixed;
  opacity: 1;
  transition: all 1s ease;
 margin: 0;
 z-index: 999;
 
}
.fade-img3{
 
  background-image: url('../../Assets/Images/graphic/last/poza-interior.png');
  background-repeat: no-repeat;
 
  background-color: black;
  background-size: 100% 100vh;
  background-attachment: fixed;
  // transform: translateY(40px);
  opacity: 1;
  transition: all 0.3s ease;
 margin: 0;
 z-index: 999;
}
.sec1, .sec2, .sec3{
  height: 100vh;
}

.overlay {
  transition: 0.3s linear all;
  position: absolute; /* Sit on top of the page content */
  width: 40%; /* Full width (cover the whole page) */
  height: 100%; /* Full height (cover the whole page) */
  top: 0;
  left: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5); /* Black background with opacity */
  z-index: 999; /* Specify a stack order in case you're using a different order for other elements */
}

JS

$(document).ready(function(){
    var controller=new ScrollMagic.Controller()
// build a scene

var ourScene= new ScrollMagic.Scene({
    triggerElement:'#project01',
    duration:"100%"
})
.setClassToggle('#bigSection', 'fade-img1')
.addIndicators({
    name:'fade scene',
    colorTRigger:'black',
    indent:200,
    colorStart:'#75c695'
})
.addTo(controller)
var ourScene= new ScrollMagic.Scene({
    triggerElement:'#project02',
    duration:"100%"
})
.setClassToggle('#bigSection', 'fade-img2')
.addIndicators({
    name:'fade scene',
    colorTRigger:'black',
    indent:200,
    colorStart:'#75c695'
})
.addTo(controller)

var ourScene= new ScrollMagic.Scene({
    triggerElement:'#project03',
    duration:"200%"
})
.setClassToggle('#bigSection', 'fade-img3')
.addIndicators({
    name:'fade scene',
    colorTRigger:'black',
    indent:200,
    colorStart:'#75c695'
})
.addTo(controller)



var ourScene= new ScrollMagic.Scene({
    triggerElement:'#project01',
    // duration:"200%"
})
.setClassToggle('#target-overlay', 'overlay')
.addIndicators({
    name:'overlay',
    colorTRigger:'black',
    indent:200,
    colorStart:'#75c695'
})
.addTo(controller)




})

欢迎任何帮助。谢谢

我对 ScrollMagic 不熟悉 API 但我认为这段代码片段可以使动画中涉及的 JS 和 CSS 前景变得更清晰。

事实上,其中大部分都可以在不需要外部设备的情况下完成 API,只需来回触发 CSS class !

希望对您有所帮助:

  let animationDone = false;
    window.addEventListener("scroll", () => {
      /*
       * IF you scrolled more than a certain amount:
       * in this case i choose half a page height's (50vh),
       * you trigger the slide animation by adding the onscreen class to the background2 div.
       * Otherwise if you previously triggered the animation and
       * you scrolled in the opposite direction: the animation is triggered backwards.
       */
      if(window.scrollY > window.innerHeight / 2) {
        document.getElementsByClassName("background2")[0].classList.add("onscreen");
        document.getElementById("secondPage").classList.add("onscreen");
        animationDone = true; //We makes sure that we always know the state of our animation
      } else if(animationDone) {
        document.getElementsByClassName("background2")[0].classList.remove("onscreen");
        document.getElementById("secondPage").classList.remove("onscreen");
        animationDone = false; //We makes sure that we always know the state of our animation
      }
    }, {passive:true});
      body {
        color:white;
        margin: 0;
        width:100vw;
        height:200vh;   /* 200vh is only for demo purposes: it allows to scroll the html body even thought there's nothing inside */
      }

      #mainContent {
        text-align: center;
        z-index: 99;
        position: absolute;
      }

      #mainContent > * {
          display: flex;
          justify-content: center;
          align-items: center;
      }

      #firstPage {
        width: 100vw;
        height: 100vh;
      }

      #secondPage {
        width: 100vw;
        height: 100vh;
        opacity: 0; /* This makes our background2 div transparent as soon as its hidden */
        transform: translateX(-100vw); /* This shifts our background to the left by 100vw (its width) */
        transition: 1s;     /* The page content animation's duration */
      }

      #secondPage.onscreen {
        /*
         * This cancels the second page's previous shift (to left) when the onscreen class is applied to secondPage div
         * in 0.3s so that it won't snap-> the left to right transition is realized !
         */
        transform: translateY(0);
        opacity: 1; /* This makes our background2 fades from transparent (its original state) to opaque */
      }

      .background1 {
        z-index: 1;         /* Lower stacking index than the other background to hide it */
        position: fixed;
        width: 100vw;
        height: 100vh;
        background: red;
      }

      .background2 {
        z-index: 2;         /* Higher stacking index than the other background to show it*/
        position: fixed;
        width: 100vw;
        height: 100vh;
        background: blue;

        opacity: 0; /* This makes our background2 div transparent as soon as its hidden */
        transform: translateX(100vw); /* This shifts our background to the right by 100vw (its width) */
        transition: 0.3s;     /* The background2 animation's duration */
      }

      .background2.onscreen {
        /*
         * This cancels the background's previous shift when the onscreen class is applied to background2
         * in 0.3s so that it won't snap-> the right to left transition is realized !
         */
        transform: translateY(0);
        opacity: 1; /* This makes our background2 fades from transparent (its original state) to opaque */
      }
  <body>
    <div id = "mainContent">
      <h1 id = "firstPage">The main content goes here</h1>
      <h1 id = "secondPage">Animation Triggered !</h1>
    </div>

    <div class = "background1"></div>
    <div class = "background2"></div>
    </div>
  </body>