Created
October 6, 2016 01:08
-
-
Save brianyang/249bfe625e2f38010654fcfcc757f361 to your computer and use it in GitHub Desktop.
parallax example from netlify
This file contains hidden or 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
var parallaxImages = function parallaxImages() { | |
if ($(".about.page").length && $("html.no-touchevents").length && winWidth >= 768) { | |
var triggered = false | |
, oldWindowPos = 0 | |
, fasterImgMargin = 140 | |
, slowerImgMargin = 40; | |
$(window).on("scroll", function() { | |
window.requestAnimationFrame(scrollHandler); | |
function scrollHandler() { | |
var currentWindowPos = $(window).scrollTop() | |
, scrollingDown = currentWindowPos > oldWindowPos; | |
if (scrollingDown && !triggered) { | |
slowerImgMargin--; | |
fasterImgMargin = fasterImgMargin - 2 | |
} else { | |
slowerImgMargin++; | |
fasterImgMargin = fasterImgMargin + 2 | |
} | |
if (slowerImgMargin >= -169 && slowerImgMargin <= 20) { | |
$("#vertical-parallax-img").css({ | |
transform: "translate3d(0px, " + slowerImgMargin + "px, 0px)" | |
}) | |
} | |
if (fasterImgMargin >= -278 && fasterImgMargin <= 100) { | |
$("#horizontal-parallax-img").css({ | |
transform: "translate3d(0px, " + fasterImgMargin + "px, 0px)" | |
}) | |
} | |
oldWindowPos = currentWindowPos | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment