简体   繁体   English

用鼠标在悬停上打开/关闭导航,在移动设备上触摸?

[英]open/close nav on hover with mouse, touch on mobile?

My navigation is a short bar that is fixed to the top of the screen. 我的导航栏是固定在屏幕顶部的短栏。 If you hover over it, it expands to show you the menu options and collapses if you roll out of it. 如果将鼠标悬停在该菜单上,它将展开以显示菜单选项,而如果您将其展开则将折叠。 This works great on the desktop (and actually even modern Android devices where you can click inside the nav to open it, and click outside to close it). 这在台式机上非常有效(实际上甚至在现代Android设备上,您都可以在导航栏中单击以将其打开,然后在外部单击以将其关闭)。 On iOS devices, touching the nav will open it, but touching outside the nav will NOT close it, except in odd cases like touching an image. 在iOS设备上,触摸导航将打开它,但触摸导航外部不会将其关闭,除非在特殊情况下,例如触摸图像。

I could rewrite the nav so it opens/closes on click across the board, but that feels clunky on the desktop. 我可以重写导航,以便在单击时全开即可关闭,但在桌面上感觉很笨拙。 Having it open and close on hover feels more discoverable and snappy. 悬停时打开和关闭它会更容易发现和捕捉。

Have you folks ever come across this? 你们有没有遇到过这个? Is it possible to have it work differently on mobile? 是否可以在移动设备上以其他方式工作? Should it? 应该是?

There's a few ways to solve this - here's the fastest (hackiest) one: 有几种方法可以解决此问题-这是最快(最骇人)的一种:

Create a hidden input (or you can use a regular input if you've always got on on your page). 创建一个隐藏的输入(或者,如果您总是在页面上,可以使用常规输入)。 Listen for a "touchstart" event on the body - if the target is inside your nav element you can ignore it, OTHERWISE, focus and blur that input element simultaneously. 侦听身体上的“ touchstart”事件-如果目标在导航元素内,则可以忽略它,否则,同时聚焦和模糊该输入元素。 This will remove focus from your "hovered" nav item. 这会将焦点从“悬停”的导航项目上移开。

This is a piece of code I recently used (uses Modernizr and jQuery): 这是我最近使用的一段代码(使用Modernizr和jQuery):

            Modernizr.touch && $('body').on('touchstart',function(e) {
                $('input',$('#search')).focus().blur();
            });

也许像@media这样的东西可以帮助您: http : //www.w3schools.com/css/css_mediatypes.asp

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

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