如何在一页网站上添加整页视差滚动

时间:2019-08-15 12:55:49

标签: javascript css smooth-scrolling parallax.js

我想在一页网站上添加整页parallax scroll effect,但是我不知道如何添加这种效果。

我尝试设置height: 100vh部分,但结果是最后一部分覆盖了上述部分。

您有什么建议吗?任何帮助表示赞赏。非常感谢。

HTML:

<html lang="en">
  <body>
    <div class="twinkling"></div>
    <header>
      <div class="header-container">
        <h1>Constellations</h1>
      </div>
      <div class="button-container" id="toggle">
        <span class="top"></span>
        <span class="middle"></span>
        <span class="bottom"></span>
      </div>
      <div class="overlay" id="overlay">
        <nav class="overlay-menu">
          <ul>
            <li><a href="#constellation">Home</a></li>
            <li><a href="#what is constellations?">About</a></li>
            <li><a href="#12 constellations">12 Constellations</a></li>
            <li><a href="#">Stargazing</a></li>
          </ul>
        </nav>
      </div>
    </header>
    <section id="section1" class="background">
      <div id="crystal-animation">
        <figure>
          <img src="img/crystal.svg" alt="" class="crystal">
        </figure>
      </div>
    </section>
    <section id="section2" class="background">
      <article class="content">
        <h2>What is Constellations?</h2>
        <p>Constellations are patterns in the night sky often formed by the most prominent stars to the naked eye. </p>
        <p>Constellations often carry names and take the shape of gods, hunters, princesses, objects and mythical beasts associated with Greek mythology – however, at times. </p>
        <aside><img src="img/crystal-animation.svg" alt="" class="crystal-animation"></aside>
      </article>
    </section>
    <section id="section3" class="background">
      <h2 >12 Constellations</h2>
      <p>Click the constellation cards and understand more.</p>
      <div class="card">
        <ul class="gallery">
          <li><a href="aries.html"><img src="img/12-constellation-cards/1.svg" alt="" class="svg"></a></li>
          <li><a href="taurus.html"><img src="img/12-constellation-cards/4.svg" alt="" class="svg"></a></li>
          <li><a href="gemini.html"><img src="img/12-constellation-cards/7.svg" alt="" class="svg"></a></li>
          <li><a href="cancer.html"><img src="img/12-constellation-cards/10.svg" alt="" class="svg"></a></li>
        </ul>
        <ul class="gallery">
          <li><a href="leo.html"><img src="img/12-constellation-cards/2.svg" alt="" class="svg"></a></li>
          <li><a href="virgo.html"><img src="img/12-constellation-cards/5.svg" alt="" class="svg"></a></li>
          <li><a href="libra.html"><img src="img/12-constellation-cards/8.svg" alt="" class="svg"></a></li>
          <li><a href="pisces.html"><img src="img/12-constellation-cards/11.svg" alt="" class="svg"></a></li>
        </ul>
        <ul class="gallery">
          <li><a href="saguttarius.html"><img src="img/12-constellation-cards/3.svg" alt="" class="svg"></a></li>
          <li><a href="capricorn.html"><img src="img/12-constellation-cards/6.svg" alt="" class="svg"></a></li>
          <li><a href="aquarius.html"><img src="img/12-constellation-cards/9.svg" alt="" class="svg"></a></li>
          <li><a href="scorpio.html"><img src="img/12-constellation-cards/12.svg" alt="" class="svg"></a></li>
        </ul>
      </div>
    </section>
    <section id="section4" class="background">
      <div class="s3-content">
        <h2>How to Find Constellations in the Night Sky?</h2>
        <p class="s3">Using a star map will be your best bet for assisting in finding where to look for constellations, depending on your location and time of year. It’s different depending on where you live and on the seasons. You can also need the additional app to help find the constellations, that lets you enter your location and gives you a customized star map. </p>
        <p class="s3">All you need is a dark sky, so as far away from cities as possible, and for extra visual aide, a pair of binoculars or a telescope. With binoculars or a telescope, you’ll see fainter stars and other features like nebulae and star clusters. When you’re out observing, you’ll want to generally orient yourself towards the North Star.</p>
      </div>
    </section>
  </body>
</html>

JavaScript:

$(document).ready(function () {
   let hamburgerClicked = false;
   $("#toggle").on("click", function () {
      if (hamburgerClicked == false) {
         $(".top").css(
            "transform",
            "translateY(11px) translateX(0) rotate(45deg)"
         );
         $(".middle").css("display", "none");
         $(".bottom").css(
            "transform",
            "translateY(-11px) translateX(0) rotate(-45deg)"
         );

         hamburgerClicked = true;
      } else {
         $(".top").css("transform", "translateY(0px) translateX(0) rotate(0deg)");
         $(".middle").css("display", "");
         $(".bottom").css(
            "transform",
            "translateY(0px) translateX(0) rotate(0deg)"
         );
         hamburgerClicked = false;
      }
      $("#overlay").toggleClass("active");
      $("#overlay").toggleClass("open");
   });
});

CSS:

* {
  box-sizing: border-box;
  list-style: none;
}

html {
  height: 100%;
  scroll-behavior: smooth;
}

body {
  height: 100%;
  font-family: 'Source Sans Pro', sans-serif;
  font-weight: 200;
  font-size: 15px;
  color: #fff;
  background-image: url(../img/background/background.png);
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}

#meteor-shower {
  position: absolute;
  top: 0;
  left: 0;
}

section {
  height: 100vh;
  transform: translateY(40vh);
  will-change: transform;
  transform: translateY(30vh);
  transition-timing-function: .2s all, cubic-bezier(0.22, 0.44, 0, 1);
  text-decoration: none;
  scroll-behavior: smooth;
}

.twinkling {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 95%;
  display: block;
}

.twinkling {
  position: absolute;
  background: transparent url(../img/background/2446101-03.png) repeat top center;
  background-color: rgba(0, 0, 0, 0);
  background-size: cover;
  background-repeat: no-repeat;
  z-index: 1;
  -webkit-animation: move-twink-back 16s infinite;
  -moz-animation: move-twink-back 16s infinite;
  -o-animation: move-twink-back 16s infinite;
  animation: move-twink-back 16s infinite;
}

@keyframes move-twink-back {
  from {
    background-position: 0 0;
  }
  to {
    background-position: -10000px 5000px;
  }
}

@-webkit-keyframes move-twink-back {
  from {
    background-position: 0 0;
  }
  to {
    background-position: -10000px 5000px;
  }
}

header {
  height: 3em;
  position: fixed;
}

.header-container {
  overflow: hidden;
  position: fixed;
  top: 0;
  width: 100%;
}

.header-container h1 {
  font-family: 'Gabriela', serif;
  font-size: 1.5em;
  float: left;
  padding-top: 5px;
  padding-left: 20px;
  display: block;
}

.button-container {
  position: fixed;
  top: 5%;
  right: 2%;
  height: 27px;
  width: 35px;
  cursor: pointer;
  z-index: 100;
  transition: opacity 0.25s ease;
  opacity: 1;
  content: "";
}

.button-container:hover {
  opacity: 0.7;
}

.top:active {
  transform: translate(11px) translateX(0) rotate(45deg);
}

.middle:active {
  opacity: 0;
}

.bottom:active {
  transform: translateY(-11px) translateX(0) rotate(-45deg);
}

span {
  background: #fff;
  border: none;
  height: 4px;
  width: 32px;
  position: absolute;
  top: 0;
  left: 0;
  transition: all 0.35s ease;
  cursor: pointer;
  border-radius: 3px;
}

.middle {
  top: 10px;
}

.bottom {
  top: 20px;
}

.overlay {
  z-index: 500;
  position: fixed;
  background: rgba(0, 0, 0, 0.6);
  top: 0;
  left: 0;
  width: 5%;
  height: 0%;
  opacity: 0.6;
  visibility: hidden;
  transition: opacity 0.35s, visibility 0.35s, height 0.35s;
}

li {
  animation: fadeInRight 0.5s ease forwards;
  animation-delay: 0.35s;
}

li:nth-of-type(2) {
  animation-delay: 0.4s;
}

li:nth-of-type(3) {
  animation-delay: 0.45s;
}

li:nth-of-type(4) {
  animation-delay: 0.5s;
}

nav {
  position: relative;
  height: 70%;
  top: 20%;
  transform: translateY(-50%);
  font-size: 0.8em;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0 auto;
  display: inline-block;
  position: relative;
  height: 100%;
}

li {
  display: block;
  height: 25%;
  min-height: 50px;
  position: relative;
  opacity: 0;
}

a {
  display: block;
  position: relative;
  text-decoration: none;
  overflow: hidden;
}

a:hover {
  transform: scale(1);
}

a:hover:after,
a:focus:after,
a:active:after {
  width: 30%;
}

a:after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0%;
  transform: translateX(-50%);
  height: 3px;
  background: rgba(0, 0, 0, 0.6);
  transition: 0.35s;
}

@keyframes fadeInRight {
  0% {
    opacity: 0;
    left: 20%;
  }
  100% {
    opacity: 1;
    left: 0;
  }
}

.active {
  visibility: visible;
}

#section1 {
  background: rgba(0, 0, 0, 0);
  height: 100vh;
}

#section1 {
  :nth-child(1) {
    padding-top: 0px;
    display: flex;
  }
}

#crstal-animation {
  overflow: hidden;
  display: block;
}

.crystal {
  width: 25%;
  position: absolute;
  top: -46px;
  left: 40%;
  margin: 0;
  margin-right: -50%;
  transform-style: preserve-3d;
  -webkit-animation: rotation 8s infinite linear;
  -moz-animation: rotation 8s infinite linear;
  -o-animation: rotation 8s infinite linear;
  animation: rotation 8s infinite linear;
}

@-webkit-keyframes rotation {
  0% {
    -webkit-transform: rotate(0deg);
    filter: drop-shadow(16px 16px 10px rgb(255, 255, 255, 0.8));
  }
  35% {
    -webkit-transform: rotate(38deg) rotateX(5deg);
    filter: drop-shadow(0px 10px 16px rgb(255, 255, 255, 0.8));
  }
  65% {
    -webkit-transform: rotate(-38deg) rotateX(2deg);
    filter: drop-shadow(16px 0px 10px rgb(255, 255, 255, 0.8));
  }
  86% {
    -webkit-transform: rotateX(-20deg) rotateY(1deg);
    filter: drop-shadow(0px 10px 16px rgb(255, 255, 255, 0.8));
  }
  100% {
    -webkit-transform: rotate(0deg);
    filter: drop-shadow(16px 16px 10px rgb(255, 255, 255, 0.8));
  }
}

#section2 {
  padding: 60px 130px;
  display: inline-block;
  background: rgba(0, 0, 0, 0);
  height: 100vh;
}

.content {
  width: 59%;
  background: rgba(0, 0, 0, 0.6);
  position: relative;
  padding-bottom: 30px;
  padding-left: 30px;
  display: block;
}

.content p {
  width: 88%;
  margin: 15px;
  text-align: left;
}

.content h2 {
  text-align: center;
  padding-top: 30px;
}

.crystal-animation {
  width: 68%;
  float: right;
  margin-top: -56%;
  margin-right: -80%;
  -webkit-filter: drop-shadow(5px 5px 5px #222);
  -moz-filter: drop-shadow(5px 5px 5px #222);
  -o-filter: drop-shadow(5px 5px 5px #222);
  filter: drop-shadow(5px 5px 5px #222);
}

aside {
  display: block;
}

#section3 {
  background: rgba(0, 0, 0, 0);
  height: 100vh;
}

#section3:nth-child(2) {
  display: flex;
  align-items: center;
  justify-content: center;
}

.gallery {
  list-style: none outside none;
  flex-direction: row;
  display: flex;
  align-content: center;
  flex-flow: row wrap;
  justify-content: center;
}

h2,
p {
  text-align: center;
}

.heading {
  text-align: center;
}

.gallery li {
  margin: 10px;
}

.gallery li:hover {
  transform: scale(1.1)
}

.svg {
  width: 75%;
  filter: drop-shadow(10px)rgba(0, 0, 0, 0.6);
  -moz-box-shadow: 10px 10px 5px rgba(0, 0, 0, 0.6);
  -webkit-box-shadow: 10px 10px 5px rgba(0, 0, 0, 0.6);
  -khtml-box-shadow: 10px 10px 5px rgba(0, 0, 0, 0.6);
}

#section3-content {
  padding-right: 100px;
  padding-left: 100px;
  margin-top: -90px;
  overflow: hidden;
  transition-timing-function: ease;
  transition: .3s;
}

.intro h3 {
  font-family: 'Gabriela', serif;
}

.content-text {
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  padding-bottom: 30px;
  padding-left: 20px;
}

.constellation-img {
  opacity: .35;
  width: 50%;
  float: left;
  top: -80px;
  position: relative;
}

.constellation-star {
  width: 56%;
  opacity: 1;
  float: left;
  left: 1%;
  margin-top: -80px;
  position: absolute;
}

.intro {
  float: right;
  width: 50%;
  top: 40%;
  padding-top: 30px;
  padding-bottom: 10px;
  padding-right: 100px;
  color: floralwhite;
}

.constellation-pattern {
  width: 11%;
  opacity: .9;
  margin-left: -10px;
  margin-bottom: -15px;
}

.intro p {
  text-align: left;
  font-size: .9em;
  margin-bottom: 15px;
  font-weight: 200;
}

.constellation-icon {
  list-style: none;
  padding: 0;
  margin: 0;
  display: block;
}

.constellation-icon li {
  display: table-cell;
  vertical-align: middle;
  padding: 1px;
  padding-top: 15px;
}

.constellation-button {
  width: 50%;
}

.constellation-button:hover {
  background: rgb(0, 0, 205, 0.7);
  filter: brightness(3);
  border-radius: 80px;
  border: 2px solid rgb(0, 0, 205, 0.7);
}

.constellation-button:active {
  background: rgb(0, 0, 205);
  filter: grayscale(.2);
  transition-timing-function: linear;
}

.constellation-star-aq {
  width: 46%;
  opacity: 1;
  float: left;
  left: 1%;
  margin-top: -10px;
  position: absolute;
}

.constellation-star-ca {
  width: 36%;
  opacity: 1;
  float: left;
  left: 1%;
  margin-top: 30px;
  margin-left: 40px;
  position: absolute;
  transform: rotate(16deg);
}

.constellation-img-ca {
  opacity: .35;
  width: 50%;
  float: left;
  top: -40px;
  position: relative;
}

.constellation-star-ge {
  width: 36%;
  opacity: 1;
  float: left;
  left: 1%;
  margin-top: 90px;
  margin-left: 60px;
  position: absolute;
  transform: rotate(-3deg);
}

.constellation-img-leo {
  opacity: .35;
  width: 50%;
  float: left;
  top: -40px;
  position: relative;
}

.constellation-star-leo {
  width: 40%;
  opacity: 1;
  float: left;
  left: 1%;
  margin-top: 20px;
  margin-left: 70px;
  position: absolute;
  transform: rotate(5deg);
}

.constellation-img-sc {
  opacity: .35;
  width: 50%;
  float: left;
  top: -20px;
  position: relative;
}

#section4 {
  height: 100vh;
  display: block;
  align-content: center;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  padding: 0;
  margin: 0;
}

.s3 {
  text-align: left;
}

.s3-content {
  width: 74%;
  padding-top: 16%;
  padding-left: 30%;
}

这是JSFiddle上的一个粗糙的演示工作:https://jsfiddle.net/ha3c04yL/4/

0 个答案:

没有答案