javascript - 创建一个不消耗点击事件的悬停区域

标签 javascript html css svg mouseevent

我想在悬停在某个元素上时显示工具提示框,我只希望工具提示在用户鼠标离开由矢量图形定义的自定义悬停区域时消失。

除了悬停区域会阻止点击事件冒泡到工具提示外,这是可行的,如下所示。

$(document).ready(function() {
  $("#hover-me").mouseenter(function(e) {
    $("#mouse-box").css("visibility", "visible");
    $("#tooltip").show();
  });
  $("#mouse-box").mouseleave(function(e) {
    $("#mouse-box").css("visibility", "hidden");
    $("#tooltip").hide();
  });
});
#tooltip {
  position: absolute;
  top: 20px;
  display: none;
  height: 60px;
}

#hover-me {
  position: absolute;
  top: 40px;
  left: 140px;
}

svg {
  position: absolute;
  left: 0;
  width: 210px;
  height: 83px;
  opacity: 0.03;
  pointer-events: none;
}

svg clipPath rect:first-child {
  width: 90px;
  height: 100%;
  x: 0;
  y: 0;
}

svg clipPath rect:last-child {
  width: 130px;
  height: 35px;
  x: 90px;
  y: 25px;
}

#mouse-box {
  fill: red;
  visibility: hidden;
  width: 100%;
  height: 100%;
  pointer-events: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="tooltip">Can't click</button>
<div id="hover-me">Hover me</div>
<svg>
    <defs>
      <clipPath id="clip">
        <rect/><rect/>
      </clipPath>
    </defs>
    <rect id="mouse-box" clip-path="url(#clip)"/>
</svg>

最佳答案

您可能无法让所有鼠标事件正常运行,但至少对于 click(),您可以使用 hide-test-show-click 技巧。

$(document).ready(function() {
  $("#hover-me").mouseenter(function(e) {
    $("#mouse-box").css("visibility", "visible");
    $("#tooltip").show();
  });
  $("#mouse-box").mouseleave(function(e) {
    $("#mouse-box").css("visibility", "hidden");
    $("#tooltip").hide();
  });

  $("#mouse-box").click(function(e) {
    $("#mouse-box").hide();
    var elem = document.elementFromPoint(e.pageX, e.pageY);
    $("#mouse-box").show();
    $(elem).click();
  });
  
  $("#tooltip").click(function(e) {
    alert("Clicked!");
  });
});
#tooltip {
  display: none;
  height: 150px;
}

#hover-me {
  position: absolute;
  top: 65px;
  left: 140px;
}

svg {
  position: absolute;
  left: 0;
  width: 210px;
  height: 150px;
  opacity: .03;
  pointer-events: none;
}

svg clipPath rect:first-child {
  width: 70px;
  height: 670px;
  x: 0;
  y: 0;
}

svg clipPath rect:last-child {
  width: 150px;
  height: 35px;
  x: 70px;
  y: 50px;
}

#mouse-box {
  fill: red;
  visibility: hidden;
  width: 100%;
  height: 100%;
  pointer-events: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="tooltip">Tooltip</button>
<div id="hover-me">Hover me</div>
<svg>
    <defs>
      <clipPath id="clip">
        <rect/><rect/>
      </clipPath>
    </defs>
    <rect id="mouse-box" clip-path="url(#clip)"/>
</svg>

关于javascript - 创建一个不消耗点击事件的悬停区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43037525/

相关文章:

css - 在div中从底部到顶部堆积图像

javascript - 更改 :hover to click/tap function on mobile/touch devices not working

javascript - jQuery - 选择一个仅包含超过一定数量子级的容器

Javascript 以奇数间隔循环

javascript - 单击表 'checking' 复选框中的行?

html - 如何在使用 Bootstrap 4 加载页面后禁用加载器/微调器?

css - 浏览器正在下载页面上已加载的背景图片?

javascript - 将 nl 字符串转换为向量或一些等价的数字

javascript - 在 JSfiddle 中添加图片

html - 当我单击另一个单选按钮时,如何清除字段中所有键入的字符?我的几乎所有字段在另一个页面上都有相同的 ngModel