JSFiddle - React, Tailwind, and code Playground
by HDL52
HTML
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400,700">
<section>
<div id="parallax" class="parallax-item">
<h2>
Div 1
</h2>
</div>
<div class="parallax-item">
<h2>
Div 2
</h2>
</div>
<div class="parallax-item">
<h2>
Div 3
</h2>
</div>
<div class="parallax-item">
<h2>
Div 4
</h2>
</div>
</section>
CSS
* {
padding: 0;
margin: 0;
}
html {
scroll-behavior: smooth;
}
.parallax-item {
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-family: Montserrat, sans-serif;
width: 100%;
min-height: 100vh;
}
.parallax-item h2 {
font-size: 3rem;
text-transform: uppercase;
background-color: whitesmoke;
padding: 1rem 1.5rem 1rem 1.5rem;
font-weight: bold;
}
.parallax-item:first-child {
background: url("https://raw.githubusercontent.com/Hongda-OSU/PicGo-2.3.1/master/img%E5%85%8B%E9%B2%81%E8%B5%9B%E5%BE%B7%E6%88%98%E8%AE%B03.jpg");
background-size: cover;
}
.parallax-item:nth-child(2) {
background: #000000;
}
.parallax-item:nth-child(3) {
background: url("https://raw.githubusercontent.com/Hongda-OSU/PicGo-2.3.1/master/img1l99wbel5ip2y1oivo40zrh051humxd.png");
background-size: cover;
background-position: center;
background-attachment: fixed;
}
.parallax-item:nth-child(4) {
background: #00f8f8;
}
@media screen and (max-width: 768px) {
.parallax-item h2 {
font-size: 1.5rem;
}
}
JavaScript
const parallax = document.getElementById("parallax");
window.addEventListener("scroll", () => {
const offset = window.pageYOffset;
parallax.style.backgroundPositionY = offset * 0.7 + "px";
});