2017-06-23 99 views
1

我已經在HTML和CSS中編寫了一些代碼。輸出顯示在屏幕截圖中。 當選擇了一個菜單項目(展開後)時,菜單右側的顏色部分消失,同時選擇了上述菜單(在鼠標未懸停該項目後)。 如何在鼠標未在菜單上時保持顏色?CSS多級別摺疊菜單

(請不要提出任何Java腳本代碼。)

CSS Multi-Level Accordion Menu

* { 
 
    margin: 0; 
 
    padding: 0; 
 
    font-family: Arial; 
 
    font-size: 14px; 
 
} 
 

 
.block { 
 
    float: right; 
 
    max-width: 360px; 
 
    width: 100%; 
 
    height: 100%; 
 
    background-color: white; 
 
    overflow: hidden; 
 
    margin: 0; 
 
} 
 

 
    .block > div { 
 
     display: block; 
 
     position: relative; 
 
     background-color: #fff; 
 
     color: #767676; 
 
     border-bottom: 1px solid #e5e5e5; 
 
     border-left: 1px solid #e5e5e5; 
 
    } 
 

 
     .block > div:nth-child(1):before { 
 
      font-family: 'FontAwesome'; 
 
      content: "\F0CA"; 
 
      font-size: 16px; 
 
      width: 50px; 
 
      font-weight: bold; 
 
      text-align: center; 
 
      position: absolute; 
 
      right: 0; 
 
      top: 0; 
 
      line-height: 50px; 
 
      margin: 0; 
 
      color: #767676; 
 
      border-right: 4px solid #E94B3B; 
 
      background: none; /* For browsers that do not support gradients */ 
 
      background: -webkit-linear-gradient(to right, rgba(0,0,0,0) 50%, #E94B3B 50%); /* Safari 5.1-6.0 */ 
 
      background: -o-linear-gradient(to right, rgba(0,0,0,0) 50%, #E94B3B 50%); /* For Opera 11.6-12.0 */ 
 
      background: -moz-linear-gradient(to right, rgba(0,0,0,0) 50%, #E94B3B 50%); /* For Firefox 3.6-15 */ 
 
      background: linear-gradient(to right, rgba(0,0,0,0) 50%, #E94B3B 50%); /* Standard syntax */ 
 
      background-size: 200% 100%; 
 
      transition: all 0.25s ease-in-out; 
 
     } 
 

 
     .block > div:nth-child(1):hover:before { 
 
      background-position: 100% 0; 
 
      color: white; 
 
     } 
 

 
     .block > div:nth-child(2):before { 
 
      font-family: 'FontAwesome'; 
 
      content: "\F0F6"; 
 
      font-size: 16px; 
 
      width: 50px; 
 
      font-weight: bold; 
 
      text-align: center; 
 
      position: absolute; 
 
      right: 0; 
 
      top: 0; 
 
      line-height: 50px; 
 
      margin: 0; 
 
      color: #767676; 
 
      border-right: 4px solid #ffb61c; 
 
      background: none; /* For browsers that do not support gradients */ 
 
      background: -webkit-linear-gradient(to right, rgba(0,0,0,0) 50%, #ffb61c 50%); /* Safari 5.1-6.0 */ 
 
      background: -o-linear-gradient(to right, rgba(0,0,0,0) 50%, #ffb61c 50%); /* For Opera 11.6-12.0 */ 
 
      background: -moz-linear-gradient(to right, rgba(0,0,0,0) 50%, #ffb61c 50%); /* For Firefox 3.6-15 */ 
 
      background: linear-gradient(to right, rgba(0,0,0,0) 50%, #ffb61c 50%); /* Standard syntax */ 
 
      background-size: 200% 100%; 
 
      transition: all 0.25s ease-in-out; 
 
     } 
 

 
     .block > div:nth-child(2):hover:before { 
 
      background-position: 100% 0; 
 
      color: white; 
 
     } 
 

 
     .block > div > input + label { 
 
      cursor: pointer; 
 
      display: block; 
 
      line-height: 50px; 
 
      background-color: white; 
 
      transition: background-color 0.5s; 
 
      color: #767676; 
 
      padding-right: 60px; 
 
      text-align: right; 
 
     } 
 

 
     .block > div > input ~ div { 
 
      visibility: hidden; 
 
      max-height: 0; 
 
      padding: 0; 
 
      opacity: 0; 
 
      transition: all 0.5s; 
 
     } 
 

 

 
     .block > div > input:checked + label { 
 
      background-color: #f3f3f3; 
 
      transition: background-color 0.5s; 
 
      color: black; 
 
     } 
 

 
     .block > div > input:checked ~ div { 
 
      display: block; 
 
      opacity: 1; 
 
      visibility: visible; 
 
      height: auto; 
 
      max-height: 10000px; 
 
      padding: 0; 
 
      transition: all 0.5s; 
 
     } 
 

 
    .block a { 
 
     display: block; 
 
     line-height: 50px; 
 
     text-decoration: none; 
 
     color: black; 
 
     border-top: solid 1px #e5e5e5; 
 
     padding-right: 50px; 
 
     text-align: right; 
 
    } 
 

 
     .block a:hover { 
 
      background: #eeeeee; 
 
     } 
 

 

 
@font-face { 
 
    font-family: 'FontAwesome'; 
 
    src: url('../fonts/FontAwesome.otf') format('opentype'); 
 
}
<div class="block"> 
 
     <div> 
 
      <input type="radio" name="item" id="item-one" hidden /> 
 
      <label for="item-one">First</label> 
 
      <div> 
 
       <p><a href="#">Menu 1.1</a></p> 
 
       <p><a href="#">Menu 1.2</a></p> 
 
       <p><a href="#">Menu 1.3</a></p> 
 
      </div> 
 
     </div> 
 
     <div> 
 
      <input type="radio" name="item" id="item-two" hidden /> 
 
      <label for="item-two">second</label> 
 
      <div> 
 
       <p><a href="#">Menu 2.1</a></p> 
 
       <p><a href="#">Menu 2.2</a></p> 
 
       <p><a href="#">Menu 2.3</a></p> 
 
      </div> 
 
     </div> 
 
    </div>

+0

僅供參考,如果你發佈的jsfiddle與你的工作代碼或者編輯你的帖子以獲得一個嵌入式工作示例,你更可能爲你的問題得到一個可靠的答案。 –

+0

@JamesKraus ok。 –

回答

0

你可以重構你的HTML一下,讓懸停狀態,可以實現與而不是你的輸入上的:checked

首先,將.block>div上的僞元素替換爲span元素,該元素位於label之後。我們需要的東西在input之後,所以我們可以用CSS來選擇它,因爲沒有CSS父選擇器。我們不能在輸入中使用僞元素,所以我們只需添加一個跨度。

<div class="block"> 
    <div> 
    <input type="checkbox" name="item" id="item-one" hidden /> 
    <label for="item-one">First</label> 
    <span></span> 
    <div> 
     <p><a href="#">Menu 1.1</a></p> 
     <p><a href="#">Menu 1.2</a></p> 
     <p><a href="#">Menu 1.3</a></p> 
    </div> 
    </div> 
</div> 

注意,我把小盒子,象徵了你content財產從僞元素和突出部分把它放在span

之後,這只是一個改變你的選擇器的問題。因此,這...

.block > div:nth-child(1):before { ... } 
.block > div:nth-child(1):hover:before { ... } 

變爲這個...

#item-one + label + span { ... } 

.block>div:nth-child(1):hover #item-one + label + span, 
#item-one:checked + label + span { ... } 

這就是它!

你需要添加的span這是不存在的僞元素的顯式高度,以及跨度設定爲display: block。其餘的樣式完全相同。

最後需要說明的是,我將輸入從type="radio"更改爲type="checkbox"。這樣,您可以將它們摺疊起來,而不是一旦單擊它就被迫一直打開。

這裏就是第一個輸入使用新的風格,第二個片段是一樣的你原來的:

* { 
 
    margin: 0; 
 
    padding: 0; 
 
    font-family: Arial; 
 
    font-size: 14px; 
 
} 
 
.block { 
 
    float: right; 
 
    max-width: 360px; 
 
    width: 100%; 
 
    height: 100%; 
 
    background-color: white; 
 
    overflow: hidden; 
 
    margin: 0; 
 
} 
 
.block>div { 
 
    display: block; 
 
    position: relative; 
 
    background-color: #fff; 
 
    color: #767676; 
 
    border-bottom: 1px solid #e5e5e5; 
 
    border-left: 1px solid #e5e5e5; 
 
} 
 
#item-one + label + span { 
 
    display: block; 
 
    font-family: 'FontAwesome'; 
 
    content: "\F0CA"; 
 
    font-size: 16px; 
 
    width: 50px; 
 
    height: 50px; 
 
    font-weight: bold; 
 
    text-align: center; 
 
    position: absolute; 
 
    right: 0; 
 
    top: 0; 
 
    line-height: 50px; 
 
    margin: 0; 
 
    color: #767676; 
 
    border-right: 4px solid #E94B3B; 
 
    background: none; 
 
    /* For browsers that do not support gradients */ 
 
    background: -webkit-linear-gradient(to right, rgba(0, 0, 0, 0) 50%, #E94B3B 50%); 
 
    /* Safari 5.1-6.0 */ 
 
    background: -o-linear-gradient(to right, rgba(0, 0, 0, 0) 50%, #E94B3B 50%); 
 
    /* For Opera 11.6-12.0 */ 
 
    background: -moz-linear-gradient(to right, rgba(0, 0, 0, 0) 50%, #E94B3B 50%); 
 
    /* For Firefox 3.6-15 */ 
 
    background: linear-gradient(to right, rgba(0, 0, 0, 0) 50%, #E94B3B 50%); 
 
    /* Standard syntax */ 
 
    background-size: 200% 100%; 
 
    transition: all 0.25s ease-in-out; 
 
} 
 
.block>div:nth-child(1):hover #item-one + label + span, 
 
#item-one:checked + label + span { 
 
    background-position: 100% 0; 
 
    color: white; 
 
} 
 
.block>div:nth-child(2):before { 
 
    font-family: 'FontAwesome'; 
 
    content: "\F0F6"; 
 
    font-size: 16px; 
 
    width: 50px; 
 
    font-weight: bold; 
 
    text-align: center; 
 
    position: absolute; 
 
    right: 0; 
 
    top: 0; 
 
    line-height: 50px; 
 
    margin: 0; 
 
    color: #767676; 
 
    border-right: 4px solid #ffb61c; 
 
    background: none; 
 
    /* For browsers that do not support gradients */ 
 
    background: -webkit-linear-gradient(to right, rgba(0, 0, 0, 0) 50%, #ffb61c 50%); 
 
    /* Safari 5.1-6.0 */ 
 
    background: -o-linear-gradient(to right, rgba(0, 0, 0, 0) 50%, #ffb61c 50%); 
 
    /* For Opera 11.6-12.0 */ 
 
    background: -moz-linear-gradient(to right, rgba(0, 0, 0, 0) 50%, #ffb61c 50%); 
 
    /* For Firefox 3.6-15 */ 
 
    background: linear-gradient(to right, rgba(0, 0, 0, 0) 50%, #ffb61c 50%); 
 
    /* Standard syntax */ 
 
    background-size: 200% 100%; 
 
    transition: all 0.25s ease-in-out; 
 
} 
 
.block>div:nth-child(2):hover:before { 
 
    background-position: 100% 0; 
 
    color: white; 
 
} 
 
.block>div>input+label { 
 
    cursor: pointer; 
 
    display: block; 
 
    line-height: 50px; 
 
    background-color: white; 
 
    transition: background-color 0.5s; 
 
    color: #767676; 
 
    padding-right: 60px; 
 
    text-align: right; 
 
} 
 
.block>div>input~div { 
 
    visibility: hidden; 
 
    max-height: 0; 
 
    padding: 0; 
 
    opacity: 0; 
 
    transition: all 0.5s; 
 
} 
 
.block>div>input:checked+label { 
 
    background-color: #f3f3f3; 
 
    transition: background-color 0.5s; 
 
    color: black; 
 
} 
 
.block>div>input:checked~div { 
 
    display: block; 
 
    opacity: 1; 
 
    visibility: visible; 
 
    height: auto; 
 
    max-height: 10000px; 
 
    padding: 0; 
 
    transition: all 0.5s; 
 
} 
 
.block a { 
 
    display: block; 
 
    line-height: 50px; 
 
    text-decoration: none; 
 
    color: black; 
 
    border-top: solid 1px #e5e5e5; 
 
    padding-right: 50px; 
 
    text-align: right; 
 
} 
 
.block a:hover { 
 
    background: #eeeeee; 
 
} 
 
@font-face { 
 
    font-family: 'FontAwesome'; 
 
    src: url('../fonts/FontAwesome.otf') format('opentype'); 
 
}
<div class="block"> 
 
    <div> 
 
    <input type="checkbox" name="item" id="item-one" hidden /> 
 
    <label for="item-one">First</label> 
 
    <span></span> 
 
    <div> 
 
     <p><a href="#">Menu 1.1</a></p> 
 
     <p><a href="#">Menu 1.2</a></p> 
 
     <p><a href="#">Menu 1.3</a></p> 
 
    </div> 
 
    </div> 
 
    <div> 
 
    <input type="checkbox" name="item" id="item-two" hidden /> 
 
    <label for="item-two">second</label> 
 
    <div> 
 
     <p><a href="#">Menu 2.1</a></p> 
 
     <p><a href="#">Menu 2.2</a></p> 
 
     <p><a href="#">Menu 2.3</a></p> 
 
    </div> 
 
    </div> 
 
</div>

+0

謝謝你回答這個問題♥ –

+0

絕對!此解決方案是否適合您? – cjl750

+0

是的。你是最棒的。 ty –