0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【CSS3】hoverしたら左から色を変更するアニメーション付きボタン

Last updated at Posted at 2020-03-14

【CSS3】hoverしたら左から色を変更するアニメーション付きボタン

hoverしたら空のspanタグが移動してくる動作でアニメーションを表現。

※今回の記事はudemyの動画を参考に書き、備忘録的な動機で投稿しています。

index.html
<div id="container">
  <button class="btn slide-bg"><span></span>Button</button>
</div>
style.css
#container {
  text-align: center;
}

.btn {
  background-color: #ffff;
  color: black;
  border: 1px solid black;
  padding: 10px 40px;
  margin: 50px 0;
  font-weight: 600;
  cursor: pointer;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
  z-index: 1;
}

.btn.slide-bg {
  position: relative;
  overflow: hidden;
}

.btn.slide-bg:hover {
  color: #ffff;
}

.btn.slide-bg:hover span {
  -webkit-transform: none;
          transform: none;
  z-index: -1;
}

.btn.slide-bg span {
  display: inline-block;
  width: 100%;
  height: 100%;
  background-color: #000;
  position: absolute;
  top: 0;
  left: 0;
  -webkit-transform: translateX(-100%);
          transform: translateX(-100%);
  -webkit-transition: -webkit-transform 0.3s;
  transition: -webkit-transform 0.3s;
  transition: transform 0.3s;
  transition: transform 0.3s, -webkit-transform 0.3s;
}

疑似要素を使う場合

index.html
<div id="container">
  <button class="btn slide-bg">Button</button>
</div>
style.css
#container {
  text-align: center;
}

.btn {
  background-color: #ffff;
  color: black;
  border: 1px solid black;
  padding: 10px 40px;
  margin: 50px 0;
  font-weight: 600;
  cursor: pointer;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
  z-index: 1;
}

.btn.slide-bg {
  position: relative;
  overflow: hidden;
}

.btn.slide-bg:hover {
  color: #ffff;
}

.btn.slide-bg:hover::before {
  -webkit-transform: none;
          transform: none;
  z-index: -1;
}

.btn.slide-bg::before {
  content: '';
  display: inline-block;
  width: 100%;
  height: 100%;
  background-color: #000;
  position: absolute;
  top: 0;
  left: 0;
  -webkit-transform: translateX(-100%);
          transform: translateX(-100%);
  -webkit-transition: -webkit-transform 0.3s;
  transition: -webkit-transform 0.3s;
  transition: transform 0.3s;
  transition: transform 0.3s, -webkit-transform 0.3s;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?