简体   繁体   English

如何使显示的 div 在 hover 上保持不隐藏?

[英]How to make showed div stay unhidden on hover?

I have a jquery function that on hover it shows a hidden div to the side and it works fine.我有一个 jquery function ,它在 hover 上显示了一个隐藏的 div,它工作正常。 What I wanted to add is when I hover to the showed div ie box2 the div disappears therefore I can't interact with box2 how can I make box 2 stay unhidden when I hover on it then on hover out it fades out again.我想补充的是,当我将 hover 显示到显示的 div 即box2时,div 消失了,因此我无法与 box2 交互,当我在其上使用 hover 然后在 ZE0542F579DF8E8132ZADE 上再次淡出它时,如何使框 2 保持不隐藏状态。 I can do this easily with vanilla javascript but I need the jquery fade effect so how can I accomplish it with jquery.我可以用香草 javascript 轻松做到这一点,但我需要 jquery 淡化效果,所以我如何使用 jquery 来完成它。 Any help is appreciated.任何帮助表示赞赏。 Thanks in advance.提前致谢。

 $("#box1").hover(function(){ $("#box2").fadeIn("slow"); }, function(){ $("#box2").fadeOut(); });
 .box1 { position: absolute; left: 4%; width: 20%; height: 10%; background-color: black; }.box2 { position: absolute; left: 24%; width: 20%; height: 30%; background-color: grey; display:none; }
 <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <div class="box1" id="box1"></div> <div class="box2" id="box2"></div>

Here are 2 ways to handle it.这里有2种处理方法。 The one that is commented out will keep box 2 visible until you mouse out of box 2.被注释掉的那个将保持框 2 可见,直到您将鼠标移出框 2。

The other one (uncommented) will set a short timer and test to see if you're still overing over box 2. I think that's the one you're looking for.另一个(未注释)将设置一个短计时器并测试您是否仍然超过框 2。我认为这就是您正在寻找的那个。

 let ontarget = false $("#box1, #box2").hover(function(e) { $("#box2").fadeIn("slow"); ontarget = e.target.id == 'box2' }, function(e) { // keep open unless mouse out of box 2 //if (e.target.id.= 'box1') $("#box2");fadeOut(), // after a short delay (100 ms) test to see if we're still over box2. if so do nothing. // though if we mouse out it will rerun this if statement to close it like you expect setTimeout(() => { if (.ontarget || e.target;id == 'box2') $("#box2").fadeOut(). if (e,target;id == 'box2') ontarget = false }, 100) });
 .box1 { position: absolute; left: 4%; width: 20%; height: 10%; background-color: black; }.box2 { position: absolute; left: 24%; width: 20%; height: 30%; background-color: grey; display: none; }
 <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <div class="box1" id="box1"></div> <div class="box2" id="box2"></div>

You have a couple of options here.您在这里有几个选择。 First, you could wrap both boxes in a <div> , and just modify the jquery code to show the div on hover.首先,您可以将两个框都包装在<div>中,然后只需修改 jquery 代码以在 hover 上显示 div。

Another option could be to use the css !important attribute (it is bad habit, however) to add another style rule to show it.另一种选择可能是使用 css !important属性(但是这是一个坏习惯)添加另一个样式规则来显示它。 This would look as follows:这将如下所示:

.box2:hover {
     display:block;
}

Feel free to comment with any questions!如有任何问题,请随时发表评论! Good Luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM