簡體   English   中英

我如何才能使此CSS / jQuery菜單僅在單擊時打開,而不是懸停打開?

[英]How do I get this CSS/jQuery menu to open only on click, rather than hover?

這是codepen: http ://codepen.io/anon/pen/pvQYog

代碼布局:

HTML

<center>  
<div style="vertical-align:top;">

    <ul class="navbar cf">

            <!-- <li><a href="#">item 2</a></li> -->
            <li style="width:200px;"><a href="#" class="ActiveListItem">Category</a>
                <ul>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li><a href="#">6</a></li>
                    <li><a href="#">7</a></li>
                                <li><a href="#">More...</a>
                        <ul>
                            <li><a href="#">8</a></li>
                            <li><a href="#">9</a></li>
                            <li><a href="#">10</a></li>
                            <li><a href="#">11</a></li>
                            <li><a href="#">12</a>

                        </ul>
                    </li> 

                </ul>
            </li>

        </ul>
       </div>        

CSS

/* clearfix */
/**
 * For modern browsers
 * 1. The space content is one way to avoid an Opera bug when the
 *    contenteditable attribute is included anywhere else in the document.
 *    Otherwise it causes space to appear at the top and bottom of elements
 *    that are clearfixed.
 * 2. The use of `table` rather than `block` is only necessary if using
 *    `:before` to contain the top-margins of child elements.
 */
.cf:before,
.cf:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.cf:after {
    clear: both;
}

/**
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */
.cf {
    *zoom: 1;
}


ul.navbar {

  background:white; 
  border-style:solid;
  border-color:gray;
  border-width:1px;
  width: 200px;
  border-radius: 4px;

}


.ActiveListItem:after {
    content: '\25BC';
    float:right;
    font-weight:bold;
    padding: 0px 0px;
    font-size:100%;


}




ul.navbar li a.ActiveListItem {
    background:white !important;
    color:black;
    border-style:solid;
    border-color:white;
    border-radius:14px;
    padding:3px 5px !important;
    font-weight:normal !important;
    margin-left:14px;/* got the activeitem centered with the list text this way*/
    margin-right:0px;

}

ul.navbar li {


    position: relative;
}

ul.navbar li a {

    display: block;
    color: white;
    padding:10px 5px;
    text-decoration:none;
    transition: all .2s ease-in;

}

ul.navbar li a:hover,
ul.navbar li:hover > a {
    background:#a6d0e1;
    color: #333;
    font-weight:900;
}

ul.navbar li ul {

    margin-top: 1px;     /*Dictates how far away textbox is from list items*/
    position: absolute;
    background: #222;
    left: em;
    font-size: 14px;
    min-width: 200px;
    opacity: 0;
    visibility: hidden;
    z-index: 99;
    box-shadow: inset 0 2px 3px rgba(0,0,0,.6),
                            0 5px 10px rgba(0,0,0,.6);
    /*transition: all .1s ease-in-out;*/              /*the sideways opener*/
}



ul.navbar li:hover > ul { opacity: 1; visibility: visible; left: 0; }

ol, ul { list-style: outside none none; }

.hidden { display: none; }

JS:

// sub menus identification
$(function() {
  $('.navbar ul li a').click(function(){
    $('.navbar > li:first-child > a').text($(this).text());
    $('.navbar > li > ul').addClass('hidden');
  });
  $('.navbar > li').mouseenter(function(){
    $(this).find('ul').removeClass('hidden');
  });
});

另外,還有另一個不太重要的問題:是否有辦法使每行顯示兩個項目,而不是每行顯示一個項目?

ul.navbar li ul替換您的ul.navbar li ul 並同時刪除ul.navbar li:hover > ul

ul.navbar li ul {
    margin-top: 1px;
    position: absolute;
    background: #222;
    font-size: 14px;
    min-width: 200px;
    display: none;
    z-index: 99;
    box-shadow: inset 0 2px 3px rgba(0,0,0,.6),
    0 5px 10px rgba(0,0,0,.6);
}

腳本將是-

$(function() {
    $('.ActiveListItem').click(function(){      
        $('.navbar li ul').toggle();
    });
});

這是使用您的代碼的示例。 您將需要進行修改以獲取所需的內容。 希望這對您有幫助。 http://codepen.io/khay/pen/bNQZBN

您需要使用onclick函數來滿足您的要求,下面我將舉一個例子。

<a onclick="window.open('/your url','name','scrollbars=1, width=600, height=800')" target="_blank">
    <span style="cursor:pointer">
          <p style="text-align:right;cursor:pointer;">
              <small>tester</small>
          </p>
    </span>
</a>

當您單擊文本時,此功能將為您打開一個新窗口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM