A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. title: Pure CSS Parallax Websites
  2. url: http://keithclark.co.uk/articles/pure-css-parallax-websites/
  3. hash_url: bdea3b9e4326f5709a02c72c720367db
  4. <p>This article demonstrates how to use CSS transforms, perspective and some scaling trickery to create a pure CSS parallax scrolling website.</p>
  5. <p>Parallax is almost always handled with JavaScript and, more often than not, it's implemented badly with the worst offenders listening for the <code>scroll</code> event and modifying the DOM directly in the handler, triggering needless reflows and paints. All this happens out of sync with the browsers rendering pipeline causing dropped frames and stuttering. It's not all bad though, <code>requestAnimationFrame</code> and deferring DOM updates can transform parallax websites - but what if you could remove the JavaScript dependency completely?</p>
  6. <p>Deferring the parallax effect to CSS removes all these issues and allows the browser to leverage hardware acceleration resulting in almost everything being handled by the compositor. The result is consistent frame rates and perfectly smooth scrolling. You can also combine the effect with other CSS features such as <a href="http://www.w3.org/TR/css3-mediaqueries/">media queries</a> or <a href="http://www.w3.org/TR/css3-conditional/">supports</a> - responsive parallax anyone?</p>
  7. <p><a class="button" href="http://keithclark.co.uk/articles/pure-css-parallax-websites/demo3/">Try the demo</a></p>
  8. <h2>The theory</h2>
  9. <p>Before we dive into how the effect works, let's establish some barebones markup:</p>
  10. <pre class="figure__item codeblock"><code class="code code--html">&lt;div class="parallax"&gt;
  11. &lt;div class="parallax__layer parallax__layer--back"&gt;
  12. ...
  13. &lt;/div&gt;
  14. &lt;div class="parallax__layer parallax__layer--base"&gt;
  15. ...
  16. &lt;/div&gt;
  17. &lt;/div&gt;</code></pre>
  18. <p>And here are the basic style rules:</p>
  19. <pre class="figure__item codeblock"><code class="code code--css">.parallax {
  20. perspective: 1px;
  21. height: 100vh;
  22. overflow-x: hidden;
  23. overflow-y: auto;
  24. }
  25. .parallax__layer {
  26. position: absolute;
  27. top: 0;
  28. right: 0;
  29. bottom: 0;
  30. left: 0;
  31. }
  32. .parallax__layer--base {
  33. transform: translateZ(0);
  34. }
  35. .parallax__layer--back {
  36. transform: translateZ(-1px);
  37. }</code></pre>
  38. <p>The <code>parallax</code> class is where the parallax magic happens. Defining the <code>height</code> and <code>perspective</code> style properties of an element will lock the perspective to its centre, creating a fixed origin 3D viewport. Setting <code>overflow-y: auto</code> will allow the content inside the element to scroll in the usual way, but now descendant elements will be rendered relative to the fixed perspective. This is the key to creating the parallax effect.</p>
  39. <p>Next is the <code>parallax__layer</code> class. As the name suggests, it defines a layer of content to which the parallax effect will be applied; the element is pulled out of content flow and configured to fill the space of the container.</p>
  40. <p>Finally we have the modifier classes <code>parallax__layer--base</code> and <code>parallax__layer--back</code>. These are used to determine the scrolling speed of a parallax element by translating it along the Z axis (moving it farther away, or closer to the viewport). For brevity I have only defined two layer speeds - we'll add more later.</p>
  41. <p><a class="button" href="http://keithclark.co.uk/articles/pure-css-parallax-websites/demo1/">Try it</a></p>
  42. <h2>Depth correction</h2>
  43. <p>Since the parallax effect is created using 3D transforms, translating an element along the Z axis has a side effect - its effective size changes as we move it closer to or farther away from the viewport. To counter this we need to apply a <code>scale()</code> transform to the element so that it appears to be rendered at its original size:</p>
  44. <pre class="figure__item codeblock"><code class="code code--css">.parallax__layer--back {
  45. transform: translateZ(-1px) scale(2);
  46. }</code></pre>
  47. <p>The scale factor can be calculated with <code>1 + (translateZ * -1) / perspective</code>. For example, if our viewport <code>perspective</code> is set to <code>1px</code> and we translate an element <code>-2px</code> along the Z axis the correction scale factor would be 3:</p>
  48. <pre class="figure__item codeblock"><code class="code code--css">.parallax__layer--deep {
  49. transform: translateZ(-2px) scale(3);
  50. }</code></pre>
  51. <p><a class="button" href="http://keithclark.co.uk/articles/pure-css-parallax-websites/demo2/">Try it depth corrected</a></p>
  52. <h2>Controlling layer speed</h2>
  53. <p>Layer speed is controlled by a combination of the perspective and the Z translation values. Elements with negative Z values will scroll slower than those with a positive value. The further the value is from <code>0</code> the more pronounced the parallax effect (i.e. <code>translateZ(-10px)</code> will scroll slower than <code>translateZ(-1px)</code>).</p>
  54. <h2>Parallax sections</h2>
  55. <p>The previous examples demonstrated the basic techniques using very simple content but most parallax sites break the page into distinct sections where different effects can be applied. Here's how to do that.</p>
  56. <p>Firstly, we need a <code>parallax__group</code> element to group our layers together:</p>
  57. <pre class="figure__item codeblock"><code class="code code--html">&lt;div class="parallax"&gt;
  58. &lt;div class="parallax__group"&gt;
  59. &lt;div class="parallax__layer parallax__layer--back"&gt;
  60. ...
  61. &lt;/div&gt;
  62. &lt;div class="parallax__layer parallax__layer--base"&gt;
  63. ...
  64. &lt;/div&gt;
  65. &lt;/div&gt;
  66. &lt;div class="parallax__group"&gt;
  67. ...
  68. &lt;/div&gt;
  69. &lt;/div&gt;</code></pre>
  70. <p>Here's the CSS for the group element:</p>
  71. <pre class="figure__item codeblock"><code class="code code--css">.parallax__group {
  72. position: relative;
  73. height: 100vh;
  74. transform-style: preserve-3d;
  75. }</code></pre>
  76. <p>In this example, I want each group to fill the viewport so I've set <code>height: 100vh</code>, however arbitrary values can be set for each group if required. <code>transform-style: preserve-3d</code> prevents the browser flattening the <code>parallax__layer</code> elements and <code>position: relative</code> is used to allow the child <code>parallax__layer</code> elements to be positioned relative to the group element.</p>
  77. <p>One important rule to keep in mind when grouping elements is, we cannot clip the content of a group. Setting <code>overflow: hidden</code> on a <code>parallax__group</code> will break the parallax effect. Unclipped content will result in descendant elements overflowing, so we need to be creative with the <code>z-index</code> values of the groups to ensure content is correctly revealed/hidden as the visitor scrolls through the document.</p>
  78. <p>There are no hard and fast rules for dealing with layering as implementations will differ between designs. It's much easier to debug layering issues if you can see how the parallax effect works - you can do that by applying simple transform to the group elements:</p>
  79. <pre class="figure__item codeblock"><code class="code code--css">.parallax__group {
  80. transform: translate3d(700px, 0, -800px) rotateY(30deg);
  81. }</code></pre>
  82. <p>Have a look at the following example - note the <strong>debug</strong> option!</p>
  83. <p><a class="button" href="http://keithclark.co.uk/articles/pure-css-parallax-websites/demo3/">Try it with groups</a></p>
  84. <h2>Browser support</h2>
  85. <ul class="bullet-list">
  86. <li>Firefox, Safari, Opera and Chrome all support this effect.</li>
  87. <li>Firefox works too but there is currently a minor issue with alignment.</li>
  88. <li>IE doesn't support <code>preserve-3d</code> yet (it's coming) so the parallax effect won't work. That's ok though, you should still design your content to work without the parallax effect - You know, progressive enhancement and all that!</li>
  89. </ul>
  90. <hr/>
  91. <h2>Update: 25 Feb 2015</h2>
  92. <p>Since writing this article it's become apparent that webkit based browsers don't correctly calculate the effective width of an element once transformed into 3D space and scaled up. This bug allows users to scroll content horiziontally ignoring any constraits defined with <code>overflow</code> property. It is possible to work around for this bug by anchoring the <code>transform-origin</code> and <code>perspective-origin</code> to the right hand side of the viewport:</p>
  93. <pre class="figure__item codeblock"><code class="code code--css">.parallax {
  94. perspective-origin-x: 100%;
  95. }
  96. .parallax__layer {
  97. transform-origin-x: 100%;
  98. }</code></pre>
  99. <p>Locking the perspective and transform origins in this way causes any content overflow to occur off the left side of the screen. Since it's not possible to scroll to a negative position the overflow issue is masked.</p>
  100. <p><a class="button" href="http://keithclark.co.uk/articles/pure-css-parallax-websites/demo3-webkit-overflow-fix/">Try the webkit fix</a></p>