gpt4 book ai didi

javascript - 如何以编程方式在元素上触发 "hoverintent"

转载 作者:行者123 更新时间:2023-11-29 21:47:25 24 4
gpt4 key购买 nike

我正在使用 Tristen Brown 的 hoverintent library ,它是 jQuery 的纯 Javascript 版本 hoverIntent图书馆。

我有两个元素 firstsecond

second 元素有 hoverintent 处理程序,它会导致工具提示出现在该元素上方。当我手动将鼠标悬停在 second 上时,工具提示会按预期显示。

我想以编程方式触发second 的工具提示。例如,要与 first 元素进行交互,会导致显示 second 元素的工具提示。我尝试使用 jQuery trigger 来做到这一点。我能够触发鼠标悬停处理程序,但不能触发任何 hoverIntent。

有什么建议吗?

这是我的 Javascript:

$( document ).ready(function() {
var first = document.getElementById('first');
var second = document.getElementById('second');

$(first).mouseover(function(){
$(second).trigger({ type:"mouseover", clientX:"350", clientY:"105" });
$(second).trigger({ type:"mousemove", clientX:"350", clientY:"105" });
});

hoverintent(second, function(e) {
this.className = 'on';
}, function(e) {
this.className = 'off';
});

$(second).mouseover(function(){
console.log("mouseover");
});
});

这是我的 HTML:

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src='http://tristen.ca/hoverintent/dist/hoverintent.min.js'></script>

<meta charset="utf-8">
<title>JS Bin</title>
</head>

<body>
<div style="padding:100px;">
<ul class='examples'>
<li id='first'>
Trigger
</li>
<li id='second'>
hoverintent
<span class='popup'>Hi there</span>
</li>
</ul>
</div>
</body>

</html>

完整的 JS bin 在这里:

http://jsbin.com/kumeva/4/edit?js,output

最佳答案

I would like to trigger the tooltip of second by mousing over the first element.

您可以将一系列鼠标事件分派(dispatch)到#second 并保持hoverintent 代码和分派(dispatch)代码完全分开,如下所示:

// Hoverintent code
$(document).ready(function() {
var second = document.getElementById('second');
hoverintent(second, function(e) {
this.className = 'on';
}, function(e) {
this.className = 'off';
});
});

///////////////////////////////////

// Dispatch code
$(document).ready(function() {
var first = document.getElementById('first');
var second = document.getElementById('second');
$(first).on("mouseover", function(){
// Send a mouseover to wake hoverintent
var event = new MouseEvent("mouseover");
second.dispatchEvent(event);

// Send a mousemove trigger the internal hover code
event = new MouseEvent("mousemove");
second.dispatchEvent(event);
});

$(first).on("mouseout", function(){
// Cancel the hover code
var event = new MouseEvent("mouseout");
second.dispatchEvent(event);
});
});

Demo

关于javascript - 如何以编程方式在元素上触发 "hoverintent",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30589309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com