返回

让按钮随着鼠标滑动而动起来!CSS3 鼠标滑过按钮动画第二节

前端

有了第一小节的经验,我们可以对直接的动画效果做一些升级效果,如果组合 :before、:after,效果有更酷。请先看一下效果示例吧:

下面一个一个示例讲解

示例一

.button {
  width: 100px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background-color: #0099FF;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s ease-in-out;
}

.button:hover {
  background-color: #0066FF;
  transform: scale(1.1);
}

.button:before {
  content: "";
  position: absolute;
  top: -5px;
  left: -5px;
  width: 110px;
  height: 60px;
  background-color: #0099FF;
  border-radius: 5px;
  opacity: 0;
  transition: all 0.3s ease-in-out;
}

.button:hover:before {
  opacity: 1;
}

.button:after {
  content: "";
  position: absolute;
  top: 5px;
  left: 5px;
  width: 90px;
  height: 40px;
  background-color: #fff;
  border-radius: 5px;
  opacity: 0;
  transition: all 0.3s ease-in-out;
}

.button:hover:after {
  opacity: 1;
}

解析:

1、:before

首先我们给按钮添加一个 :before 伪类,并设置其位置、大小、背景颜色、圆角等样式。

2、:hover :before

在 :hover 状态下,我们将 :before 的不透明度设置为 1,使其显示出来。

3、:after

我们再给按钮添加一个 :after 伪类,并设置其位置、大小、背景颜色、圆角等样式。

4、:hover :after

在 :hover 状态下,我们将 :after 的不透明度设置为 1,使其显示出来。

示例二

.button {
  width: 100px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background-color: #0099FF;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s ease-in-out;
}

.button:hover {
  background-color: #0066FF;
  transform: scale(1.1);
}

.button:before {
  content: "";
  position: absolute;
  top: -5px;
  left: -5px;
  width: 110px;
  height: 60px;
  background-color: #0099FF;
  border-radius: 5px;
  opacity: 0;
  transition: all 0.3s ease-in-out;
}

.button:hover:before {
  transform: rotate(360deg);
  opacity: 1;
}

.button:after {
  content: "";
  position: absolute;
  top: 5px;
  left: 5px;
  width: 90px;
  height: 40px;
  background-color: #fff;
  border-radius: 5px;
  opacity: 0;
  transition: all 0.3s ease-in-out;
}

.button:hover:after {
  transform: rotate(-360deg);
  opacity: 1;
}

解析:

1、:before

和示例一相同,我们先给按钮添加一个 :before 伪类,并设置其位置、大小、背景颜色、圆角等样式。

2、:hover :before

在 :hover 状态下,我们将 :before 的不透明度设置为 1,并使其旋转 360 度,这样就会出现一个旋转的圆环。

3、:after

我们再给按钮添加一个 :after 伪类,并设置其位置、大小、背景颜色、圆角等样式。

4、:hover :after

在 :hover 状态下,我们将 :after 的不透明度设置为 1,并使其旋转 -360 度,这样就会出现一个反方向旋转的圆环。

示例三

.button {
  width: 100px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background-color: #0099FF;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s ease-in-out;
}

.button:hover {
  background-color: #0066FF;
  transform: scale(1.1);
}

.button:before {
  content: "";
  position: absolute;
  top: -5px;
  left: -5px;
  width: 110px;
  height: 60px;
  background-color: #0099FF;
  border-radius: 5px;
  opacity: 0;
  transition: all 0.3s ease-in-out;
}

.button:hover:before {
  transform: translateX(10px);
  opacity: 1;
}

.button:after {
  content: "";
  position: absolute;
  top: 5px;
  left: 5px;
  width: 90px;
  height: 40px;
  background-color: #fff;
  border-radius: 5px;
  opacity: 0;
  transition: all 0.3s ease-in-out;
}

.button:hover:after {
  transform: translateX(-10px);
  opacity: 1;
}

解析:

1、:before

和示例一相同,我们先给按钮添加一个 :before 伪类,并设置其位置、大小、背景颜色、圆角等样式。

2、:hover :before

在 :hover 状态下,我们将 :before 的不透明度设置为 1,并将其向右平移 10 像素。

3、:after

我们再给按钮添加一个 :after 伪类,并设置其位置、大小、背景颜色、圆角等样式。

4、:hover :after

在 :hover 状态下,我们将 :after 的不透明度设置为 1,并将其向左平移 10 像素。

小结

以上就是 CSS3 鼠标滑过按钮动画第二节的内容,我们学习了如何使用 :before 和 :after 伪类来创建更酷炫的按钮动画。希望这些示例对您有所帮助,让您能够在自己的项目中创建出更加美观、有趣的按钮。