javascript - Cancel setTimeout if mouse is over div -
i have 2 div classes, , b. when mouse on div a, div b should appear, if mouse on or b, div b should stay opened. if mouse out of both, , b divs, b should disappear. (as guess simple tooltip script)
this jquery code wrote:
$(document).ready(function() { function show() { $("bbb").css({'display':'block'}); } $("aaa").each(function() { $(this).mouseover(function() { show(); }); $(this).mouseleave(function() { time = settimeout("hide()", 200); }); $("bbb").mouseleave(function() { settimeout("hide()", 200); }); $("bbb").mouseenter(function() { cleartimeout(time); }); }); }); function hide() { $("bbb").css({'display':'none'}); }
the problem when move b a, b disappears! want disappear if mouse neither on a, nor b. how can fix problem?
there few small problems code. 1 biting right aren't clearing bbb's timeout when enter aaa. can fix adding cleartimeout
aaa's mouseover
handler.
secondly, it's safest clear kind of timeout before set each time, don't have timeout tracking overwritten if unexpected happens. (it's safe clear timeout, if it's invalid or has occurred.)
lastly, though problem in example code, you're leaking time
global object. ;-)
try instead:
$(document).ready(function() { var time; function show() { $("bbb").css({'display':'block'}); } $("aaa").each(function() { $(this).mouseover(function() { cleartimeout(time); show(); }); $(this).mouseleave(function() { cleartimeout(time); time = settimeout("hide()", 200); }); $("bbb").mouseleave(function() { cleartimeout(time); time = settimeout("hide()", 200); }); $("bbb").mouseenter(function() { cleartimeout(time); }); }); }); function hide() { $("bbb").css({'display':'none'}); }
Comments
Post a Comment