Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. define(
  2. [
  3. 'jquery'
  4. ], function ($) {
  5.  
  6. var parallaxElements = $('.para');
  7.  
  8. var ticking = false;
  9. lastScrollY = $(window).scrollTop();
  10.  
  11. function onResize() {
  12. updateElements();
  13. lastScrollY = $(window).scrollTop();
  14. }
  15.  
  16. function onScroll(evt) {
  17.  
  18. if (!ticking) {
  19. ticking = true;
  20. requestAnimFrame(updateElements);
  21. lastScrollY = $(window).scrollTop();
  22. }
  23. }
  24.  
  25. function updateElements() {
  26.  
  27. if (parallaxElements !== null) {
  28. parallaxElements.each(function (){
  29.  
  30. var limitMax = lastScrollY + $(window).height() + 50;
  31. var limitMin = lastScrollY + $(window).height()/3;
  32.  
  33. if( $(this).offset().top > limitMin){
  34. var pos = ($(this).offset().top - limitMin)/(limitMax - limitMin) * 35;
  35. prefix($(this), "Transform", "translate3d(0, " + pos + "px, 0)");
  36. }else{
  37. prefix($(this), "Transform", "translate3d(0, 0, 0)");
  38. }
  39.  
  40. });
  41. }
  42.  
  43. ticking = false;
  44. }
  45.  
  46.  
  47. function prefix(obj, prop, value) {
  48. var prefs = ['webkit', 'Moz', 'o', 'ms'];
  49. for (var pref in prefs) {
  50. obj.css( prefs[pref] + prop , value);
  51. }
  52. }
  53.  
  54. // shim layer with setTimeout fallback
  55. window.requestAnimFrame = (function() {
  56. return window.requestAnimationFrame ||
  57. window.webkitRequestAnimationFrame ||
  58. window.mozRequestAnimationFrame ||
  59. window.oRequestAnimationFrame ||
  60. window.msRequestAnimationFrame ||
  61. function(callback) {
  62. window.setTimeout(callback, 1000 / 60);
  63. };
  64. })();
  65.  
  66.  
  67. function initParallax() {
  68. updateElements();
  69. lastScrollY = $(window).scrollTop();
  70. }
  71.  
  72. window.addEventListener('resize', onResize, false);
  73. window.addEventListener('scroll', onScroll, false);
  74.  
  75.  
  76. return {
  77. init: initParallax
  78. }
  79. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement