Last active
April 20, 2018 11:25
-
-
Save p410n3/f13c8197cbea61469d1de6c414cf4426 to your computer and use it in GitHub Desktop.
My parallax JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="box-three parralax" id="parallax-three"> | |
<div class="container"> | |
<h2>Bla bla</h2> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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