jquery - 悬停事件,但不在触摸/移动设备上

标签 jquery hover jquery-hover

我正在尝试创建一个显示图像的悬停事件。在我的台式机/笔记本电脑上它可以正确触发。然而,在移动设备(触摸)上,图像仅在单击模式链接时出现,然后它是粘性的(它保留在屏幕上)。

在移动设备或触摸屏上,我不希望触发悬停事件。

这就是我正在处理的内容。

Jsfiddle

HTML

<a href="#test" id="test-parent">Hover to show image</a>

<img class="preview" id="test-child" src="https://mayvers.com.au/wp-content/uploads/2017/09/test-image.jpg" />

<script>
  $("#test-parent").hover(function() {
    $("#test-child").fadeToggle();
  });
</script>

CSS

.preview {
  display: none;
  position: absolute;
  bottom: 0;
  right: 0;
  z-index: 1;
  height: 50%;
}

最佳答案

您可以检查是否是触摸事件并相应地触发悬停效果。

<script>
  $("#test-parent").hover(function() {
   if(!isTouchEvent()){
    $("#test-child").fadeToggle();
   }
  });

  function isTouchEvent() {
    return 'ontouchstart' in document.documentElement
           || navigator.maxTouchPoints > 0
           || navigator.msMaxTouchPoints > 0;
  }

</script>

<强> JSFiddle Demo

关于jquery - 悬停事件,但不在触摸/移动设备上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59033059/

相关文章:

javascript - 在两个元素之间保持悬停状态处于事件状态

jquery - 我可以在jquery悬停时更改图像吗?

javascript - 更新嵌套复选框的 CSS onDomReady

html - 如何为图像添加混合模式?

css - 悬停时更改 tr 的背景颜色

jquery - .slideToggle 悬停在 div jquery 上

javascript - jquery 选择 this.parent 之外的新元素

javascript - JQueryUI 可排序,无占位符位移

php - 通过 jQuery 检查属性 href

jquery - 在悬停时更改 jquery img src 使图像向下移动