<?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: SERJ F.D</title>
    <description>The latest articles on DEV Community by SERJ F.D (@serjfd).</description>
    <link>https://dev.to/serjfd</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%2F975895%2F27c43562-24b9-4777-8b45-7b82635643bf.png</url>
      <title>DEV Community: SERJ F.D</title>
      <link>https://dev.to/serjfd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/serjfd"/>
    <language>en</language>
    <item>
      <title>Beautiful UI animation on scroll with React</title>
      <dc:creator>SERJ F.D</dc:creator>
      <pubDate>Sat, 26 Nov 2022 04:58:07 +0000</pubDate>
      <link>https://dev.to/serjfd/beautiful-ui-animation-on-scroll-with-react-4ndb</link>
      <guid>https://dev.to/serjfd/beautiful-ui-animation-on-scroll-with-react-4ndb</guid>
      <description>&lt;h1&gt;
  
  
  Want to create beautiful UI animation for your website? Here's 5 easy steps to accomplish that.
&lt;/h1&gt;

&lt;p&gt; &lt;br&gt;
Here are few easy steps to follow to create beautiful scroll animations.&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Install &lt;code&gt;react-intersection-observer&lt;/code&gt; from &lt;a href="https://github.com/thebuilder/react-intersection-observer" rel="noopener noreferrer"&gt;Github&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  2. Next step is to add &lt;code&gt;useInView&lt;/code&gt; function to your component that you want to animate, or to a component you want to control the animation with.
&lt;/h3&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  Create the &lt;code&gt;useInView&lt;/code&gt; function in your component and pass the &lt;code&gt;ref&lt;/code&gt; to element you wish to animate.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Function 
const { ref: elRef, inView: elVisible } = useInView()

// Element
&amp;lt;div ref={elRef}&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;br&gt;
Where &lt;code&gt;ref&lt;/code&gt; parameter is responsible for tracking the element, and &lt;code&gt;inView&lt;/code&gt; is responsible to let you know when the element is visible on the screen. &lt;code&gt;inView&lt;/code&gt; is either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
 &lt;br&gt;
Now that you have the state for when the element is visible on the screen, you can use that state to add dynamic &lt;code&gt;className&lt;/code&gt; or &lt;code&gt;style&lt;/code&gt;. You can pass it as prop to children elements to animate them, or use it directly in that component.&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  3. Use &lt;code&gt;inView&lt;/code&gt; state to add dynamic &lt;code&gt;className&lt;/code&gt; or &lt;code&gt;style&lt;/code&gt;.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This is an example of passing the `inView` state as a prop to child component.

// Parent Component
&amp;lt;div ref={elRef}&amp;gt;
   &amp;lt;ChildComponent isVisible={refVisible} /&amp;gt;
&amp;lt;/div&amp;gt;

// Child Component
const Component = ({ isVisible }) =&amp;gt; {
  return (
    &amp;lt;section className={`${s.el} ${isVisible ? s.animate : ""}`}&amp;gt;&amp;lt;/section&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;br&gt;
Once you added the class for animation you can go to your &lt;code&gt;styles.css&lt;/code&gt; and add some styles.&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  4. Add animation styles.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Animation */
.el.animate {
  animation: slideUp 1s ease 0s alternate forwards;
}

/* Keyframes */
@keyframes slideUp {
  0% {
    transform: translateY(100%);
  }
  100% {
    transform: translateY(0%);
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;br&gt;
You can also pass additional options to the &lt;code&gt;useInView&lt;/code&gt; to have more control over the element you wish to animate.&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  5.(OPTIONAL) Customise animation trigger event.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const { ref: elRef, inView: elVisible } = useInView({
    triggerOnce: true,
    threshold: 0.5,
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;br&gt;
This particular options are &lt;code&gt;triggerOnce&lt;/code&gt; - which essentially if set to &lt;code&gt;true&lt;/code&gt; triggers the animation only once, the first time you scroll the page, or if set to &lt;code&gt;false&lt;/code&gt; it will trigger each time the element is visible on the screen, like scrolling past the element and then back to it, will trigger the animation again.&lt;br&gt;
 &lt;br&gt;
Second options is &lt;code&gt;threshold&lt;/code&gt; - which is a value between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;, which are responsible for triggering the animation when a certain percentage of the element is visible on the screen, where &lt;code&gt;1&lt;/code&gt; is 100% visible and &lt;code&gt;0&lt;/code&gt; is 0% visible (which basically means when more than 0% of the element is visible - &lt;strong&gt;start the animation&lt;/strong&gt;).&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  6. Enjoy 🔥
&lt;/h3&gt;

&lt;p&gt; &lt;br&gt;
 &lt;br&gt;
&lt;strong&gt;Thanks for the read. Like, Share, Follow&lt;/strong&gt; 🙌&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
  </channel>
</rss>
