Web, WordPress etc…

CSSだけでボタンが出入りするトグルボタンを実装

この記事をシェアする

スポンサードリンク




こんにちは、シロマ@mademodeです。

今回は、下のデモのようなクリックするとボタンがにゅっと出てくるトグルボタンを実装します。
デモの左下にあるボタンをクリックしてみて下さい。

DEMO

このブログでもウィンドウサイズが狭くなると、フォローボタンがトグルボタンに収納されます。

JavaScriptを使わずに、CSSだけで実装できます。

実装方法

それではまず、HTML。

  1. <div class="in-out">
  2. <input type="checkbox" id="in-out-checkbox">
  3. <label for="in-out-checkbox" class="in-out-label">
  4. <span class="tate"></span>
  5. <span class="yoko"></span>
  6. </label>
  7. <button type="button" class="button1">1</button>
  8. <button type="button" class="button2">2</button>
  9. <button type="button" class="button3">3</button>
  10. </div>
<div class="in-out">
<input type="checkbox" id="in-out-checkbox">
<label for="in-out-checkbox" class="in-out-label">
<span class="tate"></span>
<span class="yoko"></span>
</label>
<button type="button" class="button1">1</button>
<button type="button" class="button2">2</button>
<button type="button" class="button3">3</button>
</div>

そして、CSS。

  1. .in-out {
  2. position: fixed;
  3. bottom: 20px;
  4. left: 20px;
  5. width: 50px;
  6. height: 50px;
  7. }
  8. .in-out button,
  9. .in-out-label {
  10. position: absolute;
  11. background-color: #ddd;
  12. border: none;
  13. border-radius: 50%;
  14. width: 50px;
  15. height: 50px;
  16. transition: all .3s;
  17. box-shadow: 2px 2px 4px rgba(0,0,0,.2);
  18. }
  19. .in-out button {
  20. top: 0;
  21. opacity: 0;
  22. z-index: 10;
  23. }
  24. .in-out-label {
  25. z-index: 20;
  26. }
  27. .in-out-label span {
  28. position: absolute;
  29. background-color: #aaa;
  30. display: block;
  31. transition: all .3s;
  32. }
  33. .in-out-label .tate {
  34. width: 4px;
  35. height: 24px;
  36. left: 23px;
  37. top: 13px;
  38. }
  39. .in-out-label .yoko {
  40. width: 24px;
  41. height: 4px;
  42. left: 13px;
  43. top: 23px;
  44. }
  45. .in-out input[type="checkbox"] {
  46. display: none;
  47. }
  48. .in-out input[type="checkbox"]:checked + .in-out-label .tate {
  49. height: 0;
  50. top: 25px;
  51. }
  52. .in-out input[type="checkbox"]:checked ~ button {
  53. opacity: 1;
  54. }
  55. .in-out input[type="checkbox"]:checked ~ .button1 {
  56. top: -210px;
  57. }
  58. .in-out input[type="checkbox"]:checked ~ .button2 {
  59. top: -140px;
  60. }
  61. .in-out input[type="checkbox"]:checked ~ .button3 {
  62. top: -70px;
  63. }
.in-out {
position: fixed;
bottom: 20px;
left: 20px;
width: 50px;
height: 50px;
}
.in-out button,
.in-out-label {
position: absolute;
background-color: #ddd;
border: none;
border-radius: 50%;
width: 50px;
height: 50px;
transition: all .3s;
box-shadow: 2px 2px 4px rgba(0,0,0,.2);
}
.in-out button {
top: 0;
opacity: 0;
z-index: 10;
}
.in-out-label {
z-index: 20;
}
.in-out-label span {
position: absolute;
background-color: #aaa;
display: block;
transition: all .3s;
}
.in-out-label .tate {
width: 4px;
height: 24px;
left: 23px;
top: 13px;
}
.in-out-label .yoko {
width: 24px;
height: 4px;
left: 13px;
top: 23px;
}
.in-out input[type="checkbox"] {
display: none;
}
.in-out input[type="checkbox"]:checked + .in-out-label .tate {
height: 0;
top: 25px;
}
.in-out input[type="checkbox"]:checked ~ button {
opacity: 1;
}
.in-out input[type="checkbox"]:checked ~ .button1 {
top: -210px;
}
.in-out input[type="checkbox"]:checked ~ .button2 {
top: -140px;
}
.in-out input[type="checkbox"]:checked ~ .button3 {
top: -70px;
}

長いね。

ポイントとしてはCSSにちょっと変わったセレクタを使用している点ですね。

セレクタの「+」は隣接する兄弟要素、「~」は後の兄弟要素に適用されます。あまり使われないセレクタなので、知らない人もいるかもしれません。

あと、アコーディオンメニューとか作る時にinputのチェックボックスは結構使えるので、覚えておくと便利かと思います。

スポンサードリンク




Illustratorでポリゴンのパターンを作る>

この記事をシェアする