* {
margin:0;
padding:0;
}
.bar {
width:100vw;
height:50px;
background-color:moccasin;
position:relative;
}
a {
text-decoration:none;
}
a:link {
color:#333;
}
.summary {
color:#333;
display:inline-block;
padding:0 32px;
margin-left:20px;
line-height:50px;
user-select:none;
}
.details:focus-within .summary {
background-color:#222;
color:#fff;
pointer-events:none;
}
.box {
background-color:mistyrose;
width:128px;
margin-left:20px;
padding:5px 0;
display:none;
}
.details:focus-within .box {
display:block;
}
.box a {
display:block;
padding:15px 20px;
}
.box a:hover {
background-color:#ccc;
color:red;
}
效果实现的核心是使用css3新的属性 :focus-within ,这个属性的意思是当该元素或子元素获得焦点时,会产生作用(最大的好处就是父元素和子元素可以用这一句代码相互影响)