Guest User

parallax scrolling javascript error

a guest
Dec 23rd, 2014
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <style>
  5.  
  6. body{
  7. margin: 0;
  8. padding: 0;
  9. }
  10.  
  11. ul{
  12. margin: 0;
  13. padding: 0;
  14. }
  15.  
  16. ul li{
  17. list-style: none;
  18. overflow: hidden;
  19. height: 600px;
  20. }
  21.  
  22. .parallax-background{
  23. height: 700px;
  24. }
  25.  
  26. .flower{
  27. background-image: url('http://webneel.com/wallpaper/sites/default/files/images/01-2014/2-flower-wallpaper.jpg');
  28. background-position: 50% 70%;
  29. }
  30.  
  31. .lens{
  32. background-image: url('http://lh5.ggpht.com/-Lg0g3WfBvR0/TiblYuKu3II/AAAAAAAABAM/xIaE-os2xOI/s9000/Camera%2BLens%2BIn%2BThe%2BDark%2BRoom.jpg');
  33. background-position: 50% 100%;
  34. }
  35.  
  36. .beach{
  37. background-image: url('http://webneel.com/wallpaper/sites/default/files/images/04-2013/tropical-beach-wallpaper.jpg');
  38. background-position: 50% 70%;
  39. }
  40.  
  41. .wolf{
  42. background-image: url('http://jootix.com/upload/DesktopWallpapers/cache/grey-wolf-snow-grey-wolf-snow-1366x768.jpg');
  43. background-position: 50% 70%;
  44. }
  45. </style>
  46. </head>
  47.  
  48. <body>
  49.  
  50. <ul class="parallax">
  51. <li>
  52. <div class="parallax-background flower"></div>
  53. </li>
  54.  
  55. <li>
  56. <div class="parallax-background lens"></div>
  57. </li>
  58.  
  59. <li>
  60. <div class="parallax-background beach"></div>
  61. </li>
  62.  
  63. <li>
  64. <div class="parallax-background wolf"></div>
  65. </li>
  66. </ul>
  67.  
  68. <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  69. <script>
  70.  
  71. (function($) {
  72.  
  73. var $container = $(".parallax");
  74. var $divs = $container.find("div.parallax-background");
  75. var thingBeingScrolled = document.body;
  76. var liHeight = $divs.eq(0).closest("li").height();
  77. var diffHeight = $divs.eq(0).height() - liHeight;
  78.  
  79. var i, len, div, li, offset, scroll;
  80.  
  81. var render = function(){
  82.  
  83. //things were scrolling
  84. top = thingBeingScrolled.scrollTop;
  85.  
  86. for(i = 0, len=$divs.length; i < len; i++){
  87.  
  88. //get one div
  89. div = $divs[i];
  90.  
  91. //get the parent LI
  92. li = div.parentNode;
  93.  
  94. //calculate the offseTop of div
  95. offset = $(div).offset().top;
  96.  
  97. //calculate the amount to scroll
  98. scroll = Math.round((top - offset) / liHeight) * diffHeight;
  99.  
  100. //apply the scroll amount
  101. div.style.webkitTransform = "translate3d(0px, "+scroll+"px,0px)";
  102.  
  103. }
  104.  
  105. };
  106.  
  107. (function loop(){
  108.  
  109. requestAnimationFrame(loop);
  110. render();
  111.  
  112. })();
  113.  
  114. })(jQuery();
  115.  
  116. </script>
  117. </body>
  118. </html>
Advertisement
Add Comment
Please, Sign In to add comment