在開發hexo主題pixel的時候沒有選擇bootstrap和jquery實現響應式菜單,而是
使用了純css實現響應式菜單,這個想法來自於You-Dont-Need-Javascript,
這個項目分享了很多精彩的純css效果,值得學習。
簡單閱讀這些css效果源碼之后發現大部分css實現動態效果都是使用[type="checkbox"]
也就是一個單選按鈕來保存html組件的狀態,更復雜的效果可能
需要單選按鈕組合來實現。
checkbox有一個checked狀態,可以使用css偽類選擇器[type="checkbox"]:checked
來實現css切換。
下面是這個博客使用的導航菜單scss格式的源碼
.nav-collapse {
height: 60px;
.nav-brand {
float: left;
padding: 10px;
font-style: italic;
font-family: Georgia;
font-size: 28px;
text-decoration: none;
color: #000;
margin-right: 50px;
}
[type="checkbox"].check,
label.toggle {
display: none;
}
.nav-nav {
li {
text-align: center;
line-height: 40px;
margin-left: 40px;
float: left;
list-style: none;
}
li:hover {
cursor: pointer;
position: relative;
}
li > a {
display: block;
width: 100%;
padding: 5px 0;
font-weight: bold;
font-size: 14px;
text-transform: uppercase;
letter-spacing: .2px;
}
li:hover > a {
color: #e1eb3e;
}
}
}
@media all and (max-width: 768px) {
.nav-collapse {
.nav-brand {
padding: 20px;
}
.nav-nav {
background: #fff;
opacity: 0;
display: block;
z-index: 9999;
position: fixed;
right: 0;
left: 0;
padding: 0;
top: 60px;
li {
display: block;
width: 100%;
text-align: left;
text-indent: 20px;
margin-left: 10px;
border-bottom: 1px solid #efefef;
}
}
label.toggle {
display: block;
background: #fff;
width: 40px;
height: 40px;
cursor: pointer;
position: absolute;
right: 20px;
top: 20px;
}
label.toggle:after {
content: '';
display: block;
width: 30px;
height: 5px;
background: #777;
margin: 7px 5px;
box-shadow: 0px 10px 0px #777, 0px 20px 0px #777;
}
[type="checkbox"].check:checked + .nav-nav {
opacity: 1;
transition: all .5s linear;
}
}
}
上面這個的導航菜單是固定在頂部的,如果需要導航菜單使用流動布局,下面這個是可以放到頁面任何位置的導航菜單
.nav-collapse {
background-color: #000;
.nav-brand {
float: left;
padding: 10px;
font-style: italic;
font-family: Georgia;
font-size: 28px;
text-decoration: none;
color: #000;
margin-right: 50px;
}
[type="checkbox"].check,
label.toggle {
display: none;
}
.nav-nav {
li {
text-align: center;
float: left;
list-style: none;
}
li:hover {
cursor: pointer;
position: relative;
}
li > a {
display: block;
width: 100%;
color: #fff;
padding: 10px 12px;
font-weight: bold;
font-size: 14px;
text-transform: uppercase;
letter-spacing: .2px;
font-family: 'Consolas', 微軟雅黑;
}
li:hover > a {
color: #e1eb3e;
}
}
}
@media all and (max-width: 768px) {
.top {
width: 100%;
}
.nav-collapse {
.nav-brand {
padding: 20px;
}
.nav-nav {
background: #fff;
display: none;
z-index: 9999;
li {
display: block;
width: 100%;
text-align: left;
text-indent: 20px;
margin-left: 10px;
border-bottom: 1px solid #efefef;
a {
color: #000;
}
}
li:hover > a {
color: #e1eb3e;
}
}
label.toggle {
display: block;
width: 40px;
height: 40px;
cursor: pointer;
}
label.toggle:after {
content: '';
display: block;
width: 30px;
height: 5px;
background: #fff;
margin: 7px 5px;
box-shadow: 0px 10px 0px #fff, 0px 20px 0px #fff;
}
[type="checkbox"].check:checked + .nav-nav {
opacity: 1;
display: block;
transition: all .5s linear;
}
}
}
如果想要看實際效果可以訪問這個網站美劇天堂,效果是一樣的。
休閑時間看看美劇是很好的消遣。