Jerico Borja
Jerico Borja

Reputation: 5

Font awesome translation or transform when click by user

Good day! I'm trying to make a fixed floating action button with Font Awesome 5 that when a user clicks on the large button it will transform or translate the Font Awesome into another Font Awesome logo.

Here's the fiddle of https://jsfiddle.net/4eLq3dj0/ that has what I've tried so far. I also wanted it to spin around the bottom so I used this other thread.

HTML

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css" rel="stylesheet">
        <link rel="stylesheet" href="">
    </head>

    <body>
        <div class="button">
        <div class="icons">
            <button href="#" class="floating-btn">
            <i class="fas fa-plus icon-default "></i>
            <i class="fas fa-times icon-hover"></i>
            </button>
        </div>
    </div>
    </body>
</html>

CSS

.floating-btn{
            width: 80px;
            height: 80px;
            background: #f5af09;
            display: flex;
            align-items: center;
            justify-content: center;
            text-decoration: none;
            border-radius: 50%;
            color: #ffffff;
            font-size: 40px;
            box-shadow: 2px 2px 5px (0,0,0,0.25);
            position: fixed;
            right: 20px;
            bottom: 20px;
            transition: background 0.25s;
            outline: blue;
            border: none;
            cursor: pointer;

        }
         .button .icons {
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0 2.3rem 0 0;
            width: 1.25rem;
            height: 2.6rem;
        }

        .button .icons i {
            position: absolute;
            top: 5;
            left: 5;
            display: block;
        }

        .floating-btn:active{
            background:#007D63;
        }

         .button .icons .icon-hover {
            transition: opacity 0.3s, transform 0.3s;
            transform: rotate(-180deg) scale(0.5);
            opacity: 0;
        }

        .button:active {
            transform: scale(1.2);
            box-shadow: 20px 15px rgba(0, 0, 0, 0.15);
        }

        .button:active .icon-hover {
            transform: rotate(0deg) scale(1);
            opacity: 1;
        }

        .button:active .icon-default {
            transform: rotate(180deg) scale(0.5);
            opacity: 0;
        }

Upvotes: 0

Views: 327

Answers (2)

tao
tao

Reputation: 90237

Is this what you're trying to achieve? Hover:

.floating-btn {
  width: 80px;
  height: 80px;
  background: #f5af09;
  text-decoration: none;
  border-radius: 50%;
  color: #ffffff;
  font-size: 40px;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  right: 20px;
  bottom: 20px;
  transition: background-color 0.25s ease-out;
  border: none;
  cursor: pointer;
}

.floating-btn:hover {
  background-color: red;
}

.floating-btn i {
  transition: transform 0.25s cubic-bezier(.4, 0, .2, 1);
}

.floating-btn:hover i {
  transform: rotate(45deg);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css" rel="stylesheet" />

<div class="button">
  <div class="icons">
    <button href="#" class="floating-btn">
      <i class="fas fa-plus"></i>
    </button>
  </div>
</div>

Click:

document.querySelector('.floating-btn').addEventListener('click', function(e) {
  e.target.closest('button').classList.toggle('clicked');
})
.floating-btn {
  width: 80px;
  height: 80px;
  background: #f5af09;
  text-decoration: none;
  border-radius: 50%;
  color: #ffffff;
  font-size: 40px;
  box-shadow: 0 1px 3px 0 rgba(0,0,0,.2), 0 1px 1px 0 rgba(0,0,0,.14), 0 2px 1px -1px rgba(0,0,0,.12);
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  right: 20px;
  bottom: 20px;
  transition: background-color 0.25s ease-out, box-shadow 0.25s ease-out;
  border: none;
  cursor: pointer;
  outline: none;
}
.floating-btn:hover {
  box-shadow: 0 4px 5px -2px rgba(0,0,0,.2), 0 7px 10px 1px rgba(0,0,0,.14), 0 2px 16px 1px rgba(0,0,0,.12);
}


.floating-btn.clicked {
  background-color: red;
}

.floating-btn i {
  transition: transform 0.25s cubic-bezier(.4, 0, .2, 1);
}

.floating-btn.clicked i {
  transform: rotate(315deg);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css" rel="stylesheet" />

<div class="button">
  <div class="icons">
    <button href="#" class="floating-btn">
      <i class="fas fa-plus"></i>
    </button>
  </div>
</div>

Upvotes: 2

OM PRAKASH
OM PRAKASH

Reputation: 393

.floating-btn {
  width: 80px;
  height: 80px;
  background: #f5af09;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  border-radius: 50%;
  color: #ffffff;
  font-size: 40px;
  box-shadow: 2px 2px 5px (0, 0, 0, 0.25);
  position: fixed;
  right: 20px;
  bottom: 20px;
  transition: background 0.25s;
  outline: blue;
  border: none;
  cursor: pointer;
}

.floating-btn i {
  position: absolute;
  top: 5;
  left: 5;
  display: block;
}

.floating-btn:active {
  background: #007D63;
}

.floating-btn .icon-hover {
  transition: opacity 0.3s, transform 0.3s;
  transform: rotate(-180deg) scale(0.5);
  opacity: 0;
}

.floating-btn:active {
  transform: scale(1.2);
  box-shadow: 20px 15px rgba(0, 0, 0, 0.15);
}

.floating-btn:active .icon-hover {
  transform: rotate(0deg) scale(1);
  opacity: 1;
}

.floating-btn:active .icon-default {
  transform: rotate(180deg) scale(0.5);
  opacity: 0;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css" rel="stylesheet">
  <link rel="stylesheet" href="">
</head>

<body>
  <div class="button">
    <div class="icons">
      <button href="#" class="floating-btn">
		<i class="fas fa-plus icon-default"></i>
		<i class="fas fa-times icon-hover"></i>
      </button>
    </div>
  </div>
</body>

Upvotes: 0

Related Questions