Advertisement
Guest User

Untitled

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