<?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: Steven Lei</title>
    <description>The latest articles on DEV Community by Steven Lei (@stevenlei).</description>
    <link>https://dev.to/stevenlei</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%2F737747%2Fa1b2bc44-f5a0-4c08-acff-b0c12d65ae75.jpeg</url>
      <title>DEV Community: Steven Lei</title>
      <link>https://dev.to/stevenlei</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stevenlei"/>
    <language>en</language>
    <item>
      <title>A JavaScript Utility for Measuring Spacing</title>
      <dc:creator>Steven Lei</dc:creator>
      <pubDate>Tue, 16 Nov 2021 04:05:49 +0000</pubDate>
      <link>https://dev.to/stevenlei/a-javascript-utility-for-measuring-spacing-4ne3</link>
      <guid>https://dev.to/stevenlei/a-javascript-utility-for-measuring-spacing-4ne3</guid>
      <description>&lt;p&gt;Hello, I am Steven. I just wrote a JavaScript utility named &lt;strong&gt;SpacingJS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As a frontend web developer, it is important to grasp the spacing between elements on a webpage. The DevTool can check the dimension, padding, margin, styles and more of an element, but it seems like it cannot measure the distance between elements like Sketch/Figma. So I have written this utility:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xS3JfR_W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afv2u3wqrkwlvi3vrr7n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xS3JfR_W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afv2u3wqrkwlvi3vrr7n.png" alt="SpacingJS" width="880" height="794"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can experience on the website:&lt;br&gt;
&lt;a href="https://spacingjs.com"&gt;https://spacingjs.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Besides including the script with a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag to your website, a Chrome Extension is available on the &lt;a href="https://chrome.google.com/webstore/detail/spacingjs/fhjegjndanjcamfldhenjnhnjheecgcc"&gt;Chrome Web Store&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The project is open-source and available here: &lt;a href="https://github.com/stevenlei/spacingjs"&gt;https://github.com/stevenlei/spacingjs&lt;/a&gt;. Would you please star it if you like it?:)&lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>design</category>
      <category>css</category>
    </item>
    <item>
      <title>Inspired by Apple's web animations, I have written a JavaScript library.</title>
      <dc:creator>Steven Lei</dc:creator>
      <pubDate>Wed, 27 Oct 2021 07:16:04 +0000</pubDate>
      <link>https://dev.to/stevenlei/inspired-by-apples-web-animations-i-have-written-a-javascript-library-2ll5</link>
      <guid>https://dev.to/stevenlei/inspired-by-apples-web-animations-i-have-written-a-javascript-library-2ll5</guid>
      <description>&lt;p&gt;Hello, I am Steven Lei.&lt;/p&gt;

&lt;p&gt;I love Apple products, and so do their website. They did a lot of fantastic animations for presenting their products, primarily scrolled-based, when we scroll the webpage up and down, the animation plays forward and backward.&lt;/p&gt;

&lt;p&gt;As a web developer, I tried to reproduce their animation work, learn from the process, and record it as screencasts.&lt;/p&gt;

&lt;p&gt;To achieve the scroll-based animation, we have to do some calculation work with JavaScript, like whether the element has appeared on screen, the scrolled distance, the element offset, and so forth. And we will update the element style accordingly.&lt;/p&gt;

&lt;p&gt;As they have the common requirements mentioned above, I wondered if this development process could be more straightforward. What if we can achieve the same goal without writing a single line of JavaScript?&lt;/p&gt;

&lt;h2&gt;
  
  
  Initial Design
&lt;/h2&gt;

&lt;p&gt;Scroll-based animations are based on the scrolling distance and position of elements. I would like to have a number when the element starts to appear from the bottom of the page, and disappear from the top. Numbers between &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt; would be simple enough, at least in this stage.&lt;/p&gt;

&lt;p&gt;I have used the &lt;code&gt;IntersectionObserver&lt;/code&gt; API to explore if the element is on-screen or not. And next, we have to think about how to use this number.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Flexibility
&lt;/h2&gt;

&lt;p&gt;As animation involves a variety of requirements, for instance, fading in an element, moving the element, changing the color of a text etc. We cannot limit the ability just on some of those. So I have decided to put the number as a CSS variable, this would be flexible enough for working out different animation scenarios.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Name of the Library
&lt;/h2&gt;

&lt;p&gt;I have started to develop the library and named it &lt;strong&gt;Trigger JS&lt;/strong&gt; because every scroll triggers a new value, just a literal meaning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thoughts on the usage of JavaScript
&lt;/h2&gt;

&lt;p&gt;I want to simplify the development process, so this library's purpose is to let developers create scroll-based animation without writing JavaScript. I have decided to use HTML attributes and CSS variables.&lt;/p&gt;

&lt;p&gt;As the library name is &lt;strong&gt;Trigger JS&lt;/strong&gt;, I have used &lt;code&gt;tg-&lt;/code&gt; as the prefix of the related HTML attributes. I know &lt;code&gt;tg-&lt;/code&gt; does not quite fulfil the standard of HTML5, that's okay, I will keep an option to set the prefix, for example, &lt;code&gt;data-tg-*&lt;/code&gt;, to completely get rid of this concern. But for now, I will keep using &lt;code&gt;tg-&lt;/code&gt; here first.&lt;/p&gt;

&lt;h2&gt;
  
  
  The First Attribute: &lt;code&gt;tg-name&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;tg-name&lt;/code&gt; HTML attribute is mainly for two purposes: elements to be monitored and the name of the CSS variable.&lt;/p&gt;

&lt;p&gt;We will add the &lt;code&gt;tg-name&lt;/code&gt; attribute to the element that we want to monitor, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;tg-name=&lt;/span&gt;&lt;span class="s"&gt;"opacity"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Hello World
&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Trigger JS&lt;/strong&gt; will fetch all HTML elements with &lt;code&gt;tg-name&lt;/code&gt; attribute into an array called &lt;code&gt;activeElements&lt;/code&gt; (via &lt;code&gt;document.querySelector('[tg-name'])&lt;/code&gt;), and observe them with &lt;code&gt;IntersectionObserver&lt;/code&gt; API.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;scroll&lt;/code&gt; event listener is attached to &lt;code&gt;window&lt;/code&gt;, which will calculate the relative position of elements in &lt;code&gt;activeElements&lt;/code&gt; within the browser window: &lt;code&gt;0&lt;/code&gt; when the element appears from the bottom, and &lt;code&gt;1&lt;/code&gt; when the element disappears from the top, and &lt;code&gt;0.5&lt;/code&gt; when it is in the middle.&lt;/p&gt;

&lt;p&gt;Finally, we will put this calculation result to the element with CSS variable, for instance, &lt;code&gt;--opacity: 0.5&lt;/code&gt;. So that we can use the value in the appropriate CSS property, in this example, &lt;code&gt;opacity: var(--opacity);&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Range Setting
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;0&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt; is excellent in some situations. But if we want to move an element with &lt;code&gt;transform&lt;/code&gt;, it would be good to adjust the start and end value (We can do the calculation in CSS with &lt;code&gt;calc()&lt;/code&gt; though, but let's make it simpler).&lt;/p&gt;

&lt;p&gt;That's why I have added &lt;code&gt;tg-from&lt;/code&gt; and &lt;code&gt;tg-to&lt;/code&gt;. If we want to move an element by &lt;code&gt;400px&lt;/code&gt; from the right to the left when scrolling up the page, we can set &lt;code&gt;tg-from="200"&lt;/code&gt; and &lt;code&gt;tg-to="-200"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And yes, we have to define the frequency of movement. We can set &lt;code&gt;tg-steps="400"&lt;/code&gt; so that the element is moving pixel-by-pixel. But what if we want to keep the increment to be &lt;code&gt;1&lt;/code&gt; but change the range of &lt;code&gt;tg-from&lt;/code&gt; and &lt;code&gt;tg-to&lt;/code&gt;? We can do the calculation ourselves or use another attribute &lt;code&gt;tg-step="1"&lt;/code&gt; to avoid doing the math.&lt;/p&gt;

&lt;h2&gt;
  
  
  Numbers are not Perfect
&lt;/h2&gt;

&lt;p&gt;Sometimes, we need an exact value. Let's say we want to change the text color, what we need is a color code instead.&lt;/p&gt;

&lt;p&gt;That's why &lt;code&gt;tg-map&lt;/code&gt; is being added, this gave us the ability to &lt;em&gt;convert&lt;/em&gt; a value from a number to a value. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt;
  &lt;span class="na"&gt;tg-name=&lt;/span&gt;&lt;span class="s"&gt;"color"&lt;/span&gt;
  &lt;span class="na"&gt;tg-from=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt;
  &lt;span class="na"&gt;tg-to=&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt;
  &lt;span class="na"&gt;tg-step=&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;
  &lt;span class="na"&gt;tg-map=&lt;/span&gt;&lt;span class="s"&gt;"0: black; 1: blue; 2: purple"&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Hello World
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The calculation value will be 0, 1 or 2 throughout the scroll, and convert to a final value with the setting in &lt;code&gt;tg-map&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; -&amp;gt; &lt;code&gt;black&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt; -&amp;gt; &lt;code&gt;blue&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;2&lt;/code&gt; -&amp;gt; &lt;code&gt;purple&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we can update the text color easily with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Noice Reduction
&lt;/h2&gt;

&lt;p&gt;Sometimes we are only interested in certain values. For example, we only want to know when &lt;code&gt;25&lt;/code&gt;, &lt;code&gt;50&lt;/code&gt;, &lt;code&gt;75&lt;/code&gt; happens from 0 to 100 (&lt;code&gt;tg-from="0"&lt;/code&gt; and &lt;code&gt;tg-to="100"&lt;/code&gt;). In this situation, &lt;code&gt;tg-filter&lt;/code&gt; takes part.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt;
  &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"heading"&lt;/span&gt;
  &lt;span class="na"&gt;tg-name=&lt;/span&gt;&lt;span class="s"&gt;"color"&lt;/span&gt;
  &lt;span class="na"&gt;tg-from=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt;
  &lt;span class="na"&gt;tg-to=&lt;/span&gt;&lt;span class="s"&gt;"100"&lt;/span&gt;
  &lt;span class="na"&gt;tg-step=&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;
  &lt;span class="na"&gt;tg-filter=&lt;/span&gt;&lt;span class="s"&gt;"25,50,75"&lt;/span&gt;
  &lt;span class="na"&gt;tg-map=&lt;/span&gt;&lt;span class="s"&gt;"25: red; 50: yellow; 75: green"&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Red (25), Yellow (50), Green (75)
&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100vh&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* In order to make the page have enough rooms for scrolling */&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;#heading&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Open Source
&lt;/h2&gt;

&lt;p&gt;The above is the introduction of creating this library, and this project is available on &lt;a href="https://github.com/triggerjs/trigger"&gt;GitHub - triggerjs/trigger&lt;/a&gt;. Please feel free to try out, submit pull requests and share your thoughts. Your opinion is precious.&lt;/p&gt;

&lt;p&gt;Please star it if you like it, and that would be great to have your participation.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>animation</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
