JSFiddle - React, Tailwind, and code Playground
by graphettion
HTML
<section class="parallax" data-plx-img="http://loremflickr.com/960/720/cat?random=1.jpg">
<h2>Proper Background Parallax</h2>
<p>Works on touch devices including iPhone.</p>
</section>
<section class="parallax" data-plx-img="http://loremflickr.com/960/720/cat?random=2.jpg">
<h2>Proper Background Parallax</h2>
<p>Works on touch devices including iPhone.</p>
</section>
<section class="parallax" data-plx-img="http://loremflickr.com/960/720/cat?random=3.jpg">
<h2>Proper Background Parallax</h2>
<p>Works on touch devices including iPhone.</p>
</section>
<section class="parallax" data-plx-img="http://loremflickr.com/960/720/cat?random=4.jpg">
<h2>Proper Background Parallax</h2>
<p>Works on touch devices including iPhone.</p>
</section>
<section class="parallax" data-plx-img="http://loremflickr.com/960/720/cat?random=5.jpg">
<h2>Proper Background Parallax</h2>
<p>Works on touch devices including iPhone.</p>
</section>
<section class="parallax" data-plx-img="http://loremflickr.com/960/720/cat?random=6.jpg">
<h2>Proper Background Parallax</h2>
<p>Works on touch devices including iPhone.</p>
</section>
CSS
*,
*:before,
*:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-kerning: auto;
}
html {
-webkit-text-size-adjust: 100%;
background-color: #ffffff;
}
body {
width: 100%;
max-width: 100%;
margin: 0;
padding: 0;
position: relative;
font-size: 1.8em;
line-height: 1.3;
font-weight: 400;
font-family: 'Open Sans', 'Source Sans Pro', Roboto, 'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', 'Myriad Pro', 'Segoe UI', Myriad, Helvetica, 'Lucida Grande', 'DejaVu Sans Condensed', 'Liberation Sans', 'Nimbus Sans L', Tahoma, Geneva, Arial, sans-serif;
-webkit-text-size-adjust: 100%;
background-color: #ffffff;
}
/*--- Parallax ---*/
.parallax {
display: block;
position: relative;
width: 100%;
max-width: 100%;
height: 500px;
padding: 50px 0;
background: none;
z-index: 1;
text-align: center;
overflow: hidden;
}
.plx-img {
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
height: 125%;
z-index: -1;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.parallax h2 {
display: block;
font-size: 60px;
line-height: 66px;
font-weight: 700;
text-align: center;
color: #ffffff;
text-shadow: 0 1px 5px rgba(0, 0, 0, 0.75);
}
.parallax p {
display: block;
width: 600px;
max-width: 100%;
margin: 0 auto;
font-size: 24px;
line-height: 28px;
font-weight: 400;
text-align: center;
color: #ffffff;
text-shadow: 0 1px 5px rgba(0, 0, 0, 0.75);
}
@media screen and (max-width: 767px) {
.parallax h2 {
font-size: 40px;
line-height: 46px;
}
.parallax p {
font-size: 18px;
line-height: 22px;
}
}
JavaScript
function parallaxImages() {
// Set the scroll for each parallax individually
var plx = document.getElementsByClassName('parallax');
for (i = 0; i < plx.length; i++) {
var height = plx[i].height;
var img = plx[i].getAttribute('data-plx-img');
var plxImg = document.createElement("div");
plxImg.className += " plx-img";
plxImg.style.height = (height + 50) + 'px';
plxImg.style.backgroundImage = 'url(' + img + ')';
plx[i].insertBefore(plxImg, plx[i].firstChild);
}
}
parallaxImages();
function plxScroll() {
var scrolled = window.scrollY;
var win_height_padded = window.innerHeight * 1.25;
// Set the scroll for each parallax individually
var plx = document.getElementsByClassName('parallax');
for (i = 0; i < plx.length; i++) {
var offsetTop = plx[i].offsetTop;
var singleScroll = (scrolled - offsetTop) - 50;
if (scrolled + win_height_padded >= offsetTop) {
var plxImg = plx[i].getElementsByClassName('plx-img')[0];
plxImg.style.top = (singleScroll / 5) + "px";
}
}
}
window.addEventListener('load', plxScroll);
window.addEventListener('resize', plxScroll);
window.addEventListener('scroll', plxScroll);