{"id":728,"date":"2024-04-27T23:48:10","date_gmt":"2024-04-27T23:48:10","guid":{"rendered":"https:\/\/www.cssportal.com\/blog\/?p=728"},"modified":"2024-04-27T23:48:10","modified_gmt":"2024-04-27T23:48:10","slug":"css-loader-on-button-click","status":"publish","type":"post","link":"https:\/\/www.cssportal.com\/blog\/css-loader-on-button-click\/","title":{"rendered":"CSS Loader on Button Click"},"content":{"rendered":"<p>In today&#8217;s blog post, we&#8217;ll be looking at how to create a user-friendly and visually appealing experience by implementing CSS loaders on button clicks.  Ever clicked a button and felt stuck waiting for something to happen?  A well-designed loading animation can significantly improve user experience by providing visual feedback during actions that take time.  Not only do loaders enhance user experience, but they&#8217;re also surprisingly easy to create using CSS!  Let&#8217;s get started and explore how to add these interactive elements to your buttons.<\/p>\n<h3>The HTML Code<\/h3>\n<p>Now that we understand the benefits of using CSS loaders on buttons, let&#39;s set up the basic structure using HTML.  The code we&#39;ll be using is quite simple:<\/p>\n<div class=\"headerblog\">HTML<\/div>\n<pre><code class=\"language-html\">&lt;button type=&quot;submit&quot; class=&quot;loaderbutton&quot; id=&quot;submit&quot;&gt;Click Me!&lt;\/button&gt;\r\n<\/code><\/pre>\n<p>This code snippet defines a button element with the following attributes:<\/p>\n<ul>\n<li><strong>type=&quot;submit&quot;<\/strong>: This specifies that the button is intended for submitting a form (optional and can be omitted if not submitting a form).<\/li>\n<li><strong>class=&quot;loaderbutton&quot;<\/strong>: We&#39;ve assigned a class named &quot;loaderbutton&quot; to the button. This class will be used later in our CSS styles to target and style the button, including the loader animation.<\/li>\n<li><strong>id=&quot;submit&quot;<\/strong>: An id attribute is assigned with the value &quot;submit&quot;. While not strictly necessary for this example, IDs can be useful for more specific targeting with JavaScript if needed.<\/li>\n<\/ul>\n<p>The text content within the opening and closing <code>&lt;button&gt;<\/code> tags defines the label displayed on the button (&quot;Click Me!&quot; in this case).<\/p>\n<p>In the next section, we&#39;ll look at the CSS to create the actual loading animation for this button.<\/p>\n<h3>The CSS Code<\/h3>\n<p>We&#39;ve established the basic button structure with HTML. Now, let&#39;s bring it to life with some CSS magic! Here&#39;s the code that will style the button and create the loading animation:<\/p>\n<div class=\"headerblog\">CSS<\/div>\n<pre><code class=\"language-css\">.loaderbutton {\r\n  padding: 10px 30px;\r\n  background: #3e52c1;\r\n  border: none;\r\n  color: #fff;\r\n  border-radius: 5px;\r\n  cursor: pointer;\r\n  font-size: 1.2em;\r\n  position: relative;\r\n  display: block;\r\n}\r\n\r\n.loaderbutton::after {\r\n  content: &#39;&#39;;\r\n  display: block;\r\n  width: 1.1em;\r\n  height: 1.1em;\r\n  position: absolute;\r\n  left: calc(50% - 0.75em);\r\n  top: calc(50% - 0.75em);\r\n  border: 0.2em solid transparent;\r\n  border-right-color: white;\r\n  border-radius: 50%;\r\n  animation: loader-animation 0.7s linear infinite;\r\n  opacity: 0;\r\n}\r\n\r\n@keyframes loader-animation {\r\n  from {\r\n    transform: rotate(0);\r\n  }\r\n  to {\r\n    transform: rotate(360deg);\r\n  }\r\n}\r\n\r\n.loaderbutton.loading {\r\n  color: transparent;\r\n}\r\n\r\n.loaderbutton.loading::after {\r\n  opacity: 1;\r\n}\r\n<\/code><\/pre>\n<p>Let&#39;s break down what this code does:<\/p>\n<ol>\n<li><strong>Styling the Button (.loaderbutton)<\/strong>:<\/li>\n<\/ol>\n<ul>\n<li>This block defines styles for the button element with the class &quot;loaderbutton&quot;. <\/li>\n<li>Properties like <code>padding<\/code>, <code>background<\/code>, <code>border<\/code>, <code>color<\/code>, and <code>font-size<\/code> define the button&#39;s appearance.<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>Creating the Loader Element (.loaderbutton::after)<\/strong>:<\/li>\n<\/ol>\n<ul>\n<li>This uses the CSS pseudo-element <code>::after<\/code> to create a hidden element positioned within the button.<\/li>\n<li>We define its size, position, and a border that creates a circle using <code>border-radius<\/code>.<\/li>\n<li>The <code>animation<\/code> property references the <code>loader-animation<\/code> keyframes to create the rotation effect.<\/li>\n<li>Initially, the opacity is set to 0 (<code>opacity: 0<\/code>), making it invisible.<\/li>\n<\/ul>\n<ol start=\"3\">\n<li><strong>Animation Definition (@keyframes loader-animation)<\/strong>:<\/li>\n<\/ol>\n<ul>\n<li>This defines the animation named <code>loader-animation<\/code>.<\/li>\n<li>It specifies a rotation from 0 degrees to 360 degrees, creating a spinning motion. <\/li>\n<li>The animation duration is set to 0.7 seconds with a linear timing function (<code>0.7s linear<\/code>).<\/li>\n<\/ul>\n<ol start=\"4\">\n<li><strong>Activating the Loader (.loaderbutton.loading)<\/strong>:<\/li>\n<\/ol>\n<ul>\n<li>This block targets the button element with the class &quot;loaderbutton&quot; that also has the additional class &quot;loading&quot;.<\/li>\n<li>When this class is added, the button text color becomes transparent (<code>color: transparent<\/code>).<\/li>\n<li>This allows the loader animation to become visible.<\/li>\n<\/ul>\n<ol start=\"5\">\n<li><strong>Showing the Loader (.loaderbutton.loading::after)<\/strong>:<\/li>\n<\/ol>\n<ul>\n<li>This targets the pseudo-element within the button that has both &quot;loaderbutton&quot; and &quot;loading&quot; classes.<\/li>\n<li>When the &quot;loading&quot; class is added, the opacity is set to 1 (<code>opacity: 1<\/code>), making the loader animation visible.<\/li>\n<\/ul>\n<p>By combining these styles, we achieve a button that displays a loading animation (spinning circle) when the &quot;loading&quot; class is added. In the next section, we&#39;ll explore how to use JavaScript to control this behavior based on button clicks.<\/p>\n<h3>The JavaScript Code<\/h3>\n<p>We&#39;ve styled the button and created the loading animation using HTML and CSS. Now, it&#39;s time to make it interactive! JavaScript will allow us to control the loader animation based on user interaction (clicking the button). Here&#39;s the code that brings it all together:<\/p>\n<div class=\"headerblog\">Javascript<\/div>\n<pre><code class=\"language-javascript\">const animBtn = document.getElementById(&quot;submit&quot;);\r\n\r\nanimBtn.addEventListener(&#39;click&#39;, () =&gt; {\r\n  animBtn.classList.add(&quot;loading&quot;);\r\n  \/\/The following is used for demo purposes only\/\/\r\n  setTimeout(() =&gt; animBtn.classList.remove(&quot;loading&quot;), 5000);\r\n});\r\n<\/code><\/pre>\n<p>Let&#39;s understand what this code does:<\/p>\n<ol>\n<li><strong>Get Button Reference (const animBtn)<\/strong>:\n<ul>\n<li>This line uses <code>document.getElementById(&quot;submit&quot;)<\/code> to select the button element with the ID &quot;submit&quot; and assigns it to a constant variable named <code>animBtn<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Add Click Event Listener<\/strong>:\n<ul>\n<li>This line uses <code>animBtn.addEventListener(&#39;click&#39;, () =&gt; { ... })<\/code> to attach a click event listener to the button element referenced by <code>animBtn<\/code>.<\/li>\n<li>The part within the curly braces (<code>{}<\/code>) is a function that will be executed when the button is clicked.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Add and Remove Loading Class<\/strong>:\n<ul>\n<li>Inside the event listener function, we use <code>animBtn.classList.add(&quot;loading&quot;)<\/code> to add the class &quot;loading&quot; to the button element.<\/li>\n<li>This class addition triggers the CSS styles defined earlier to show the loader animation and hide the button text.<\/li>\n<li><strong>Important Note:<\/strong> The commented line (<code>\/\/The following is used for demo purposes only\/\/<\/code>) uses <code>setTimeout<\/code> to simulate a 5-second delay before removing the &quot;loading&quot; class. This is for demonstration purposes only. In your actual application, the button should return to its normal state after the intended action is complete.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>The Result<\/h3>\n<p>Now that we&#39;ve implemented all the code snippets, let&#39;s see the final outcome!  With this code in place, clicking the button will trigger the following sequence:<\/p>\n<p><iframe src=\"\/blog-examples\/buttonloader.php\" height=\"80\"><\/iframe><\/p>\n<ol>\n<li><strong>Button Clicked:<\/strong> Clicking the button fires the event listener function we defined in JavaScript.<\/li>\n<li><strong>&quot;Loading&quot; Class Added:<\/strong> The JavaScript code adds the class &quot;loading&quot; to the button element.<\/li>\n<li><strong>CSS Activated:<\/strong>  The CSS style associated with the &quot;loading&quot; class takes effect.<\/li>\n<li><strong>Loader Animation Shown:<\/strong> The hidden loader animation element becomes visible due to a change in opacity. <\/li>\n<li><strong>Button Text Hidden:<\/strong> The button text color becomes transparent, effectively hiding the original button text.<\/li>\n<\/ol>\n<p><strong>Remember:<\/strong> The commented line in the JavaScript code using <code>setTimeout<\/code> is for demonstration only. In your application, you&#39;ll want to remove the &quot;loading&quot; class after your intended action is complete (e.g., data submission is finished). This will make the button return to its normal state.<\/p>\n<p>By following these steps, you&#39;ve successfully created a user-friendly button that provides visual feedback using a CSS loader animation during clicks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s blog post, we&#8217;ll be looking at how to create a user-friendly and visually appealing experience by implementing CSS loaders on button clicks. Ever clicked a button and felt stuck waiting for something to happen? A well-designed loading animation can significantly improve user experience by providing visual feedback during actions that take time. Not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":729,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,7,3],"tags":[],"class_list":["post-728","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","category-html","category-javascript","wpautop"],"_links":{"self":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/comments?post=728"}],"version-history":[{"count":1,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/728\/revisions"}],"predecessor-version":[{"id":730,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/posts\/728\/revisions\/730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/media\/729"}],"wp:attachment":[{"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/media?parent=728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/categories?post=728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cssportal.com\/blog\/wp-json\/wp\/v2\/tags?post=728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}