2016-03-29 82 views
0

如何在懸停在鏈接上時在側欄中創建具有小描述的下拉菜單的功能?它可以通過純CSS完成嗎?關於懸停在引導程序的下拉菜單中的頁面描述

http://codepen.io/anon/pen/aNyORv

<nav class="navbar navbar-default" role="navigation"> 
    <div class="navbar-header"> 
     <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> 
     <span class="sr-only">Toggle navigation</span> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     </button> 
    </div> 
    <!--/.navbar-header--> 

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
     <ul class="nav navbar-nav"> 

      <li class="dropdown"> 
       <a href="#" class="dropdown-toggle" data-toggle="dropdown">Two Column <b class="caret"></b></a> 
       <ul class="dropdown-menu multi-column columns-2"> 
        <div class="row"> 
         <div class="col-sm-6"> 
          <ul class="multi-column-dropdown"> 
           <li><a href="#">title 1</a></li> 
           <li><a href="#">title 2</a></li> 
          </ul> 
         </div> 
         <div class="col-sm-6"> 
          <ul class="multi-column-dropdown"> 
           <li>This is visible by default or when hovering over Title 1</li> 
           <li>This is hidden but visible when hovering over Title 2</li> 
          </ul> 
         </div> 
        </div> 
       </ul> 
      </li> 
     </ul> 
    </div> 
    <!--/.navbar-collapse--> 
</nav> 
<!--/.navbar--> 

回答

1

你以後的功能是使用Bootstrap TooltipBootstrap Popover通常提供。兩者都完全可以style可定製。此外,他們是移動友好的(見答案末尾的註釋)。

另外,你可以使用這樣的東西。請注意,你有你的.columns-2>.col-xs-6min-height調整到足夠大,以適應描述的值不使用滾動條(如果有的話描述的是大到足以需要它):

.dropdown-menu.columns-2 { 
 
\t min-width: 400px; 
 
} 
 
.dropdown-menu li a { 
 
\t padding: 5px 15px; 
 
\t font-weight: 300; 
 
} 
 
.multi-column-dropdown { 
 
\t list-style: none; 
 
    margin: 0px; 
 
    padding: 0px; 
 
} 
 
.multi-column-dropdown li a { 
 
\t display: block; 
 
\t clear: both; 
 
\t line-height: 1.428571429; 
 
\t color: #333; 
 
\t white-space: normal; 
 
} 
 
    
 

 
.multi-column-dropdown .columns-2 { 
 
    position: relative; 
 
} 
 
.multi-column-dropdown li a { 
 
    width: 100%; 
 
    height: 100%; 
 
    padding: 7px 15px; 
 
} 
 
.columns-2>.col-xs-6 { 
 
    /* adjust this to accomodate the height of your descriptions on all screens */ 
 
    min-height: 100px; 
 
    
 
} 
 
.multi-column-dropdown li:first-child .li-description { 
 
    display: inline-block; 
 
} 
 
.multi-column-dropdown li .li-description { 
 
    display: none; 
 
    position: absolute; 
 
    width: 100%; 
 
    left: 100%; 
 
    top: 0; 
 
    padding: 7px 15px; 
 
    background-color: #FBFBFB; 
 
} 
 
.multi-column-dropdown li:hover .li-description { 
 
    display: inline-block; 
 
} 
 
@media(min-width: 768px) { 
 
    .multi-column-dropdown li .li-description { 
 
    background-color: white; 
 
    } 
 
    .columns-2>.col-xs-6 { 
 
    /* adjust this to accomodate the height of your descriptions on all screens */ 
 
    min-height: 160px; 
 
    } 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css" rel="stylesheet"/> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 
 

 
<nav class="navbar navbar-default" role="navigation"> 
 
    <div class="navbar-header"> 
 
     <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> 
 
     <span class="sr-only">Toggle navigation</span> 
 
     <span class="icon-bar"></span> 
 
     <span class="icon-bar"></span> 
 
     <span class="icon-bar"></span> 
 
     </button> 
 
    </div> 
 
    <!--/.navbar-header--> 
 

 
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
 
     <ul class="nav navbar-nav"> 
 

 
      <li class="dropdown"> 
 
       <a href="#" class="dropdown-toggle" data-toggle="dropdown">Two Column <b class="caret"></b></a> 
 
       <ul class="dropdown-menu multi-column columns-2"> 
 
        
 
         <div class="col-xs-6"> 
 
          <ul class="multi-column-dropdown"> 
 
           <li><a href="#">title 1</a><div class="li-description">This is visible by default or when hovering over Title 1</div></li> 
 
           <li><a href="#">title 2</a><div class="li-description">This is hidden but visible when hovering over Title 2. It is a longer description and we need <code>min-height</code> on <code>.columns-2>.col-xs-6</code> so it doesn't create a scrollbar...</div></li> 
 
          </ul> 
 
         </div> 
 
         <div class="col-xs-6">&nbsp;</div> 
 
         
 
        
 
       </ul> 
 
      </li> 
 
     </ul> 
 
    </div> 
 
    <!--/.navbar-collapse--> 
 
</nav> 
 
<!--/.navbar-->

您的codepen上的附註:

  • 您正在加載jQuery的兩個版本。這是一個很大的NO NO
  • 您應該在margin: 0 15px;父母內部使用.row,例如.container。這會在您的行上生成不需要的水平滾動條。密切關注Bootstrap的標記。在你的UI架構

旁註:

比今天的互聯網用戶的一半
  • 更多的是從觸摸設備瀏覽。他們應該如何hover並查看說明?
+0

謝謝,我將探索代碼。我知道Bootstrap有工具提示代碼,但我從來沒有想過在這個例子中可能會有用。至於移動用戶,這個描述只對桌面用戶有用,我會試着將其隱藏在其他屏幕上 – Marko