简体   繁体   English

链接到移动设备的悬停下拉菜单

[英]Link with hover dropdown menu for Mobile Devices

I've found an issue I just can't seem to solve. 我发现了一个我似乎无法解决的问题。

I've got a navigation, 5 links in total. 我有一个导航,总共5个链接。 One of the links has a dropdown menu when you hover over it showing 3 more links. 将鼠标悬停在其中一个链接上时,会显示一个下拉菜单,显示另外3个链接。

Fine when a mouse is involved. 涉及鼠标时很好。 But when you start using touch devices, the parent link consumes all gestures and taps, and the viewer is shown the dropdown for a fraction of a second before being taken to the parent's link page. 但是,当您开始使用触摸设备时,父链接会消耗所有手势和轻击,并且向查看者显示下拉菜单仅需不到一秒钟的时间,然后才会转到父链接页面。

I'm wondering if there's a way of making it so the first touch of the parent link shows the dropdown menu, then a second touch would go to that link. 我想知道是否有一种方法可以使父链接的第一触摸显示下拉菜单,然后再对该链接进行第二触摸。 touching anything else would just hide the dropdown. 触摸其他任何东西只会隐藏下拉菜单。

<ul id="main-menu">
    <li><a href="#">Link</a></li>
    <li><a href="#">Link</a></li>
    <li><a href="#">Link</a>
        <ul class="sub-menu">
           <li><a href="#">Sub Link</a></li>
            <li><a href="#">Sub Link</a></li>
            <li><a href="#">Sub Link</a></li>
        </ul>
    </li>
    <li><a href="#">Link</a></li>
    <li><a href="#">Link</a></li>
</ul>

Anyone have any ideas? 有人有想法么? jQuery would be ideal jQuery将是理想的

Something like this perhaps? 大概是这样吗? You may want to customize the behavior of the dropdown, but this shows the basic logic of handling the click events and preventing default behaviour (ie following links) if the menu isn't open: 您可能想要自定义下拉菜单的行为,但这显示了在未打开菜单的情况下处理单击事件并防止默认行为(即,以下链接)的基本逻辑:

$(function() {
    $('#main-menu a').click(function(e) {
        var listItem = $(this).closest('li');
        if (!listItem.is('.open')) {
            // Opening drop-down logic here. e.g. adding 'open' class to <li>
            e.preventDefault();
            listItem.addClass('open');
        }
        // Otherwise the default behaviour of the event (clicking the link) will be unaffected
    });
});

i have done complete bins for above issue also placed demo link here. 我已经完成了以上问题的完整垃圾箱,并在此处放置了演示链接。

Demo: http://codebins.com/bin/4ldqp72 演示: http //codebins.com/bin/4ldqp72

HTML HTML

<ul id="main-menu">
  <li>
    <a href="#">
      Link
    </a>
  </li>
  <li>
    <a href="#">
      Link
    </a>
  </li>
  <li>
    <a href="#">
      Link
    </a>
    <ul class="sub-menu">
      <li>
        <a href="#">
          Sub Link
        </a>
      </li>
      <li>
        <a href="#">
          Sub Link
        </a>
      </li>
      <li>
        <a href="#">
          Sub Link
        </a>
      </li>
    </ul>
  </li>
  <li>
    <a href="#">
      Link
    </a>
  </li>
  <li>
    <a href="#">
      Link
    </a>
  </li>
</ul>

JQuery JQuery的

$(function() {
    $('ul a').click(function(e) {
        $('#main-menu li').removeClass('open');
        e.preventDefault();
        $(this).closest('li').addClass("open");
        var pos = $(this).closest('li.open').offset();
        $(this).closest('li.open').find("ul.sub-menu").css('top', pos.top + 'px');

    });
});

CSS CSS

#main-menu{
  list-style:none;
  margin:2px;
  padding:2px;
}
li{
  border:1px solid #333;
  background:#ebcdff;
  text-align:center;
  width:100px;
}
li:hover{
  background:#abcdfd;
}
li:hover a{
  color:#ff3322;
}
li a{
  text-decoration:none;
  color:#2466ff;

}
li.open {
  background:#abcdfd;

}
li.open a{
  text-decoration:none;
  color:#ff3322;

}
ul.sub-menu{
  list-style:none;
  display:none;
}
li.open > ul{
  position:absolute;
  left:70px;
  display:block;
}

Demo: http://codebins.com/bin/4ldqp72 演示: http //codebins.com/bin/4ldqp72

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM