Skip to content Skip to sidebar Skip to footer

Pure Css Parallax + Navigation Reveal On Scroll

So I've run into a problem involving pure CSS parallax in combination with a fixed navbar that changes size when the page begins to scroll or returns to the top. Each of the two pa

Solution 1:

After a bit of scouring the web and teaching myself more about javascript I have solved my dilemma.

functionyScroll() {
  var el = document.getElementById("main");
  var nav = document.getElementById("navBar");
  var y = el.scrollTop;
  if (y >= 50) {
    nav.className = "mini";
  } else {
    nav.className = "";
  }
}
html {
  font-size: 10px;
  scroll-behavior: smooth;
  color: #212121;
  box-sizing: border-box;
  line-height: 1.4;
  margin: 0;
  padding: 0;
  min-width: 32rem;
}

body {
  font: normal 4001.5rem'Montserrat Alternates';
  background: #ffffff;
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  overscroll-behavior-y: none;
}

nav {
  width: calc(100% - 1rem);
  height: 0rem;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9000;
  text-transform: uppercase;
  transition: all 150ms ease 0s;
}

nav.mini {
  background: #FFFFFF;
  height: 5rem;
  box-shadow: 00 .5rem0rgba(0, 0, 0, .15);
}

#main {
  perspective: 1px;
  transform-style: preserve-3d;
  height: 100vh;
  overflow-x: hidden;
  overflow-y: scroll;
}

#main::-webkit-scrollbar {
  width: 1rem;
}

#main::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 00 .5remrgba(#212121, .5);
}

#main::-webkit-scrollbar-thumb {
  background-color: darkgrey;
  outline: 1px solid slategrey;
}

header {
  background: url('https://faith.prismapixel.studio/assets/img/hero-desktop.jpg') top center / cover;
  display: flex;
  flex: 10 auto;
  position: relative;
  z-index: -1;
  height: 100vh;
  justify-content: center;
  align-items: center;
  transform: translateZ(-1px) scale(2.25);
}

section {
  position: relative;
  display: block;
  background: #FFFFFF;
  z-index: 1;
}

sectionarticle {
  max-width: 75rem;
  margin: 0 auto;
  padding: 7.5rem0;
}

p {
  margin: 7.5rem0;
}

</style>
<navid="navBar"class=""></nav><mainid="main"onscroll="yScroll()"><header><h1id="print">Headline Here</h1></header><section><article><p>ʻO Lorem Ipsum kahi haʻahaʻa wale nō o ka paʻi a me keʻano o nāʻoihana. ʻO Lorem Ipsum ka 'ōlelo papahana maʻamau o kaʻoihana o ka makahiki 1500, i ka wā i lawe ai kekahi mea paʻi kiʻiʻole i keʻano o ka type a scrambled iā ia e hana i kahi puke
        kiko'ī. ʻAʻole i ola wale i kaʻelima mau kenekulia, akā,ʻo ka leleʻana hoʻi i nāʻano o ka lolouila, e hoʻololiʻoleʻia ana. Ua hoʻolahaʻia i nā makahiki 1960 me ka hoʻokuʻuʻana i nā pepa Letraset i loko o nā moʻolelo Lorem Ipsum, a me nā mea hou
        aku me ka polokalamu hoʻopuka pākī e like me Aldus PageMaker me nā papa o Lorem Ipsum.
      </p><p>ʻO Lorem Ipsum kahi haʻahaʻa wale nō o ka paʻi a me keʻano o nāʻoihana. ʻO Lorem Ipsum ka 'ōlelo papahana maʻamau o kaʻoihana o ka makahiki 1500, i ka wā i lawe ai kekahi mea paʻi kiʻiʻole i keʻano o ka type a scrambled iā ia e hana i kahi puke
        kiko'ī. ʻAʻole i ola wale i kaʻelima mau kenekulia, akā,ʻo ka leleʻana hoʻi i nāʻano o ka lolouila, e hoʻololiʻoleʻia ana. Ua hoʻolahaʻia i nā makahiki 1960 me ka hoʻokuʻuʻana i nā pepa Letraset i loko o nā moʻolelo Lorem Ipsum, a me nā mea hou
        aku me ka polokalamu hoʻopuka pākī e like me Aldus PageMaker me nā papa o Lorem Ipsum.
      </p><p>ʻO Lorem Ipsum kahi haʻahaʻa wale nō o ka paʻi a me keʻano o nāʻoihana. ʻO Lorem Ipsum ka 'ōlelo papahana maʻamau o kaʻoihana o ka makahiki 1500, i ka wā i lawe ai kekahi mea paʻi kiʻiʻole i keʻano o ka type a scrambled iā ia e hana i kahi puke
        kiko'ī. ʻAʻole i ola wale i kaʻelima mau kenekulia, akā,ʻo ka leleʻana hoʻi i nāʻano o ka lolouila, e hoʻololiʻoleʻia ana. Ua hoʻolahaʻia i nā makahiki 1960 me ka hoʻokuʻuʻana i nā pepa Letraset i loko o nā moʻolelo Lorem Ipsum, a me nā mea hou
        aku me ka polokalamu hoʻopuka pākī e like me Aldus PageMaker me nā papa o Lorem Ipsum.
      </p></article></section></main>

I had to modify the width of the navbar after solving my dilemma to accommodate the width of the scrollbar due to the scrollbar actually being on the <main> element and not the <body>. But that was easy to do.

I hope this helps anyone else trying to add a fixed reveal on scroll navbar to a pure CSS website.

Post a Comment for "Pure Css Parallax + Navigation Reveal On Scroll"