Advertisement
Guest User

MyParalax

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