iPad/iPhone 悬停问题导致用户双击链接
我有一些我以前建立的网站,使用 jquery 鼠标事件...我刚买了一个 ipad,我注意到所有鼠标悬停事件都被转换为单击...所以例如我必须执行两次单击而不是一次单击..(第一次悬停,而不是实际点击)
是否有解决方法可以解决这个问题?也许我应该使用 jquery 命令而不是 mouseover/out 等。 谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
只需进行一个排除平板电脑和移动设备的 CSS 媒体查询,然后将鼠标悬停在其中即可。为此,您实际上并不需要 jQuery 或 JavaScript。
请务必将其添加到您的 html 头部,以确保它使用实际像素而不是分辨率进行计算。
Simply make a CSS media query which excludes tablets and mobile devices and put the hover in there. You don't really need jQuery or JavaScript for this.
And be sure to add this to your html head to make sure that it calculates with the actual pixels and not the resolution.
您可以像这样检查
navigator.userAgent
:但是您必须检查黑莓,机器人,以及其他触摸屏设备。仅当 userAgent 包含 Mozilla、IE、Webkit 或 Opera 时,您也可以绑定鼠标悬停,但您仍然需要筛选某些设备,因为例如 Droid 将其 userAgent 字符串报告为:
iPhone 的字符串类似。如果您只筛选 iPhone、iPod、iPad、Android 和 Blackberry,您可能会获得大多数手持设备,但不是全部。
You could check
navigator.userAgent
like this:but you would have to check for blackberries, droids, umpty other touchscreen devices. You could also bind the mouseovers only if the userAgent contains Mozilla, IE, Webkit, or Opera, but you still need to screen for some devices because the Droid, for instance, reports its userAgent string as:
The iPhone's string is similar. If you just screen for iPhone, iPod, iPad, Android and Blackberry you might get the majority of handhelds, but not all of them.
解决 iPad 上双击问题的最简单方法是将您的 css 包装在媒体查询中以实现悬停效果
@media (pointer:fine)
:包装在此媒体查询中的 CSS 仅适用于桌面。
此解决方案的说明位于此处 https://css-tricks.com /烦人的移动双击链接问题/
Simplest way to resolve double-click on IPad is wrapping your css for hover effect in media query
@media (pointer: fine)
:CSS that wrapped in this media query will applying only on desktop.
Explanation of this solution is here https://css-tricks.com/annoying-mobile-double-tap-link-issue/
感谢 MacFreek 的回答 我想出了这个解决方案。当我发现有时连续有两个 mousemove 事件但未触发 click 事件时,它变得更高级了。
Thanks to MacFreek's answer I came up with this solution. It became a bit more advanced when I discovered that sometimes there were two mousemove events in a row where the click event was not fired.
避免更改悬停 CSS 事件内的“显示”样式。我的“显示:阻止”处于悬停状态。删除 ios 后,单击即可进入 lins。顺便说一句,最新的 IOS 更新似乎修复了这个“功能”
Avoid changing of "display" style inside of hover css event. I had "display: block" in hover state. After removing ios went on lins by single tap. By the way it seems that latest IOS updates fixed this "feature"
当你有 jquery ui 下拉菜单时,这对我有用
This works for me when you have jquery ui dropdown
其他答案都不适合我。我的应用程序有很多事件侦听器、自己的复选框和具有侦听器的链接以及没有侦听器的链接。
我用这个:
None from the other answer works for me. My app has a lot of event listeners, own checkboxes and links that has listener's and links without listener's.
I use this:
这个简短的片段似乎有效。
点击链接时触发点击事件:
This short snippet seems to work.
Trigger the click event when link tapped :
我知道我太晚了,但这是我发现的最简单的解决方法之一:
这不仅适用于链接(锚标记),而且也适用于其他元素。希望这有帮助。
I am too late, I know but this is one of the easiest workaround I've found:
This does not only works for links(anchor tags) but for other elements also. Hope this helps.
为了让链接在不中断触摸滚动的情况下工作,我使用 jQuery Mobile 的“tap”事件解决了这个问题:
To get the links working without breaking touch scrolling, I solved this with jQuery Mobile's "tap" event:
受到 MacFreak 的启发,我整合了一些适合我的东西。
此 js 方法可防止悬停在 ipad 上,并防止在某些情况下将单击注册为两次单击。在 CSS 中,如果 css 中有任何 :hover psudo 类,请将它们更改为 .hover 例如 .some-class:hover 更改为 .some-class.hover
在 ipad 上测试此代码以查看 css 和 js 悬停方法的行为不同(仅在悬停效果中)。 CSS 按钮没有精美的点击警报。
http://jsfiddle.net/bensontrent/ctgr6stm/
With inspiration from MacFreak, I put together something that works for me.
This js method prevents hover from sticking on an ipad, and prevents the click registering as two clicks in some cases. In CSS, if you have any :hover psudo classes in your css, change them to .hover For example .some-class:hover to .some-class.hover
Test this code on an ipad to see how css and js hover method behave differently (in hover effect only). The CSS button doesn't have a fancy click alert.
http://jsfiddle.net/bensontrent/ctgr6stm/
只是一项改进,以避免当您错误地在链接上滑动手指时避免重定向。
Just an improvement to avoid redirection when you slide your finger on a link by mistake.
我遇到了类似的情况,我将事件绑定到元素的 mouseenter/mouseleave/click 状态,但在 iPhone 上,用户必须双击该元素才能首先触发 mouseenter 事件,然后再次触发 click 事件。
我使用与上面类似的方法解决了这个问题,但我使用了 jQuery $.browser 插件(对于 jQuery 1.9>),并向 mouseenter 绑定事件添加了 .trigger 事件,如下所示
:在 iPhone 或 iPad 上查看时,通过在元素的 mouseenter(或初始单击)上触发 .click 事件处理程序来双击该元素。可能不是最优雅的解决方案,但它在我的情况下效果很好,并且利用了我已经安装的插件,并且要求我添加一行代码以使我的现有事件在这些设备下工作。
您可以在此处获取 jQuery $.browser 插件: https://github.com/gabceb/jquery-浏览器插件
I ran into a similar situation where I had events binded to the mouseenter/mouseleave/click states of an element, yet on an iPhone, the user had to double click the element to first trigger the mouseenter event, then again to fire the click event.
I resolved this using a similar method as above, but I made use of the jQuery $.browser plugin (for jQuery 1.9>) and added a .trigger event to the mouseenter binding event, as follows:
The .trigger prevents the need to double click the element by firing the .click event handler upon mouseenter (or initial click) of the element when viewed on iPhones or iPads. Might not be the most elegant solution, but it works great in my case and utilizes a plugin that I already had in place, and required me to add a single line of code to get my existing events working under these devices.
You can get the jQuery $.browser plugin here: https://github.com/gabceb/jquery-browser-plugin
您可以使用
click touchend
,示例:
上面的示例将影响触摸设备上的所有链接。
如果您只想定位特定链接,可以通过在它们上设置一个类来实现,即:
HTML:
防止触摸设备上的额外点击
Jquery:
干杯,
Jeroen
You can use
click touchend
,example:
Above example will affect all links on touch devices.
If you want to target only specific links, you can do this by setting a class on them, ie:
HTML:
<a href="example.html" class="prevent-extra-click">Prevent extra click on touch device</a>
Jquery:
Cheers,
Jeroen
如果您使用 Modernizr,那么使用前面提到的 Modernizr.touch 就非常容易。
然而,为了安全起见,我更喜欢结合使用 Modernizr.touch 和用户代理测试。
如果您不使用 Modernizr,则只需将上面的
Modernizr.touch
函数替换为('ontouchstart' in document.documentElement)
另请注意,测试用户代理 iemobile将为您提供比 Windows Phone 更广泛的检测到的 Microsoft 移动设备。
If you use Modernizr, it is very easy to use Modernizr.touch as mentioned earlier.
However, I prefer using a combination of Modernizr.touch and user agent testing, just to be safe.
If you don't use Modernizr, you can simply replace the
Modernizr.touch
function above with('ontouchstart' in document.documentElement)
Also note that testing the user agent iemobile will give you broader range of detected Microsoft mobile devices than Windows Phone.
我遇到了同样的问题,但不是在触摸设备上。每次单击时都会触发该事件。有一些关于事件队列之类的事情。
然而,我的解决方案是这样的:
在单击事件(或触摸?)上设置一个计时器。如果在 X 毫秒内再次点击该链接,则返回 false。
要设置每个元素计时器,您可以使用
$.data()
。这也可以解决上面描述的@Ferdy 问题。
I had the same problem but not on a touch device. The event triggers every time you click. There is something about event queuing or so.
However, my solution was like this:
On click event (or touch?) you set a timer. If the link is clicked again within X ms, you just return false.
To set per element timer, you can use
$.data()
.This also may fix the @Ferdy problem described above.
我“认为”您的链接没有 onmouseover 事件,其中 1 个点击激活 onmouseover ,双击激活链接。但我不知道。我没有 iPad。
我认为你必须使用手势/触摸事件。
https://developer.apple.com/documentation/webkitjs
I "think" that your links have no onmouseover event, where 1 tap activates onmouseover and the double tap activates the link. but idk. I don't have an iPad.
I think ya gotta use gesture/touch events.
https://developer.apple.com/documentation/webkitjs
我刚刚发现如果你添加一个空监听器它就可以工作,不要问我为什么,但我在装有 iOS 9.3.2 的 iPhone 和 iPad 上测试了它,它工作得很好。
I just found out that it works if you add an empty listener, don't ask me why, but I tested it on iPhone and iPad with iOS 9.3.2 and it worked fine.
MacFreak 的回答对我非常有帮助。这里有一些实践代码,希望对您有帮助。
问题 - 应用touchend意味着每次您在某个元素上滚动手指时,它都会做出响应,就像您按下了它一样,即使您只是想滚动过去。
我正在使用 jQuery 创建一个效果,它会在某些按钮下淡出一条线以“突出显示”悬停的按钮。我不希望这意味着您必须在触摸设备上按两次按钮才能点击链接。
以下是按钮:
我希望“menu_underline”div 在鼠标悬停时淡出并在鼠标移出时淡出。但我希望触摸设备能够通过单击而不是两次单击来跟踪链接。
解决方案 - 这是使其工作的 jQuery:
非常感谢您对此 MacFreak 的帮助。
MacFreak's answer was extremely helpful to me. Here's some hands-on code in case it helps you.
PROBLEM - applying touchend means every time you scroll your finger over an element, it responds as if you've pressed it, even if you were just trying to scroll past.
I'm creating an effect with jQuery which fades up a line under some buttons to "highlight" the hovered button. I do not want this to mean you have to press the button twice on touch devices to follow the link.
Here are the buttons:
I want the "menu_underline" div to fade up on mouseover and fade out on mouseout. BUT I want touch devices to be able to follow the link on one single click, not two.
SOLUTION - Here's the jQuery to make it work:
Many thanks for your help on this MacFreak.
我在现有的解决方案中遇到了以下问题,并找到了似乎可以解决所有问题的方法。这假设您的目标是跨浏览器、跨设备,并且不希望设备嗅探。
此解决的问题
仅使用
touchstart
或touchend
:在
touchstart
上触发mouseover
事件和在touchmove
上触发mouseout
的后果不太严重,但会干扰通常的浏览器行为,例如:touchstart
上手指的位置视为mouseover
,在下一个touchstart
时进行mouseout
>。因此,在 Android 中查看鼠标悬停内容的一种方法是触摸感兴趣的区域并摆动手指,轻轻滚动页面。将touchmove
视为mouseout
会打破这一点。解决方案
理论上,您只需使用
touchmove
添加一个标志即可,但 iPhone 即使没有移动也会触发 touchmove。理论上,您可以比较touchstart
和touchend
事件pageX
和pageY
但是在 iPhone 上,没有touchend
pageX< /code> 或
pageY
。
不幸的是,要涵盖所有基础,它最终会变得更加复杂一些。
理论上,有方法来获取longpress 而不是仅仅使用 1000 作为近似值,但实际上并没有那么简单,最好使用合理的代理。
I had the following problems with the existing solutions, and found something that seems to solve all of them. This assumes you're aiming for something cross browser, cross device, and don't want device sniffing.
The problems this solves
Using just
touchstart
ortouchend
:Triggering
mouseover
events ontouchstart
andmouseout
ontouchmove
has less serious consequences, but does interfere with the usual browser behaviour, for example:touchstart
like amouseover
, which ismouseout
ed on the nexttouchstart
. One way to see mouseover content in Android is therefore to touch the area of interest and wiggle your finger, scrolling the page slightly. Treatingtouchmove
asmouseout
breaks this.The solution
In theory, you could just add a flag with
touchmove
, but iPhones trigger touchmove even if there's no movement. In theory, you could just compare thetouchstart
andtouchend
eventpageX
andpageY
but on iPhones, there's notouchend
pageX
orpageY
.So unfortunately to cover all bases it does end up a little more complicated.
In theory, there are ways to get the exact time used for a longpress instead of just using 1000 as an approximation, but in practice it's not that simple and it's best to use a reasonable proxy.
cduruk 的解决方案非常有效,但在我网站的某些部分造成了问题。因为我已经使用 jQuery 来添加 CSS 悬停类,所以最简单的解决方案就是不在移动设备上添加 CSS 悬停类(或者更准确地说,仅在不在移动设备上时添加它)。
总体思路如下:
*出于说明目的减少了代码
cduruk's solution was quite effective, but caused problems on a few parts of my site. Because I was already using jQuery to add the CSS hover class, the easiest solution was to simply not add the CSS hover class on mobile devices (or more precisely, to ONLY add it when NOT on a mobile device).
Here was the general idea:
*code reduced for illustrative purposes
对我有用的是这里其他人已经说过的:
不要在悬停或鼠标移动时显示/隐藏元素(这是我的情况下的事件)。
以下是苹果公司的说法(https://developer. apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html):
因此,您可以使用@woop的解决方案:检测userAgent,检查它是否是iOS设备,然后绑定事件。我最终使用了这种技术,因为它适合我的需求,并且当您不需要时不绑定悬停事件更有意义。
但是...如果你不想弄乱 userAgents 并且仍然在悬停/鼠标移动时隐藏/显示元素,我发现你可以通过使用本机 JavaScript 来做到这一点,如下所示:
这将在桌面版本上工作,并且不会执行任何操作在移动版本上。
为了更多的兼容性......
What worked for me is what others here have already said:
Don't show/hide elements on hover or mousemove (which is the event in my case).
Here's what Apple says (https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html):
So, you could use @woop's solution: detect the userAgent, check if it's and iOS device and then bind the event. I ended up using this technique because it suits my needs and it makes more sense do not bind hover events when you don't want it.
But... if you don't wanna mess with userAgents and still hide/show elements on hover/mousemove, i found out you can do so by using native javascript, like this:
This will work on the Desktop version and will do nothing on the mobile version.
And for a little more compatibility...
我认为尝试使用
mouseenter
代替mouseover
是明智的做法。这是绑定到.hover(fn,fn)
时在内部使用的内容,通常也是您想要的内容。I think it'd be wise to try
mouseenter
in place ofmouseover
. It's what's used internally when binding to.hover(fn,fn)
and is generally what you want.没必要搞得太复杂。
No need to make overcomplicated.
看来还是有 CSS 解决方案的。 Safari 等待第二次触摸的原因是您通常在 :hover 事件上分配的背景图像(或元素)。如果没有显示任何内容 - 您不会有任何问题。解决方案是使用辅助 CSS 文件(或 JS 方法中的样式)来定位 iOS 平台,该文件会覆盖 :hover 背景以继承,例如并隐藏要在鼠标悬停时显示的元素:
这是一个示例 CSS 和HTML - 鼠标悬停时带有星号标签的产品块:
HTML:
CSS:
解决方案(辅助 CSS):
Seems there is a CSS solution after all. The reason Safari waits for a second touch is because of the background image (or elements) you usually assign on the :hover event. If there is none to be shown - you won't have any problems. The solution is to target iOS platform with secondary CSS file (or style in case of a JS approach) which overrides :hover background to inherit for example and keep hidden the elements you were going to display on mouse over:
Here is an example CSS and HTML - a product block with a starred label on mouse over:
HTML:
CSS:
Solution (secondary CSS):
目前尚不完全清楚您的问题是什么,但如果您只想消除双击,同时保留鼠标悬停效果,我的建议是:
touchstart 上添加悬停效果
和mouseenter
。mouseleave
、touchmove
和click
上的悬停效果。背景
为了模拟鼠标,如果用户在触摸屏(如 iPad)上触摸并释放手指,浏览器(例如 Webkit mobile)会触发以下事件(来源:触摸和鼠标(位于 html5rocks.com):
touchstart
touchmove
touchend
mouseover
mouseenter
mouseover
、mouseenter
或mousemove
事件更改了页面内容,则以下事件是从未被解雇过。mousemove
mousedown
mouseup
click
似乎不可能简单地告诉网络浏览器跳过鼠标事件。
更糟糕的是,如果鼠标悬停事件更改了页面内容,则永远不会触发单击事件,如 Safari Web 内容指南 - 处理事件,特别是单指事件。 “内容更改”到底是什么,取决于浏览器和版本。我发现对于 iOS 7.0,背景颜色的更改不是(或不再?)内容更改。
解决方案解释
回顾一下:
touchstart
和mouseenter
上添加悬停效果。mouseleave
、touchmove
和click
上的悬停效果。请注意,
touchend
上没有任何操作!这显然适用于鼠标事件:
mouseenter
和mouseleave
(mouseover
和mouseout
的略微改进版本)被触发,并添加和删除悬停。如果用户实际上
单击
链接,悬停效果也会被删除。这可确保当用户按下 Web 浏览器中的后退按钮时将其删除。这也适用于触摸事件:在
touchstart
上添加悬停效果。它“不会”在touchend
上被删除。在mouseenter
上再次添加它,并且由于这不会导致任何内容更改(它已经添加),因此也会触发click
事件,并且无需跟踪链接即可让用户再次点击!浏览器在
touchstart
事件和click
之间的 300 毫秒延迟实际上得到了很好的利用,因为悬停效果将在这么短的时间内显示出来。如果用户决定取消点击,只需移动手指即可像平常一样操作。通常,这是一个问题,因为没有触发
mouseleave
事件,并且悬停效果保持不变。值得庆幸的是,通过删除touchmove
上的悬停效果可以轻松解决此问题。就是这样!
请注意,可以消除 300 毫秒的延迟,例如使用 FastClick 库,但这超出了这个问题的范围。
替代解决方案
我发现以下替代方案存在以下问题:
touchend
中模拟点击事件:即使用户只想滚动或缩放,而无意实际单击链接,这也会错误地跟随链接。touchend
中设置一个变量,该变量用作后续鼠标事件中的 if 条件,以防止该时间点的状态发生变化。该变量在单击事件中重置。如果您确实不想在触摸界面上出现悬停效果,那么这是一个不错的解决方案。不幸的是,如果由于其他原因触发了touchend
并且没有触发单击事件(例如用户滚动或缩放),并且随后尝试使用鼠标跟踪链接(即在同时具有鼠标和触摸界面的设备)。进一步阅读
mouseover
或mousemove
事件期间内容更改后不会触发任何其他事件。另请参阅 iPad/iPhone 双击问题 和 禁用移动浏览器上的悬停效果。
It is not entirely clear what your question is, but if you just want to eliminate the double click, while retaining the hover effect for the mouse, my advice is to:
touchstart
andmouseenter
.mouseleave
,touchmove
andclick
.Background
In order to simulate a mouse, browsers such as Webkit mobile fire the following events if a user touches and releases a finger on touch screen (like iPad) (source: Touch And Mouse on html5rocks.com):
touchstart
touchmove
touchend
mouseover
mouseenter
mouseover
,mouseenter
ormousemove
event changes the page content, the following events are never fired.mousemove
mousedown
mouseup
click
It does not seem possible to simply tell the webbrowser to skip the mouse events.
What's worse, if a mouseover event changes the page content, the click event is never fired, as explained on Safari Web Content Guide - Handling Events, in particular figure 6.4 in One-Finger Events. What exactly a "content change" is, will depend on browser and version. I've found that for iOS 7.0, a change in background color is not (or no longer?) a content change.
Solution Explained
To recap:
touchstart
andmouseenter
.mouseleave
,touchmove
andclick
.Note that there is no action on
touchend
!This clearly works for mouse events:
mouseenter
andmouseleave
(slightly improved versions ofmouseover
andmouseout
) are fired, and add and remove the hover.If the user actually
click
s a link, the hover effect is also removed. This ensure that it is removed if the user presses the back button in the web browser.This also works for touch events: on
touchstart
the hover effect is added. It is '''not''' removed ontouchend
. It is added again onmouseenter
, and since this causes no content changes (it was already added), theclick
event is also fired, and the link is followed without the need for the user to click again!The 300ms delay that a browser has between a
touchstart
event andclick
is actually put in good use because the hover effect will be shown during this short time.If the user decides to cancel the click, a move of the finger will do so just as normal. Normally, this is a problem since no
mouseleave
event is fired, and the hover effect remains in place. Thankfully, this can easily be fixed by removing the hover effect ontouchmove
.That's it!
Note that it is possible to remove the 300ms delay, for example using the FastClick library, but this is out of scope for this question.
Alternative Solutions
I've found the following problems with the following alternatives:
touchend
: This will incorrectly follow the link, even if the user only wanted to scroll or zoom, without the intention of actually clicking the link.touchend
that is used as a if-condition in subsequent mouse events to prevents state changes at that point in time. The variable is reset in the click event. This is a decent solution if you really don't want a hover effect on touch interfaces. Unfortunately, this does not work if atouchend
is fired for another reason and no click event is fired (e.g. the user scrolled or zoomed), and is subsequently trying to following the link with a mouse (i.e on a device with both mouse and touch interface).Further Reading
mouseover
ormousemove
event.See also iPad/iPhone double click problem and Disable hover effects on mobile browsers.
尚未对此进行全面测试,但由于 iOS 会触发触摸事件,因此假设您处于 jQuery 设置中,这可能会起作用。
这个想法是 Mobile WebKit 在点击结束时触发
touchend
事件,因此我们监听该事件,然后在触发touchend
事件后立即重定向浏览器一个链接。Haven't tested this fully but since iOS fires touch events, this could work, assuming you are in a jQuery setting.
The idea is that Mobile WebKit fires a
touchend
event at the end of a tap so we listen for that and then redirect the browser as soon as atouchend
event has been fired on a link.