Advertisement
migsmorre

Untitled

Aug 20th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Untitled Document</title>
  5.  
  6. <style type="text/css">
  7. body{
  8. margin:0;
  9. padding:0;
  10.  
  11. }
  12. ul{
  13. margin-top:0px;
  14. margin-left:25px;
  15. margin-right:25px;
  16. padding-top:0px;
  17.  
  18. }
  19. ul li{
  20. list-style:none;
  21. overflow:hidden;
  22. height:500px;
  23.  
  24. }
  25. .parallax-background{
  26. height:800px;
  27. }
  28. .image1{
  29. background-image:url(images/image1.jpg);
  30. background-position:50% 20%;
  31. background-size:cover;
  32.  
  33.  
  34. }
  35. .image2{
  36. background-image:url(images/image2.jpg);
  37. background-size:cover;
  38. background-position:50% 10%;
  39.  
  40.  
  41.  
  42. }
  43. .image3{
  44. background-image:url(images/image3.jpg);
  45. background-size:cover;
  46.  
  47. }
  48. .image4{
  49. background-image:url(images/image4.jpg);
  50. background-size:cover;
  51. background-position:50% 10%;
  52.  
  53.  
  54.  
  55. }
  56.  
  57. </style>
  58. </head>
  59.  
  60. <body>
  61. <ul class="parallax">
  62. <li>
  63. <div class="parallax-background image1" ></div>
  64. </li>
  65. <li>
  66. <div class="parallax-background image2"></div>
  67. </li>
  68. <li>
  69. <div class="parallax-background image3"></div>
  70. </li>
  71. <li>
  72. <div class="parallax-background image4"></div>
  73. </li>
  74. </ul>
  75. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" >
  76. </script>
  77. <script>
  78.  
  79. $(document).ready(function(){
  80. var $container=$(".parallax");
  81. var $divs = $container.find("div.parallax-background");
  82. var thingBeingScrolled= document.body;
  83. var liHeight=$divs.eq(0).closest("li").height();
  84. var diffHeight=$divs.eq(0).height() - liHeight;
  85.  
  86. var i,len,div,li,offset,myScroll,top;
  87.  
  88. var render=function(){
  89. //loop thur divs
  90. top=thingBeingScrolled.scrollTop;
  91. for(i=0;len<$divs.length;i++){
  92. //get one div
  93. div =$divs[i];
  94.  
  95. //get the parent li
  96. li=div.parentNode;
  97.  
  98. //calcute the offset top of div
  99. offset=$(div).offset().top;
  100.  
  101. //calculate the amount to scroll
  102.  
  103. myScroll=Math.round(((top - offset)/ liHeight)* diffHeight);
  104.  
  105. //apply the scroll amounth
  106. div.style.webkitTransform = "translate3d(0px,"+ myScroll+"px, 0px)";
  107.  
  108. }
  109. };
  110. (function loop(){
  111. requestAnimationFrame(loop);
  112. render();
  113. })();
  114.  
  115. });
  116. </script>
  117. </body>
  118. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement