<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Geoffrey</title>
    <description>The latest articles on DEV Community by Geoffrey (@geosigno).</description>
    <link>https://dev.to/geosigno</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F173126%2F680d75f6-553f-434d-a570-faf92eb81fb3.jpeg</url>
      <title>DEV Community: Geoffrey</title>
      <link>https://dev.to/geosigno</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/geosigno"/>
    <language>en</language>
    <item>
      <title>Case study: create a parallax effect directly on &lt;img&gt; tags with JavaScript</title>
      <dc:creator>Geoffrey</dc:creator>
      <pubDate>Wed, 29 May 2019 13:26:19 +0000</pubDate>
      <link>https://dev.to/geosigno/case-study-create-a-parallax-effect-directly-on-img-tags-with-javascript-cje</link>
      <guid>https://dev.to/geosigno/case-study-create-a-parallax-effect-directly-on-img-tags-with-javascript-cje</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Parallax effects in web work only with the CSS background-image property, why is&lt;br&gt;
that? &lt;strong&gt;This is not really practical.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This was a starting point for me. I wanted to have a nice parallax effect to apply directly on image tags, for several reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is the most natural way to use images on the web&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;background-image&lt;/em&gt; property doesn’t support the equivalent of picture tag,
&lt;em&gt;srcset&lt;/em&gt; and &lt;em&gt;sizes&lt;/em&gt; attributes easily&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;background-image&lt;/em&gt; with CMS is not optimal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fact that I couldn’t find this kind of library/plugin anywhere was&lt;br&gt;
surprising, so** I decided to create a new one**.&lt;/p&gt;

&lt;h3&gt;
  
  
  Main objective
&lt;/h3&gt;

&lt;p&gt;I wanted to be able to add parallax effects without any change on HTML or CSS.&lt;/p&gt;

&lt;p&gt;The reason being I already had a website almost finished, and I didn’t see myself changing all my &lt;em&gt;&amp;lt; img&amp;gt;&lt;/em&gt; tags to &lt;em&gt;&amp;lt; div&amp;gt;&lt;/em&gt; with &lt;em&gt;background-image&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This was the main thread of this library, I wanted to have a very simple way to apply parallax on any website already on production without any rework. And as a bonus, a very smooth and natural animation feeling — the effect should only give a plus and not cost anything else.&lt;/p&gt;

&lt;h3&gt;
  
  
  1st Issue: How to not break the layout?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5uiaOAwx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/gpa5y29zkcto3j3naanv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5uiaOAwx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/gpa5y29zkcto3j3naanv.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first concern was to manage the transition of the image without breaking the layout. Usual parallax effects are located in a very specific area created for that purpose only, the main thread of this case is the opposite.&lt;/p&gt;

&lt;p&gt;Parallax should be easily added anywhere there is an image, even if its located between two text blocs. And you don’t want the image to be transitioned anywhere on the website and potentially overlap with content.&lt;/p&gt;

&lt;h4&gt;
  
  
  Solution
&lt;/h4&gt;

&lt;p&gt;I have therefore reached the conclusion to dynamically add a container as a parent of the image. This container will have the same dimensions as the image and a hidden overflow. Now the image can translate from an infinite number of pixels without breaking the layout.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src="image.jpg" alt="image" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;will become:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div style="overflow: hidden"&amp;gt;
    &amp;lt;img src="image.jpg" alt="image" /&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  2nd Issue: How to avoid blank spaces?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--74RUeA4d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/thipgev0b1h2h504eo1d.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--74RUeA4d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/thipgev0b1h2h504eo1d.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So a new problem appeared, blank spaces when the image reaches its physical limit.&lt;/p&gt;

&lt;p&gt;This was very problematic, considering that the whole point was to leave the initial layout of the page unaltered. Not to mention the smooth and natural animation initially planned.&lt;/p&gt;
&lt;h4&gt;
  
  
  Solution
&lt;/h4&gt;

&lt;p&gt;I have opted for this solution: add a scaling transformation on the image.&lt;br&gt;
Meaning the image will have more matter to be transitioned with. This range can be easily calculated by:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(imageHeight * scale - imageHeight) = range
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For example, if the image is 500px height, and we apply a 1.5 scale, that means the image will have a 250px range to translate on.&lt;/p&gt;

&lt;p&gt;Now we need to get the percentage of the image position comparing to the&lt;br&gt;
viewport, using a more laborious calculation:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;((viewportBottom - imageTop) / ((viewportHeight + imageHeight) / 100)) = percentage
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And finally transcript this percentage into the range:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;((percentage / 100) * range - range / 2) = translation
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So &lt;strong&gt;the translation can be applied gradually&lt;/strong&gt; on the image using the&lt;br&gt;
&lt;em&gt;transform: translate(translation);&lt;/em&gt; property.&lt;/p&gt;

&lt;h4&gt;
  
  
  Cons: quality of the image
&lt;/h4&gt;

&lt;p&gt;Theoretically, we can apprehend the fact that if the scale is applied to an image, we will lose quality.&lt;/p&gt;

&lt;p&gt;In practice, &lt;strong&gt;this is hardly noticeable&lt;/strong&gt; if the scale is set at 1.3 (which is the default value of the library). And even less if you cater for this by adding an image with a bigger size — meaning if your image is 500px and you want to apply a 1.5 scale, compensate by using a 750px width image.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final render
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fhn3oGzh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/i1vfwvwaao4nizzwl13u.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fhn3oGzh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/i1vfwvwaao4nizzwl13u.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Performances
&lt;/h3&gt;

&lt;p&gt;With parallax animations, comes &lt;strong&gt;performance warning&lt;/strong&gt;. Thanks to &lt;a href="https://www.paulirish.com/"&gt;Paul&lt;br&gt;
Irish&lt;/a&gt; and &lt;a href="https://www.html5rocks.com/"&gt;html5rocks&lt;/a&gt;, a lot of answers were already provided and explained to an extent. Still, there is a lot to do here, some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The scroll event is performance greedy, so the use of &lt;em&gt;Request Animation Frame&lt;/em&gt; is highly recommended. I shall not elaborate, as this subject has already been touched upon &lt;a href="https://css-tricks.com/using-requestanimationframe/"&gt;by others&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Intersection Observer API&lt;/em&gt; is really powerful and performance light to check which elements are visible in the viewport. Because there is no need to cater for images that are not in the current viewport.&lt;/li&gt;
&lt;li&gt;A key point is to reduce &lt;a href="https://gist.github.com/paulirish/5d52fb081b3570c81e3a"&gt;the
reflow&lt;/a&gt; of the browser as much as possible, one (of many) solution to this is to reduce as much as possible to fetch the viewport and element offsets.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;CSS Hardware Acceleration&lt;/em&gt; isn’t very well-know but yet powerful. Changing the &lt;em&gt;transform: translateX();&lt;/em&gt; to &lt;em&gt;transform: translate3D();&lt;/em&gt; will leverage the GPU power and offers &lt;a href="https://blog.teamtreehouse.com/increase-your-sites-performance-with-hardware-accelerated-css"&gt;better
performance&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Performances are constant challenges&lt;/strong&gt; against what has been done. And improve&lt;br&gt;
what can be improved thanks to new technologies or wrong initial implementation.&lt;/p&gt;




&lt;p&gt;This case study and this library &lt;strong&gt;only reflect my point of view&lt;/strong&gt;, which may&lt;br&gt;
not be the best. You are most welcome to challenge, debate or argue any of the&lt;br&gt;
things I said above.&lt;/p&gt;

&lt;p&gt;Last but not least, you can check the simpleParallax library on&lt;br&gt;
&lt;a href="https://simpleparallax.com/"&gt;simpleparallax.com&lt;/a&gt; and &lt;a href="https://github.com/geosigno/simpleParallax"&gt;github&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>css</category>
      <category>ux</category>
    </item>
  </channel>
</rss>
