Skip to content

Instantly share code, notes, and snippets.

@p410n3
Last active April 20, 2018 11:25
Show Gist options
  • Save p410n3/f13c8197cbea61469d1de6c414cf4426 to your computer and use it in GitHub Desktop.
Save p410n3/f13c8197cbea61469d1de6c414cf4426 to your computer and use it in GitHub Desktop.
My parallax JS
<div class="box-three parralax" id="parallax-three">
<div class="container">
<h2>Bla bla</h2>
</div>
</div>
//My own parallax implementation, I didnt like the other ones
function parallax(id) {
var parallax = document.getElementById(id);
var speed = 0.5;
var bounding = parallax.getBoundingClientRect();
var offset = parallax.offsetTop * speed;
if (bounding.y < bounding.height) {
var windowYOffset = window.pageYOffset;
var elBackgrounPos = "50% " + (windowYOffset * speed - offset) + "px";
parallax.style.backgroundPosition = elBackgrounPos;
}
}
window.addEventListener("scroll", function() {
parallax("parallax-three");
});
.box-three {
&.parralax {
background-image: url("../img/bg-three.png");
background-size: cover;
background-repeat: no-repeat;
max-height: 100vh;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment