

フルスクリーンメニューってどうやって作るの?
そんな疑問にお答えします。
ハンバーガーボタンを押したら表示されるナビゲーションメニューの動きはさまざまです。
その中には画面全体を覆うタイプのフルスクリーンメニューというものがあります。
今回はjQueryを使わないで作る方法を紹介します。
jQueryを使わないフルスクリーンメニューの作り方
See the Pen
フルスクリーンナビ1 by toshi (@toshi78)
on CodePen.
jQueryを使わないでどうやってフルスクリーンメニューの動きを実装するか?
それは「 :checked 疑似クラス」というのを使います。
詳しい説明はこちらでしています。
【jQuery不要】HTMLとCSSのみでドロワーメニューを実装する方法
簡単に説明すると、チェックボックスを利用してチェックON、チェックOFF時の挙動を制御します。
今回はそれの応用で、チェックON時の画面表示を全画面表示にします。
HTML
<input type="checkbox" id="drawer-checkbox" class="menu-checkbox" />
<label for="drawer-checkbox" class="drawer-icon"><span></span></label>
💡ポイント!
- 項目名は「label」で囲む
- labelには「for=”〇〇”」、inputには「id=”〇〇”」と同じ名前を書く
押さえるべきはこの2点です。
CSS
.menu-checkbox {
display: none;
}
.drawer {
position: fixed;
top: 20px;
right: 20px;
}
.drawer-menu {
position: fixed;
top: 0;
right: 0;
z-index: 10;
width: 100%;
height: 100%;
background-color: #3584bb;
opacity: 0;
visibility: hidden;
transition: all 0.5s ease-in-out 0s;
}
.menu-checkbox:checked ~ .drawer-menu {
opacity: 1;
visibility: visible;
}
.drawer-menu-list {
margin-top: 100px;
}
.drawer-menu-item-link {
display: block;
text-align: center;
font-size: 20px;
color: #fff;
margin-top: 20px;
transition: all 0.3s ease 0s;
}
.drawer-menu-item-link:hover {
opacity: 0.6;
}
.drawer-icon {
cursor: pointer;
display: inline-block;
width: 50px;
height: 50px;
position: relative;
z-index: 20;
}
.drawer-icon span {
position: absolute;
display: block;
top: 50%;
left: 50%;
width: 84%;
height: 16%;
background-color: #3584bb;
border-radius: 4px;
margin: -8% 0 0 -42%;
transition: all 0.3s ease-in-out;
}
.drawer-icon span:before,
.drawer-icon span:after {
position: absolute;
display: block;
content: "";
top: 50%;
left: 50%;
width: 100%;
height: 100%;
background-color: #3584bb;
border-radius: 4px;
margin: -8% 0 0 -50%;
transform: rotate(0);
transition: all 0.3s ease-in-out;
}
.drawer-icon span::before {
margin-top: -38%;
}
.drawer-icon span::after {
margin-top: 19%;
}
.menu-checkbox:checked ~ .drawer-icon {
background-color: #3584bb;
}
.menu-checkbox:checked ~ .drawer-icon span {
background-color: rgba(255, 255, 255, 0);
}
.menu-checkbox:checked ~ .drawer-icon span::before,
.menu-checkbox:checked ~ .drawer-icon span::after {
position: absolute;
display: block;
content: "";
top: 50%;
left: 50%;
width: 100%;
height: 100%;
margin: -8% 0 0 -42%;
background-color: #fff;
}
.menu-checkbox:checked ~ .drawer-icon span::before {
transform: rotate(-45deg);
}
.menu-checkbox:checked ~ .drawer-icon span::after {
transform: rotate(45deg);
}
ここでのポイントは次の2点です。
💡ポイント!
-
.menu-checkbox:checked ~ .drawer-menuでメニューが開いたときの状態を指定する
- width: 100%; height: 100%;で全画面にする
.drawer-menu {
position: fixed;
top: 0;
right: 0;
z-index: 10;
width: 100%;
height: 100%;
background-color: #3584bb;
opacity: 0;
visibility: hidden;
transition: all 0.5s ease-in-out 0s;
}
.menu-checkbox:checked ~ .drawer-menu {
opacity: 1;
visibility: visible;
}
ON・OFFの表示の切り替えは「opacity: 0; visibility: hidden;」↔「opacity: 1;visibility: visible;」で行っています。
transition: all 0.5s ease-in-out 0s;でなめらかな動きにしています。
以上です。
投稿者 トシ
コメントを残す