gpt4 book ai didi

javascript - JS - 滚动悬停相关的 div

转载 作者:行者123 更新时间:2023-11-30 11:49:28 25 4
gpt4 key购买 nike

我有几个 div,它允许您将鼠标悬停在 #HoverMe 上,以便查看 #hidden div 中的内容(悬停时隐藏)。如果列表有点长,它是可滚动的。但是,当我悬停在 #HoverMe div 时,我无法滚动它。如果我想滚动 #Hidden div,那么我必须移动,这会导致它再次消失(很明显)。

我希望能够将鼠标悬停在 #HoverMe 上,并有大约 5 秒的时间移动到 #hidden。如果您在消失前将鼠标悬停在 #hidden 上,它将停止隐藏计时器并能够滚动和检查内容。

可能更糟糕的替代解决方案:

当鼠标位于 #HoverMe 时滚动 #Hiddendiv?

它的样子:

                      ------------     --------- _
| #HoverMe | | #hidden |S|
------------ | --------|R|
| car.name|O|
|---------|L|
| car.name|L|
---------|B|
| car.name|A|
|---------|R|
| car.name| |
---------|_|

代码:

<div>
<div id="HoverMe" style="width: 100px; background: green">
Car
</div>
<div id="hidden" style="overflow:auto; height:100px; position: absolute; background-color: red; display: none">

@foreach (var car in Model.Car) { @* Where the #hidden list get its content *@
<div>@car.Name</div>
}

</div>
</div>


<script>
var hoverEle = document.getElementById("HoverMe");
var popupEle = document.getElementById("hidden");

hoverEle.addEventListener('mouseover', function () {
var hoverRect = hoverEle.getBoundingClientRect(); // get the position of the hover element
popupEle.style.left = (hoverRect.right + 16) + "px";
popupEle.style.top = hoverRect.top + "px";
popupEle.style.display = "block";

}, false);

hoverEle.addEventListener('mouseout', function () {
popupEle.style.display = "none";
}, false);
</script>

最佳答案

(顺便说一句,这个问题被标记为 jQuery 但您实际上只使用普通 JS!没问题,当然,只是让您知道!)

您肯定走在正确的轨道上。我认为您可以通过设置超时并清除它来处理它。

<div>
<div id="HoverMe" style="width: 100px; background: green">
Car
</div>
<div id="hidden" style="overflow:auto; height:100px; position: absolute; background-color: red; display: none">

@foreach (var car in Model.Car) { @* Where the #hidden list get its content *@
<div>@car.Name</div>
}

</div>
</div>


<script>
var hoverEle = document.getElementById("HoverMe");
var popupEle = document.getElementById("hidden");
var timeoutId;

function showPopup() {
var hoverRect = hoverEle.getBoundingClientRect(); // get the position of the hover element
popupEle.style.left = (hoverRect.right + 16) + "px";
popupEle.style.top = hoverRect.top + "px";
popupEle.style.display = "block";
}

function hidePopup() {
popupEle.style.display = "none";
}

hoverEle.addEventListener('mouseover', function () {
showPopup();
}, false);

hoverEle.addEventListener('mouseout', function () {
timeoutId = window.setTimeout(function () {
hidePopup();
}, 5000);
}, false);

popupEle.addEventListener('mouseover', function () {
if (timeoutId) {
window.clearTimeout(timeoutId);
}
}, false);

popEle.addEventListener('mouseout', function () {
hidePopup();
}, false);

</script>

关于javascript - JS - 滚动悬停相关的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39939485/

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