0
点赞
收藏
分享

微信扫一扫

有趣的CSS - 一直动的按钮

大家好,我是 Just,这里是「设计师工作日常」,今天分享的是用 css 写一个好玩的不停震动的按钮。

《有趣的css》系列最新实例通过公众号「设计师工作日常」发布。

(目录)

整体效果

使用 animation 属性,使 button 按钮不停的循环移动,产生视觉震动的效果。

核心代码部分,简要说明了写法思路;完整代码在最后,可直接复制到本地运行。

核心代码

html 代码

<button class="btn15">动起来</button>

一个 button 标签。

css 部分代码

.btn15{
  width: 140px;
  height: 48px;
  line-height: 48px;
  background-color: #eee;
  border: none;
  border-radius: 24px;
  font-size: 16px;
  color: #333333;
  text-align: center;
  transition: all 0.24s linear;
}
.btn15:hover{
  cursor: pointer;
  color: #fff;
  background-color: #253ed2;
  animation: yizhidong 0.24s linear infinite;
}
@keyframes yizhidong{
  0%{
    transform: translate(0);
  }
  20%{
    transform: translate(-3px, 3px);
  }
  40%{
    transform: translate(-2px, -3px);
  }
  60%{
    transform: translate(3px, 2px);
  }
  80%{
    transform: translate(2px, -3px);
  }
  100%{
    transform: translate(0);
  }
}

设置 animation 属性参数,不同的关键帧过渡移动到不同的位置,让按钮位置不停循环变化。

完整代码如下

html 页面

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <title>一直动的按钮</title>
  </head>
  <body>
    <div class="app">
      <button class="btn15">动起来</button>
    </div>
  </body>
</html>

css 样式

/** style.css **/
.app{
  width: 100%;
  height: 100vh;
  background-color: #ffffff;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.btn15{
  width: 140px;
  height: 48px;
  line-height: 48px;
  background-color: #eee;
  border: none;
  border-radius: 24px;
  font-size: 16px;
  color: #333333;
  text-align: center;
  transition: all 0.24s linear;
}
.btn15:hover{
  cursor: pointer;
  color: #fff;
  background-color: #253ed2;
  animation: yizhidong 0.24s linear infinite;
}
@keyframes yizhidong{
  0%{
    transform: translate(0);
  }
  20%{
    transform: translate(-3px, 3px);
  }
  40%{
    transform: translate(-2px, -3px);
  }
  60%{
    transform: translate(3px, 2px);
  }
  80%{
    transform: translate(2px, -3px);
  }
  100%{
    transform: translate(0);
  }
}

页面渲染效果

以上就是所有代码,以及简单的思路,希望对你有一些帮助或者启发。

CSS 是一种很酷很有趣的计算机语言,在这里跟大家分享一些 CSS 实例 Demo,为学习者获取灵感和思路提供一点帮助,希望你们喜欢。

我是 Just,这里是「设计师工作日常」,求点赞求关注!

举报

相关推荐

0 条评论