How do I simulate a hover with a touch in touch enabled browsers?(如何在启用触摸的浏览器中模拟悬停?)
问题描述
使用一些像这样的 HTML:
With some HTML like this:
<p>Some Text</p>
然后是这样的一些 CSS:
Then some CSS like this:
p {
color:black;
}
p:hover {
color:red;
}
如何允许在支持触控的设备上长按来复制悬停?我可以更改标记/使用 JS 等,但想不出一个简单的方法来做到这一点.
How can I allow a long touch on a touch enabled device to replicate hover? I can change markup/use JS etc, but can't think of an easy way to do this.
推荐答案
好的,我已经解决了!它涉及稍微更改 CSS 并添加一些 JS.
OK, I've worked it out! It involves changing the CSS slightly and adding some JS.
使用jQuery让它变得简单:
Using jQuery to make it easy:
$(document).ready(function() {
$('.hover').on('touchstart touchend', function(e) {
e.preventDefault();
$(this).toggleClass('hover_effect');
});
});
英文:开始或结束触摸时,打开或关闭类hover_effect
.
In english: when you start or end a touch, turn the class hover_effect
on or off.
然后,在您的 HTML 中,将类悬停添加到您希望它使用的任何内容.在您的 CSS 中,替换以下任何实例:
Then, in your HTML, add a class hover to anything you want this to work with. In your CSS, replace any instance of:
element:hover {
rule:properties;
}
与
element:hover, element.hover_effect {
rule:properties;
}
为了增加实用性,请将其添加到您的 CSS 中:
And just for added usefulness, add this to your CSS as well:
.hover {
-webkit-user-select: none;
-webkit-touch-callout: none;
}
停止浏览器要求您复制/保存/选择图像或其他任何内容.
To stop the browser asking you to copy/save/select the image or whatever.
简单!
这篇关于如何在启用触摸的浏览器中模拟悬停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在启用触摸的浏览器中模拟悬停?


- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01