Advertisement
carr1396

parallax

Nov 1st, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Parallax</title>
  6. <link rel="stylesheet" type="text/css" href="css/parallax.css">
  7. </head>
  8. <body>
  9. <div>
  10. <ul class="parallax">
  11. <li class="parallax-section">
  12. <div class="parallax-bkg section1"></div>
  13. </li>
  14. <li class="parallax-section">
  15. <div class="parallax-bkg section2"></div>
  16. </li>
  17. <li class="parallax-section">
  18. <div class="parallax-bkg section3"></div>
  19. </li>
  20. <li class="parallax-section">
  21. <div class="parallax-bkg section4"></div>
  22. </li>
  23. </ul>
  24. </div>
  25. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  26. <script type="text/javascript" src="js/jquery-2.1.0.js"></script>
  27. <script type="text/javascript" src="js/modernizr-2.6.2.min.js"></script>
  28. <script type="text/javascript" src="js/parallax.js"></script>
  29. </body>
  30. </html>
  31.  
  32. *{
  33. padding: 0;
  34. margin: 0;
  35. }
  36.  
  37. ul{
  38.  
  39. }
  40. li{
  41. list-style: none;
  42. }
  43. .parallax{
  44. padding: 0;
  45. margin: 0;
  46. }
  47. .parallax-section{
  48. overflow: hidden;
  49. height: 100vh;
  50. width: 100vw;
  51. position: relative;
  52. }
  53. .parallax-bkg{
  54. height: 100vh;
  55. width: 100%;
  56. }
  57. .section1{
  58. background-image:url('../img/img2.jpg');
  59. background-size: cover;
  60. }
  61. .section2{
  62. background-image:url('../img/img3.jpg');
  63. background-size: cover;
  64. }
  65. .section3{
  66. background-image:url('../img/img4.jpeg');
  67. background-size: cover;
  68. }
  69. .section4{
  70. background-image:url('../img/img5.jpg');
  71. background-size: cover;
  72. }
  73.  
  74.  
  75.  
  76. /*function($){}(JQuery());*/
  77. $(document).ready(function(){
  78. /*alert("Document connected");*/
  79.  
  80. // shim layer with setTimeout fallback
  81. /*http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/*/
  82. window.requestAnimFrame = (function(){
  83. return window.requestAnimationFrame ||
  84. window.webkitRequestAnimationFrame ||
  85. window.mozRequestAnimationFrame ||
  86. function( callback ){
  87. window.setTimeout(callback, 1000 / 60);
  88. };
  89. })();
  90.  
  91.  
  92. /*default vraibales*/
  93. var $container =$(".parallax"),
  94. $sectionContainers=$container.find("div.parallax-bkg"),
  95. sectionType="li";
  96. var i,
  97. len,
  98. section,
  99. parentSection,
  100. offset,
  101. scroll
  102. ;
  103.  
  104. var scrollingElement=document.body;
  105. var top= scrollingElement.scrollTop;
  106. var heightOfSection=$sectionContainers.eq(0).closest(sectionType).height(),
  107. diffheight = $sectionContainers.eq(0).height()-heightOfSection
  108. ;
  109.  
  110.  
  111. console.log(diffheight);
  112. /*end default varibales*/
  113. var render=function(){
  114. /*console.log("We are now In Render function");*/
  115.  
  116. //lop through parralx sections
  117.  
  118. for (i = 0, len=$sectionContainers.length; i < len; i++) {
  119. /* console.log($sectionContainers[i]);*/
  120.  
  121. section=$sectionContainers[i];
  122. /*console.log(section);*/
  123. //get parent of section
  124. parentSection=section.parentNode;
  125. /*console.log(parentSection);*/
  126. //calculate the offsetTop of section
  127. //offset top is the area above current div that is it is the area from beginning of document to div top
  128. offset=$(section).offset().top;
  129. /*console.log(offset);*/
  130. //calculate amount of scroll
  131. scroll= Math.round(((top - offset)/heightOfSection)*diffheight);
  132. /*console.log(scroll);*/
  133.  
  134. //apply the scroll amount
  135. section.style.webkitTransform = "translate3d(0px, "+scroll+"px, 0px)";
  136. };
  137.  
  138. };
  139.  
  140. (function rendererLoop(){
  141. requestAnimationFrame(rendererLoop);
  142. //60 frames persec see Paul Irish's render function to add more browser compatiblity
  143. render();
  144. })();
  145.  
  146. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement