简体   繁体   English

带有延迟的JavaScript / jQuery鼠标悬停事件

[英]JavaScript / jQuery mouseover event with delay

I have a code that allows me to show hidden divs after a little delay on mouseover, my problem now is that im not very good with CS, i have elements in that div with that code: 我有一个代码,可以让我在鼠标悬停了一段时间后显示隐藏的div,我现在的问题是CS效果不是很好,我在那个div中有该代码的元素:

 $(document).ready(function() { var timer; var delay = 250; $("#content1").mouseover(function() { timer = setTimeout(function() { $("#content.left1").css("display", "block"); }, delay); }); $("#content1").mouseout(function() { $("#content.left1").css("display", "none"); clearTimeout(timer); }); }); 
 .txtmiddle { border: 1px solid rgba(215, 215, 215, 0.1); background-color: rgba(245, 245, 245, 0.7); margin-top: 5px; opacity: 0.6; filter: alpha(opacity=60); padding: 2%; border-radius: 15px; position: relative; overflow: auto; -webkit-animation: fadeIn 0.1s; animation: fadeIn 0.1s; } .txtmiddle:hover { border: 1px solid rgba(55, 55, 55, 0.2); background-color: rgba(255, 255, 255, 0.9); opacity: 1; filter: alpha(opacity=100); } #content { display: none; background: rgba(255, 255, 255, 0.9); padding: 15px; border-radius: 15px; overflow: hidden; position: relative; } #txtleft { width: 30%; float: left; margin-left: 4%; border-top: 1px solid rgba(0, 0, 0, 0.0); } 
  <div id="txtleft"><div id="content" class="left1">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit a</div> </div> <div id="middlewrapper"><div class="txtmiddle" id="content1"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Nuvola_mimetypes_info.png/100px-Nuvola_mimetypes_info.png" id="midcontentleft"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Nuvola_mimetypes_info.png/100px-Nuvola_mimetypes_info.png" id="midcontentimgright" class="1"> </div> </div> 

cant get it to run here.... but its working fine my only problem is now that everytime i hover over elements (those images) in the div the hidden content is (re-)shown again (with that delay) (before i didnt had the delay so the hidden element wouldnt disapear and apear again and one couldnt notice the change... 不能让它在这里运行....但是工作正常,我唯一的问题是现在每次我将鼠标悬停在div中的元素(这些图像)上时,都会(重新)显示隐藏的内容(有延迟)(在没有延迟,所以隐藏的元素不会消失并再次出现,并且一个人无法注意到变化...

为什么不简单地使用fadeIn('slow')fadeOut('slow')代替

I would usually use jQuery hover() to achieve what you are looking for: 我通常会使用jQuery hover()来实现您想要的功能:

 $(document).ready(function () { var timeout; $("#content1").hover(function () { timeout = setTimeout(function () { $("#content.left1").css("display", "block"); }, 250); }, function () { clearTimeout(timeout); $("#content.left1").css("display", "none"); }); }); 

Demo here . 演示在这里

As correctly said by atinder fadeIn and fadeOut functions of jQuery will serve your need: 就像Atinder所说的那样,jQuery的fadeIn和fadeOut函数将满足您的需求:

Try the below code: 试试下面的代码:

jQuery(document).ready(function() {
var delay = 1000;//the delay interval
jQuery("#content1").mouseover(function() {     
  jQuery( "#content.left1" ).fadeIn(delay);
});
jQuery("#content1").mouseout(function() {    
   jQuery( "#content.left1" ).fadeOut(delay);
   });
});

Hope it helps.. 希望能帮助到你..

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

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