我已经使用css创建了一个多级下拉菜单。
它在li
元素旁边有一个带有子项的下拉图标。
假设当我点击精选新闻时,我想更改 menu-down.png 图标。
我知道可以使用input
元素::checked
属性来完成,但不知怎的,我无法做到。
这是我的代码
HTML:
<ul class="cd-accordion-menu animated">
<li class="has-children">
<input type="checkbox" name ="group-1" id="group-1">
<label for="group-1">
<span class="menu-option-img"><img src="img/menu-cat.png" ></span>
<span class="menu-option-content has-dropdown">Featured News</span>
</label>
<ul>
<li class="has-children">
<input type="checkbox" name ="sub-group-1" id="sub-group-1">
<label for="sub-group-1">
<span class="menu-option-img"><img src="img/menu-cat.png" ></span>
<span class="menu-option-content has-dropdown">Sub 1</span>
</label>
<ul>
<li><a href="#0"><img src="img/menu-cat.png" > Menu 1</a></li>
<li><a href="#0"><img src="img/menu-cat.png" > Menu 2</a></li>
<li><a href="#0"><img src="img/menu-cat.png" > Menu 3</a></li>
</ul>
</li>
</ul>
</li>
</ul>
CSS:
.menu-down {
height: 7px !important;
position: relative !important;
left: 8px !important;
top: -4px !important;
margin-right: 0px !important;
}
.cd-accordion-menu input[type=checkbox]:checked .has-dropdown::after{
content: "";
width: 25px;
height: 25px;
background: url(../img/menu-down.png);
background-size: 25px;
background-repeat: no-repeat;
position: relative;
display: inline-block;
top: 4px;
left: 24px;
}
.has-dropdown::after {
content: "";
width: 15px;
height: 15px;
background: url(../img/menu-down.png);
background-size: 15px;
background-repeat: no-repeat;
position: relative;
display: inline-block;
top: 4px;
left: 24px;
}
.cd-accordion-menu {
margin: auto;
}
.cd-accordion-menu ul {
/* by default hide all sub menus */
display: none;
}
.cd-accordion-menu li {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.cd-accordion-menu input[type=checkbox] {
/* hide native checkbox */
position: absolute;
opacity: 0;
}
.cd-accordion-menu label, .cd-accordion-menu a {
position: relative;
display: block;
padding: 17px 4px 10px 24px;
background: #ffffff;
color: #373737;
font-size: 18px;
font-family: 'Open Sans', sans-serif;
font-weight: 600;
letter-spacing: 1px;
}
.no-touch .cd-accordion-menu label:hover, .no-touch .cd-accordion-menu a:hover {
background: #52565d;
}
.cd-accordion-menu label::before, .cd-accordion-menu label::after, .cd-accordion-menu a::after {
/* icons */
content: '';
display: inline-block;
width: 16px;
height: 16px;
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
-ms-transform: translateY(-50%);
-o-transform: translateY(-50%);
transform: translateY(-50%);
}
.cd-accordion-menu img {
min-width: auto;
height: 21px;
margin-right: 18px;
}
.cd-accordion-menu label {
cursor: pointer;
}
/*
.cd-accordion-menu label::before, .cd-accordion-menu label::after {
background-image: url(../img/cd-icons.svg);
background-repeat: no-repeat;
}
*/
.cd-accordion-menu label::before {
/* arrow icon */
left: 18px;
background-position: 0 0;
-webkit-transform: translateY(-50%) rotate(-90deg);
-moz-transform: translateY(-50%) rotate(-90deg);
-ms-transform: translateY(-50%) rotate(-90deg);
-o-transform: translateY(-50%) rotate(-90deg);
transform: translateY(-50%) rotate(-90deg);
}
.cd-accordion-menu label::after {
/* folder icons */
left: 41px;
background-position: -16px 0;
}
.cd-accordion-menu a::after {
/* image icon */
left: 36px;
background: url(../img/cd-icons.svg) no-repeat -48px 0;
}
.cd-accordion-menu input[type=checkbox]:checked + label::before {
/* rotate arrow */
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
-ms-transform: translateY(-50%);
-o-transform: translateY(-50%);
transform: translateY(-50%);
}
.cd-accordion-menu input[type=checkbox]:checked + label::after {
/* show open folder icon if item is checked */
background-position: -32px 0;
}
.cd-accordion-menu input[type=checkbox]:checked + label + ul,
.cd-accordion-menu input[type=checkbox]:checked + label:nth-of-type(n) + ul {
/* use label:nth-of-type(n) to fix a bug on safari (<= 8.0.8) with multiple adjacent-sibling selectors*/
/* show children when item is checked */
display: block;
}
.cd-accordion-menu ul label,
.cd-accordion-menu ul a {
background: #fff;
padding-left: 49px;
padding-top: 6px;
}
.no-touch .cd-accordion-menu ul label:hover, .no-touch
.cd-accordion-menu ul a:hover {
background: #3c3f45;
}
.cd-accordion-menu > li:last-of-type > label,
.cd-accordion-menu > li:last-of-type > a,
.cd-accordion-menu > li > ul > li:last-of-type label,
.cd-accordion-menu > li > ul > li:last-of-type a {
box-shadow: none;
}
.cd-accordion-menu ul label::before {
left: 36px;
}
.cd-accordion-menu ul label::after,
.cd-accordion-menu ul a::after {
left: 59px;
}
.cd-accordion-menu ul ul label,
.cd-accordion-menu ul ul a {
padding-left: 68px;
}
.cd-accordion-menu ul ul label::before {
left: 54px;
}
.cd-accordion-menu ul ul label::after,
.cd-accordion-menu ul ul a::after {
left: 77px;
}
.cd-accordion-menu ul ul ul label,
.cd-accordion-menu ul ul ul a {
padding-left: 118px;
}
.cd-accordion-menu ul ul ul label::before {
left: 72px;
}
.cd-accordion-menu ul ul ul label::after,
.cd-accordion-menu ul ul ul a::after {
left: 95px;
}
@media only screen and (min-width: 600px) {
.cd-accordion-menu label, .cd-accordion-menu a {
padding: 24px 24px 24px 82px;
font-size: 1.9rem;
}
.cd-accordion-menu label::before {
left: 24px;
}
.cd-accordion-menu label::after {
left: 53px;
}
.cd-accordion-menu ul label,
.cd-accordion-menu ul a {
padding-left: 106px;
}
.cd-accordion-menu ul label::before {
left: 48px;
}
.cd-accordion-menu ul label::after,
.cd-accordion-menu ul a::after {
left: 77px;
}
.cd-accordion-menu ul ul label,
.cd-accordion-menu ul ul a {
padding-left: 130px;
}
.cd-accordion-menu ul ul label::before {
left: 72px;
}
.cd-accordion-menu ul ul label::after,
.cd-accordion-menu ul ul a::after {
left: 101px;
}
.cd-accordion-menu ul ul ul label,
.cd-accordion-menu ul ul ul a {
padding-left: 154px;
}
.cd-accordion-menu ul ul ul label::before {
left: 96px;
}
.cd-accordion-menu ul ul ul label::after,
.cd-accordion-menu ul ul ul a::after {
left: 125px;
}
}
非常感谢任何形式的帮助
答案 0 :(得分:1)
好像你忘了添加一个相邻的选择器&#34; + label&#34;在行:
.cd-accordion-menu input[type=checkbox]:checked .has-dropdown::after{
content: "";
}
必须是:
.cd-accordion-menu input[type=checkbox]:checked + label .has-dropdown::after{
content: "";
}