Parallax effect - force text block to behave like background-attachment: fixed

后端 未结 2 443
日久生厌
日久生厌 2021-01-20 17:23

I\'m attempting to create a simple parallax effect where each 100vh section scrolls up to reveal the next section (new background color, background image, and t

2条回答
  •  心在旅途
    2021-01-20 18:04

    In a previous question I did a similar effect with image and using some JS so am going to use the same technique to reproduce this using content as I don't think there is a pure CSS solution. So the idea is to simulate the fixed position by using absolute position and adjusting the top property dynamically on scroll.

    Here is an example where I also adjusted some of the CSS to make it easier. I will also rely on CSS variables to make the JS code very light so we can manage everything with CSS.

    window.onscroll = function() {
      var scroll = window.scrollY || window.scrollTop || document.getElementsByTagName("html")[0].scrollTop;
      document.documentElement.style.setProperty('--scroll-var', scroll + "px");
    }
    :root {
      --scroll-var: 0px
    }
    
    body {
      margin: 0;
      padding: 0;
    }
    
    h2 {
      font-size: 48px;
    }
    
    p {
      font-size: 18px;
    }
    
    section {
      min-height: 100vh;
      width: 100%;
      text-align: center;
      overflow: hidden;
      background-attachment: fixed !important;
      background-size: cover !important;
      background-repeat: no-repeat !important;
      position: relative; /*Mandatory for the overflow effect*/
      height: 100vh;
    }
    
    section.first {
      background: linear-gradient(rgba(74, 180, 220, .85), rgba(74, 180, 220, .85)), url(https://picsum.photos/1920/500/?image=1057);
    }
    
    section.first .content {
      /* the first section so top start from 0*/
      top: calc((0 * 100vh) + var(--scroll-var));
    }
    
    section.second {
      background: linear-gradient(rgba(103, 198, 180, .85), rgba(103, 198, 180, .85)), url(https://picsum.photos/1920/500/?image=1067);
    }
    
    section.second .content {
      /* the second section so we need to remove the height of top section
         to have the same position so -100vh and we do the same for the other sections  
      */
      top: calc((-1 * 100vh) + var(--scroll-var));
    }
    
    section.third {
      background: linear-gradient(rgba(5, 123, 188, .85), rgba(5, 123, 188, .85)), url(https://picsum.photos/1920/500/?image=1033);
    }
    
    section.third .content {
      top: calc((-2 * 100vh) + var(--scroll-var));
    }
    
    section.fourth {
      background: linear-gradient(rgba(187, 216, 100, .85), rgba(187, 216, 100, .85)), url(https://picsum.photos/1920/500?image=1063);
    }
    
    section.fourth .content {
      top: calc((-3 * 100vh) + var(--scroll-var));
    }
    
    .content {
      position: absolute;
      height: 100%;
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .copy {
      color: #fff;
      font-family: 'Noto Serif', serif;
      font-weight: 300;
      max-width: 300px;
    }
    
    .button {
      border: 2px solid #fff;
      border-radius: 3px;
      padding: 15px 25px;
      display: inline-block;
      width: auto;
      font-family: 'Assistant', sans-serif;
      font-weight: 700;
      text-transform: uppercase;
      letter-spacing: 1px;
      transition: .2s ease all;
    }
    
    .button:hover {
      background: #fff;
      color: #333;
      cursor: pointer;
    }
    
      

    Header 1

    Nullam id dolor id nibh ultricies vehicula ut id elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.

    Header 2

    Nullam id dolor id nibh ultricies vehicula ut id elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.

    Header 3

    Nullam id dolor id nibh ultricies vehicula ut id elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.

    Call to action

    Button

提交回复
热议问题