Advertisement
Guest User

parallax

a guest
Sep 13th, 2014
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.55 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.  
  6.     <style type="text/css">
  7.     body {
  8.         margin: 0;
  9.         padding: 0;
  10.     }
  11.     ul {
  12.         margin: 0;
  13.         padding: 0;
  14.     }
  15.     ul li {
  16.         list-style: none;
  17.         overflow: hidden;
  18.         height: 600px;
  19.     }
  20.     .parallax-background {
  21.         height: 700px;
  22.     }
  23.     .flower {
  24.         background-image: url('flower.jpg');
  25.         background-position: 50% 100%;
  26.     }
  27.     .lens {
  28.         background-image: url('lens.jpg');
  29.         background-size: cover;
  30.         background-position: 50% 80%;
  31.     }
  32.     .beach {
  33.         background-image: url('beach.jpg');
  34.         background-position: 50% 80%;
  35.     }
  36.     .wolf {
  37.         background-image: url('wolf.jpg');
  38.         background-size: cover;
  39.         background-position: 50% 80%;
  40.     }
  41.     </style>
  42. </head>
  43.  
  44. <body>
  45.     <ul>
  46.         <li>
  47.             <div class="parallax-background flower">
  48.             </div>
  49.         </li>
  50.         <li>
  51.             <div class="parallax-background lens">
  52.             </div>
  53.         </li>
  54.         <li>
  55.             <div class="parallax-background beach">
  56.             </div>
  57.         </li>
  58.         <li>
  59.             <div class="parallax-background wolf">
  60.             </div>
  61.         </li>
  62.     </ul>
  63.     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
  64.     (function($) {
  65.  
  66.         var $container = $(".parallax");
  67.         var $divs = $container.find("div.parallax-background");
  68.         var thingBeingScrolled = document.body;
  69.         var liHeight = $divs.eq(0).closest("li").height();
  70.         var diffHeight = $divs.eq(0).height() - liHeight;
  71.         var i, len, div, li, offset, scroll;
  72.         var render = function() {
  73.             // things were scrolling
  74.             top = thingBeingScrolled.scrollTop;
  75.             for (i - 0, len - $divs.length; i < len; i++) {
  76.                // get one div
  77.                div = $divs[i]
  78.                // get the parent li
  79.                li = div.parentNode;
  80.                // calculate the offsetTop of div
  81.                offset = $(div).offset().top;
  82.                // calculate the amount to scroll
  83.                scroll = Math.round(((top - offset) / liHeight) * diffHeight);
  84.                // apply the scroll amount
  85.                div.style.webkitTransform = "translate3d(0px," + scroll + "px,0px)";
  86.            }
  87.        };
  88.  
  89.  
  90.        (function loop() {
  91.            requestAnimationFrame(loop);
  92.            render();
  93.        })();
  94.    })(jQuery);
  95.    </script>
  96. </body>
  97.  
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement