Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define(
- [
- 'jquery'
- ], function ($) {
- var parallaxElements = $('.para');
- var ticking = false;
- lastScrollY = $(window).scrollTop();
- function onResize() {
- updateElements();
- lastScrollY = $(window).scrollTop();
- }
- function onScroll(evt) {
- if (!ticking) {
- ticking = true;
- requestAnimFrame(updateElements);
- lastScrollY = $(window).scrollTop();
- }
- }
- function updateElements() {
- if (parallaxElements !== null) {
- parallaxElements.each(function (){
- var limitMax = lastScrollY + $(window).height() + 50;
- var limitMin = lastScrollY + $(window).height()/3;
- if( $(this).offset().top > limitMin){
- var pos = ($(this).offset().top - limitMin)/(limitMax - limitMin) * 35;
- prefix($(this), "Transform", "translate3d(0, " + pos + "px, 0)");
- }else{
- prefix($(this), "Transform", "translate3d(0, 0, 0)");
- }
- });
- }
- ticking = false;
- }
- function prefix(obj, prop, value) {
- var prefs = ['webkit', 'Moz', 'o', 'ms'];
- for (var pref in prefs) {
- obj.css( prefs[pref] + prop , value);
- }
- }
- // shim layer with setTimeout fallback
- window.requestAnimFrame = (function() {
- return window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.oRequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- function(callback) {
- window.setTimeout(callback, 1000 / 60);
- };
- })();
- function initParallax() {
- updateElements();
- lastScrollY = $(window).scrollTop();
- }
- window.addEventListener('resize', onResize, false);
- window.addEventListener('scroll', onScroll, false);
- return {
- init: initParallax
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement