CSS Animations: How to make Add to Cart Button Animated [2022]

by stackdeveloping
62 comments 487 views
add-to-cart-button

Contents

Create Shopping cart JavaScript Add to Cart Button Animated

As everyone now, in this kind of industry, e-Commerce application get a large space in the market of web developing. Today we discover a unique design for your  Add To Cart button html css. Everyone can try to design “buy now button for WooCommerce” or Shopify add to cart. We think that your e-commerce website will look different by using this design of button. In this article we have used HTML Code, CSS and CSS3 also Javascript.

HTML CSS Preview step adding

HTML Step

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <span class="cart__button">
          <span class="add__to-cart">Add to cart</span>
          <span class="added">Added</span>
          <i class="fas fa-shopping-cart"></i>
          <i class="fas fa-box"></i>
    </span>    
</body>
<script src="https://kit.fontawesome.com/3431c04d0c.js"></script>
<script src="main.js"></script>
</html>

In this section we have create only the part of code where we will design, only the button. For that we have gave to each element class attributes for representing the effect of each element. Also similar project you can find on by searching – add to cart javascript w3schools.

CSS Step

@import url(https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap);

*{
    margin: 0;
    padding:0;
    box-sizing: border-box;
}
body{
  font-family:'Poppins', sans-serif;
  background-color: hsl(0, 0%, 98%);
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items:center ;
}
.cart__button{
    position: relative; 
    width: 200px;
    height: 62px;
    background-color: hsl(250, 69%, 61%);
    border-radius: 10px;
    font-size: 16px;
    font-weight: 500;
    color: #fff;
    cursor: pointer;
    overflow: hidden;
    transition:  .3s ease-in-out;
}
.cart__button:hover{
    background-color: hsl(250, 57%, 53%);
}
.cart__button:active{
    transform: scale(.9);
}

.fa-shopping-cart  {
    position: absolute;
    z-index: 2;
    top:50%;
    left: -10%;
    transform: translate(-50%, -50%);
    font-size: 24px;
     
}

.fa-box{
   position: absolute;
   z-index: 3;
   top: -20%;
   left: 52%;
   transform:translate(-50%, -50%) ; 
   font-size: 14px;
    
}

.cart__button span{
    position: absolute;
    left: 50%;
    top: 50%;
    z-index:3;
    transform: translate(-50%, -50%);
}
.added{
    opacity: 0;
}
.add__to-cart{
    opacity: 1;
}
.cart__button.clicked .fa-shopping-cart{
    animation: cart 1.5s ease-in-out forwards;
}

.cart__button.clicked .fa-box{
    animation: box 1.5s ease-in-out forwards;
}

.cart__button.clicked .add__to-cart{
    animation: txt1 1.5s ease-in-out forwards;
}
 
.cart__button.clicked .added{
    animation: txt2 1.5s ease-in-out forwards;
}

@keyframes cart {
    0% {
        left: -10%;
    }
    40%, 60% {
        left: 50%;
    }
    100%{
        left:110%;
    }
}
@keyframes box{
    0%, 40% {
      top: -20%;  
    }
    60% {
        top:40%;
        left: 52%;
    }
    100% {
        top:40%;
        left: 112%;
    }
}

@keyframes txt1 {
    0% {
        opacity: 1;
    }
    20%, 100% {
        opacity: 0;
    }
}
@keyframes txt2 {
    0%, 80%{
        opacity: 0;
    }
     100% {
        opacity: 1;
    }
}

In this part we have created a custom design for our button with some transition effect, also we have created 4 animations effect  which they are cart, boc, txt1 and txt2 after we have declared the part of animation we used to managed using @keyframes part of CSS3.  All these effect will be visible only if we have attributed our elements with “clicked” class.

JavaScript Code

const cartButton = document.querySelectorAll('.cart__button');
cartButton.forEach(button=>{
    button.addEventListener('click', cartClick);
});

function cartClick(){
    let button= this;
    button.classList.add('clicked');
     setTimeout(()=>button.classList.remove('clicked'), 3000);
}

In this part of code, we have selected all children  elements inside cart__button class element, using forEach loop function that its self return what happen if we apply an event listener. So if we click on this button – button.classList.add(‘clicked’); – so all 4 animated elements will override with the class .clicked.

Check also the animation in the video.

Add to Cart Animated Button

Do you want to see this button live Try It now

#add to cart button html w3schools#add to cart html css #add to cart html code

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

62 comments

house porn December 22, 2023 - 3:22 pm

daxktilogibigibi.vnVPxAZLQEu8

Reply
agritourist December 29, 2023 - 7:44 pm

xyandanxvurulmus.qxKVTH3Dht3x

Reply
leeriest December 29, 2023 - 9:13 pm

xbunedirloooo.2MgGTBlHmeTK

Reply
calalu December 30, 2023 - 8:25 am

calalu xyandanxvurulmus.62eg5NNC1deg

Reply
romanticises January 1, 2024 - 8:44 am

romanticises xyandanxvurulmus.MrdHiv7EiVev

Reply
Sidney January 27, 2024 - 10:38 am

– Я не получаю то, что хочу.
– Не хватает мотивации на регулярные занятия.

– Мои цели сбываются у других людей.

Почему так, проясни у опсуимолога!

Вбивай в поиск: “опсуимолог” и получи актуальные контакты.

Ты же знаешь кто такой опсуимолог?

my page; Опсуимолог

Reply
Estert January 28, 2024 - 4:12 am

You will definitely like it https://zetds.seychellesyoga.com/info

Reply
Violat January 28, 2024 - 9:52 am

Content for your website https://ztd.bardou.online/adm

Reply
Estert January 28, 2024 - 3:55 pm

Web Development Wizards https://ztd.bardou.online/adm

Reply
Mollyt January 28, 2024 - 10:01 pm

Can provide a link mass to your website https://ztd.bardou.online/adm

Reply
Lydiat January 29, 2024 - 4:07 am

Your site’s position in the search results https://ztd.bardou.online/adm

Reply
Madelinet January 29, 2024 - 10:21 am

Free analysis of your website https://ztd.bardou.online/adm

Reply
Arielt January 29, 2024 - 4:31 pm

SEO Optimizers Team https://ztd.bardou.online/adm

Reply
Sharont January 29, 2024 - 10:39 pm

I offer mutually beneficial cooperation https://ztd.bardou.online/adm

Reply
Monicat January 30, 2024 - 4:45 am

Cool website. There is a suggestion https://ztd.bardou.online/adm

Reply
Judyt January 30, 2024 - 10:53 am

I really liked your site. Do you mind https://ztd.bardou.online/adm

Reply
Kimberlyt January 30, 2024 - 5:14 pm

Here’s what I can offer for the near future https://ztd.bardou.online/adm

Reply
Sheilat January 30, 2024 - 9:39 pm

Content for your website https://ztd.bardou.online/adm

Reply
Mollyt January 31, 2024 - 3:24 am

Web Development Wizards https://ztd.bardou.online/adm

Reply
Rebeccat January 31, 2024 - 9:17 am

Can provide a link mass to your website https://ztd.bardou.online/adm

Reply
Stellat January 31, 2024 - 3:18 pm

Your site’s position in the search results https://ztd.bardou.online/adm

Reply
Stacyt January 31, 2024 - 9:12 pm

Free analysis of your website https://ztd.bardou.online/adm

Reply
Irist February 1, 2024 - 2:57 am

SEO Optimizers Team https://ztd.bardou.online/adm

Reply
Peggyt February 1, 2024 - 8:40 am

I offer mutually beneficial cooperation https://ztd.bardou.online/adm

Reply
nhrhuey February 1, 2024 - 11:43 am Reply
mmhtpjf February 1, 2024 - 11:47 am Reply
Carolinet February 1, 2024 - 2:30 pm

Cool website. There is a suggestion https://ztd.bardou.online/adm

Reply
Beatricet February 1, 2024 - 5:47 pm

Content for your website http://myngirls.online/

Reply
Laurat February 1, 2024 - 11:39 pm

Web Development Wizards http://myngirls.online/

Reply
Susant February 2, 2024 - 5:22 am

Can provide a link mass to your website http://myngirls.online/

Reply
Gloriat February 2, 2024 - 11:08 am

Your site’s position in the search results http://myngirls.online/

Reply
Norat February 3, 2024 - 12:54 am

Free analysis of your website http://myngirls.online/

Reply
Barbarat February 3, 2024 - 2:37 pm

SEO Optimizers Team http://myngirls.online/

Reply
Kellyt February 4, 2024 - 4:23 am

I offer mutually beneficial cooperation http://myngirls.online/

Reply
Beatricet February 4, 2024 - 9:56 am

Content for your website http://fertus.shop/info/

Reply
Laurat February 4, 2024 - 5:48 pm

Web Development Wizards http://fertus.shop/info/

Reply
Susant February 5, 2024 - 1:38 am

Can provide a link mass to your website http://fertus.shop/info/

Reply
Gloriat February 5, 2024 - 9:28 am

Your site’s position in the search results http://fertus.shop/info/

Reply
Norat February 5, 2024 - 11:19 pm

Free analysis of your website http://fertus.shop/info/

Reply
Barbarat February 6, 2024 - 1:08 pm

SEO Optimizers Team http://fertus.shop/info/

Reply
Kellyt February 7, 2024 - 2:57 am

I offer mutually beneficial cooperation http://fertus.shop/info/

Reply
Stacyt February 7, 2024 - 4:48 pm

Cool website. There is a suggestion http://fertus.shop/info/

Reply
Gillt February 8, 2024 - 6:38 am

I really liked your site. Do you mind http://fertus.shop/info/

Reply
Natalyt February 8, 2024 - 8:27 pm

Here’s what I can offer for the near future http://fertus.shop/info/

Reply
Audreyt February 9, 2024 - 10:12 am

You will definitely like it http://fertus.shop/info/

Reply
Karent February 9, 2024 - 11:58 pm

The best prices from the best providers http://fertus.shop/info/

Reply
Shirleyt February 10, 2024 - 1:41 pm

Additional earnings on your website http://fertus.shop/info/

Reply
Florancet February 11, 2024 - 3:23 am

Analytics of your website http://fertus.shop/info/

Reply
Aryat February 12, 2024 - 7:09 am

How to contact the administrator on this issue http://fertus.shop/info/

Reply
Juliett February 12, 2024 - 9:14 pm

Shall we exchange links? My website http://fertus.shop/info/

Reply
Jason February 12, 2024 - 10:23 pm Reply
Sheilat February 13, 2024 - 11:02 am

The offer is still valid. Details http://fertus.shop/info/

Reply
Evat February 14, 2024 - 12:56 am

We offer cooperation on SEO optimization http://fertus.shop/info/

Reply
Monicat February 14, 2024 - 2:43 pm

Content for your website http://fertus.shop/info/

Reply
Annt February 15, 2024 - 4:25 am

Web Development Wizards http://fertus.shop/info/

Reply
Beatricet February 16, 2024 - 11:04 am

Content for your website http://fertus.shop/info/

Reply
Susant February 17, 2024 - 1:46 pm

Can provide a link mass to your website http://fertus.shop/info/

Reply
Barbarat February 19, 2024 - 5:41 am

SEO Optimizers Team http://fertus.shop/info/

Reply
amciik siteleri February 21, 2024 - 8:03 am

watch porn video bingoxx.Gopnq4sSIZCg

Reply
sexax February 21, 2024 - 8:47 am

sexx 250tldenemebonusuxx.zwj43oeveWKQ

Reply
porn sex February 21, 2024 - 10:56 am

seks siteleri eyeconartxx.T6EneGgF2sPu

Reply
escort February 22, 2024 - 5:12 pm

eskort siteleri vvsetohimalxxvc.f03OuJyVqXB4

Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More