当用户点击

美好的一天!我正在尝试使用Font Awesome 5做一个固定的浮动动作按钮,当用户单击大按钮时,它将把Font Awesome转换或转换为另一个Font Awesome徽标。

这是https://jsfiddle.net/4eLq3dj0/的小提琴,它具有我到目前为止已经尝试过的方法。我还希望它围绕底部旋转,所以我使用了另一个线程。

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.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.15);
        }

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

        .button:active .icon-default {
            transform: rotate(180deg) scale(0.5);
            opacity: 0;
        }
jinjin1991 回答:当用户点击

这是您要实现的目标吗?悬停:

.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.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,.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>

点击:

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,.2),0 1px 1px 0 rgba(0,.14),0 2px 1px -1px rgba(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 7px 10px 1px rgba(0,0 2px 16px 1px rgba(0,.12);
}


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

.floating-btn i {
  transition: transform 0.25s cubic-bezier(.4,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>

,

.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.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.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>

本文链接:https://www.f2er.com/3119320.html

大家都在问