简体   繁体   English

我如何才能使此CSS / jQuery菜单仅在单击时打开,而不是悬停打开?

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

Here is the codepen: http://codepen.io/anon/pen/pvQYog 这是codepen: http ://codepen.io/anon/pen/pvQYog

The code laid out: 代码布局:

HTML 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 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: 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');
  });
});

Also, a second less important question: Is there a way to make TWO items appear per row, rather than one item per row? 另外,还有另一个不太重要的问题:是否有办法使每行显示两个项目,而不是每行显示一个项目?

Replace your ul.navbar li ul with this. ul.navbar li ul替换您的ul.navbar li ul And also remove ul.navbar li:hover > 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);
}

And script would be - 脚本将是-

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

This is the sample using your codes. 这是使用您的代码的示例。 You will need to modify to get what you need. 您将需要进行修改以获取所需的内容。 Hope this might help you. 希望这对您有帮助。 http://codepen.io/khay/pen/bNQZBN http://codepen.io/khay/pen/bNQZBN

You need to use onclick function for your requirment, i ll give you an example below. 您需要使用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>

This function will open a new window for you when you click on the text. 当您单击文本时,此功能将为您打开一个新窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得下拉菜单打开/关闭点击而不是悬停? - How to get dropdown menu to open/close on click rather than hover? 如何在点击而不是悬停时打开Superfish jQuery菜单? - How can I open a Superfish jQuery Menu on click rather than hover? 我在 JQuery 代码上将什么更改为单击文本而不是在悬停时更改? - What do I change on JQuery Code to Click Text rather than change on Hover? 如何在悬停时让此菜单保持在图像的右上角? - CSS,jQuery - How do I get this menu to stay top right of the image on hover? - CSS, jQuery 如何获取第一个元素而不是在 jQuery 中使用 [0]? - How do I get first element rather than using [0] in jQuery? CSS:菜单在悬停时打开,在单击时关闭 - CSS: menu open on hover and close on click 使JS菜单在悬停而不是单击时显示 - Make JS menu show on hover rather than on click 如何获得我的下拉菜单,一次只允许一个打开的下拉菜单,并在单击某个菜单时保持打开状态 <li> ? - How do I get my dropdown menu to allow only one open dropdown at a time, and stay open on click of a <li>? 如何让 Material-UI SpeedDialAction onClick 事件在 SpeedDial 仅在单击时打开(不悬停)时触发 - How do I get Material-UI SpeedDialAction onClick events to fire when SpeedDial is open on click only (not hover) 打开 hover 上的菜单,而不是单击 jQuery - Open menu on hover instead of click with jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM