2016-08-22 65 views
0

在這個Family Tree當我們將鼠標懸停在任何節點上時,它的所有子節點都將被高亮顯示。但我想要的是當懸停或點擊任何節點時,其父節點的路徑應該突出顯示。我附上了需求的示例圖片。 pathToParent。如果可能的話,任何人都可以幫助我呢?如果我們可以使用javascript/jquery來實現這一點也沒關係。需要使用css從子項路徑到父項

CSS:

* {margin: 0; padding: 0;} 

.tree ul { 
    padding-top: 20px; position: relative; 

    transition: all 0.5s; 
    -webkit-transition: all 0.5s; 
    -moz-transition: all 0.5s; 
} 

.tree li { 
    float: left; text-align: center; 
    list-style-type: none; 
    position: relative; 
    padding: 20px 5px 0 5px; 

    transition: all 0.5s; 
    -webkit-transition: all 0.5s; 
    -moz-transition: all 0.5s; 
} 

/*We will use ::before and ::after to draw the connectors*/ 

.tree li::before, .tree li::after{ 
    content: ''; 
    position: absolute; top: 0; right: 50%; 
    border-top: 1px solid #ccc; 
    width: 50%; height: 20px; 
} 
.tree li::after{ 
    right: auto; left: 50%; 
    border-left: 1px solid #ccc; 
} 

/*We need to remove left-right connectors from elements without 
any siblings*/ 
.tree li:only-child::after, .tree li:only-child::before { 
    display: none; 
} 

/*Remove space from the top of single children*/ 
.tree li:only-child{ padding-top: 0;} 

/*Remove left connector from first child and 
right connector from last child*/ 
.tree li:first-child::before, .tree li:last-child::after{ 
    border: 0 none; 
} 
/*Adding back the vertical connector to the last nodes*/ 
.tree li:last-child::before{ 
    border-right: 1px solid #ccc; 
    border-radius: 0 5px 0 0; 
    -webkit-border-radius: 0 5px 0 0; 
    -moz-border-radius: 0 5px 0 0; 
} 
.tree li:first-child::after{ 
    border-radius: 5px 0 0 0; 
    -webkit-border-radius: 5px 0 0 0; 
    -moz-border-radius: 5px 0 0 0; 
} 

/*Time to add downward connectors from parents*/ 
.tree ul ul::before{ 
    content: ''; 
    position: absolute; top: 0; left: 50%; 
    border-left: 1px solid #ccc; 
    width: 0; height: 20px; 
} 

.tree li a{ 
    border: 1px solid #ccc; 
    padding: 5px 10px; 
    text-decoration: none; 
    color: #666; 
    font-family: arial, verdana, tahoma; 
    font-size: 11px; 
    display: inline-block; 

    border-radius: 5px; 
    -webkit-border-radius: 5px; 
    -moz-border-radius: 5px; 

    transition: all 0.5s; 
    -webkit-transition: all 0.5s; 
    -moz-transition: all 0.5s; 
} 

/*Time for some hover effects*/ 
/*We will apply the hover effect the the lineage of the element also*/ 
.tree li a:hover, .tree li a:hover+ul li a { 
    background: #c8e4f8; color: #000; border: 1px solid #94a0b4; 
} 
/*Connector styles on hover*/ 
.tree li a:hover+ul li::after, 
.tree li a:hover+ul li::before, 
.tree li a:hover+ul::before, 
.tree li a:hover+ul ul::before{ 
    border-color: #94a0b4; 
} 

HTML:

<div class="tree"> 
    <ul> 
     <li> 
      <a href="#">Parent</a> 
      <ul> 
       <li> 
        <a href="#">Child</a> 
        <ul> 
         <li> 
          <a href="#">Grand Child</a> 
         </li> 
        </ul> 
       </li> 
       <li> 
        <a href="#">Child</a> 
        <ul> 
         <li><a href="#">Grand Child</a></li> 
         <li> 
          <a href="#">Grand Child</a> 
          <ul> 
           <li> 
            <a href="#">Great Grand Child</a> 
           </li> 
           <li> 
            <a href="#">Great Grand Child</a> 
           </li> 
           <li> 
            <a href="#">Great Grand Child</a> 
           </li> 
          </ul> 
         </li> 
         <li><a href="#">Grand Child</a></li> 
        </ul> 
       </li> 
      </ul> 
     </li> 
    </ul> 
</div> 
+0

添加一些代碼在這裏 –

+0

我不認爲這是可能的,而不使用JavaScript,但納伊姆說,添加一些代碼。 –

+0

我認爲用css是不可能的。您無法從兒童訪問父母。 –

回答

1

This是你如何突出節點。

CSS

li.active > a { 
    background: #c8e4f8; 
    color: #000; 
    border: 1px solid #94a0b4; 
} 

jQuery的

$(document).ready(function(){ 
    $('a').mouseenter(function(){ 
    $(this).parents('li').addClass('active'); 
    }) 
    $('a').mouseout(function(){ 
    $(this).parents('li').removeClass('active'); 
    }) 
}) 
+0

父節點突出顯示非常好。也可以用類似的方式突出顯示路徑嗎? – smart987

+0

不太確定。您可能需要更改完整的結構。 – Tushar

相關問題