Reputation: 7553
I'm using HoverIndent jQuery plugin for my dropdown menu, basically I hover on #link and it shows #submenu.
Unfortunately it doesn't work as expected. Everything looks fine at first but there's this case where I leave my cursor on #link, hit F5 (page refresh) and after refresh the HoverIndent isn't triggered, I have to move my cursor out of the #link, then move it back over to make HoverIndent working. What's even worse it's usually not working straight away (I have to wait a while for page to load). I've been trying everything including disabling all other JS, but without luck.
I've been investigating this for a while and looks like this code (basic jQuery no HoverIndent here):
$("#link").on("mouseover", function() {
console.log("hover");
})
Or this:
$("#link").hover(function(){
console.log("hover");
})
Works the same, so when I leave cursor or #link and hit F5 it doesn't log "hover" to console after refresh.
Be aware it works on jsfiddle etc. because there are pages loading in iframes, but it doesn't work locally.
Also it seems to be working on HoverIdent's page, I have absolutely no idea why. Am I right hover/mouseover shouldn't fire right after page load? If yes how to force them to and what's wrong with my HoverIndent?
Big update
It gets really weird. I've downloaded HTML5 Boilerplate and copied the whole HoverIndent demo page. The same hoverIntent.js file served locally doesn't seem to work while works from vendors server:
<!-- uncomment this line and everything works? the hell? :D -->
<!-- <script type="text/javascript" src="http://cherne.net/brian/resources/jquery.hoverIntent.js"></script> -->
<!-- same code but from localhost and doesn't work -->
<script type="text/javascript" src="js/jquery.hoverIntent.js"></script>
Here's the whole code (note you need to save http://cherne.net/brian/resources/jquery.hoverIntent.js in js directory next to index.html:
<!DOCTYPE html>
<html class="no-js">
<head>
<script src="js/vendor/jquery-1.10.2.min.js"></script>
<!-- uncomment this line and everything works? the hell? :D -->
<!-- <script type="text/javascript" src="http://cherne.net/brian/resources/jquery.hoverIntent.js"></script> -->
<!-- same code but from localhost and doesn't work -->
<script type="text/javascript" src="js/jquery.hoverIntent.js"></script>
<style type="text/css" media="screen">
body{background-color:#fff;font-family:Verdana,Arial,sans-serif;font-size:1em;color:#000;margin:0;padding:0}a{outline:0}h1{margin:0;padding:.4em 0 .45em .6em;font-size:.9em;background-color:#996;color:#cc9;white-space:nowrap}h1 a{color:#cc9;text-decoration:none}h1 a:hover{color:#fff}h2{width:100%;border-bottom:#cc9 1px solid;margin:0 0 .5em;padding:1em 0 0;font-size:1.2em}h3{display:block;margin:0;padding:0 0 1px;font-size:1em;font-weight:700}h4{display:block;margin:0;padding:0 0 1px;text-transform:uppercase;font-size:.85em;font-weight:700;color:#996}img{border:none}p{margin:0;padding:0 0 1em;clear:both}ul{margin:0;padding:0 0 0 1em;clear:both;list-style-type:none}ul li{margin:0;padding:0 0 0 1em;background-image:url(images/bullet.gif);background-repeat:no-repeat;background-position:0 .15em}dl{margin:0;padding:0 0 2em;clear:both}dt{margin:0;padding:1em 0 0 1em;font-weight:700}dd{margin:0;padding:0 0 0 1em}#nav1{display:block;list-style-type:none;margin:0;padding:0 0 .25em;background-color:#cc9;white-space:nowrap}#nav1 li{display:inline;margin:0;padding:0 .75em 0 .25em;background-image:none}#nav1 li a{font-size:.9em;font-weight:700;padding:0 .3em;text-decoration:none;color:#787850}#nav1 li a:hover{background-color:#996;color:#000}#nav1 li a.current{color:#fff}#pageContent{font-size:.9em;line-height:1.3em;max-width:55em}#main{padding:1em 1em 3em;color:#000}#main a{color:#787850;text-decoration:underline}#main a:hover{text-decoration:none}#main h2.section{border-top:#cc9 1px solid;padding-top:.5em}#main p.section{border-top:#cc9 1px solid;margin-top:3em}#PICTURES #main a{float:left;display:block;width:33%;text-decoration:none;text-align:center;padding:.5em 0;margin:0 0 1em}#PICTURES #main a:hover{background:#e6e6cc}#PICTURES #main a img{display:block;width:93%;margin:0 auto;padding:0}#PICTURES #main span{display:block;width:92% margin:0 auto}div.job{clear:both;margin:1em 0 0;padding:0 0 1em}div.job h3{float:left;width:60%}div.job .what,div.job .when,div.job .where{float:left;display:block}div.job .what{padding:0 0 1px;width:40%;text-align:right;color:#996}div.job .when,div.job .where{position:relative;width:60%;text-transform:uppercase;font-size:.85em;line-height:1.5em;height:1.6em;background-color:#e6e6cc}div.job .when{width:40%;text-align:right}div.job p{padding:.5em 0}#RESUME h4{clear:both;padding:.5em 0 0 2.3em;font-weight:400;font-size:.85em}#RESUME #main ul{padding-bottom:.5em}#RESUME #main div.job ul{padding-bottom:.2em}#RESUME div.job ul li{background-image:none;padding-top:.3em;padding-bottom:.3em}#RESUME div.job p{clear:both;padding:.2em 0 .5em}#RESUME div.modified{clear:both;margin:0;padding:1em 0 0;color:#999;text-align:center}#PORTFOLIO h2{padding-top:.2em;font-size:1.2em;border-bottom:none}div.job p img{clear:left;float:left;width:100px;margin:0 .5em 0 0}#PORTFOLIO ul.screenshots{margin:0;padding:0;display:block;clear:both;list-style-type:none}#PORTFOLIO ul.screenshots li{display:block;margin:.5em 0;padding:1em 0;width:44em;float:left;background:#e6e6cc}#PORTFOLIO ul.screenshots li img{float:left;margin:0 1em}#PORTFOLIO ul.screenshots li span{display:block;padding-right:.5em}#PORTFOLIO dl{display:block;width:21em;float:left;clear:none}#PORTFOLIO dt a{font-weight:700}#RESOURCES #main li{padding-bottom:1em}#RESOURCES .download{text-align:center}#RESOURCES pre{background:#eee;color:#666;padding:1.5em;margin:0;font-size:1em;font-family:verdana;overflow:auto}#RESOURCES pre span{color:#699;font-family:arial}#RESOURCES #main .demo li{padding-bottom:0}ul.demo{display:block;width:100%;height:75px;padding:0;margin:0;background:#9cc;list-style-type:none}ul.demo li{background:#fcc;display:block;width:25%;height:50px;padding:0;margin:0;float:left;position:relative;overflow:hidden;cursor:default;font-size:.9em;line-height:1.1em}ul.demo li.p2{background:#ffc}ul.demo li.p3{background:#cfc}ul.demo li.p4{background:#ccf}ul.demo li span{display:block;margin:4px;background:#eef;cursor:default}
</style>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$("#demo1 li").hover(makeTall,makeShort);
$("#demo2 li").hoverIntent(makeTall,makeShort);
$("#demo3 li").hoverIntent(toggleHeight);
$("#demo4").hoverIntent(makeTall,makeShort,'li');
$("#demo5").hoverIntent(toggleHeight,'li');
$("#demo6").hoverIntent({
over: makeTall,
out: makeShort,
selector: 'li'
});
});
function makeTall(){$(this).animate({"height":75},200);}
function makeShort(){$(this).animate({"height":50},200);}
function toggleHeight(){var h=(parseInt($(this).css('height'),10) > 50) ? 50 : 75; $(this).animate({"height":h},200);}
</script>
</head>
<body>
<div id="pageContent">
<div id="main">
<h2>What is hoverIntent?</h2>
<p>hoverIntent is a plug-in that attempts to determine the user's intent... like a crystal ball, only with mouse movement! It is similar to <a href="http://api.jquery.com/hover/">jQuery's hover method</a>. However, instead of calling the handlerIn function immediately, hoverIntent waits until the user's mouse slows down enough before making the call.</p>
<p>Why? To delay or prevent the accidental firing of animations or ajax calls. Simple timeouts work for small areas, but if your target area is large it may execute regardless of intent. That's where hoverIntent comes in...</p>
<p class="download"><a href="jquery.hoverIntent.js">Download hoverIntent v1.8.0 (fully-commented, uncompressed)</a></p>
<p class="download"><a href="jquery.hoverIntent.minified.js">Download hoverIntent v.1.8.0 (minified)</a></p>
<p class="download"><a href="https://github.com/briancherne/jquery-hoverIntent">briancherne/jquery-hoverIntent (GitHub)</a></p>
<h2>Examples</h2>
<noscript><p><em>If you can see this message <strong>JavaScript is disabled</strong>. This plug-in requires JavaScript to be enabled in order for the examples to work. (This is really a note to myself so the next time I look at my web site with JavaScript accidentally turned off I don't freak out and wonder why it's not working)</em></p></noscript>
<h3>jQuery's hover (for reference)</h3>
<pre>$("#demo1 li").hover( makeTall, makeShort );</pre>
<ul class="demo" id="demo1">
<li class="p1"> </li>
<li class="p2"> </li>
<li class="p3"> </li>
<li class="p4"><span>hover ignores over/out events from children</span></li>
</ul>
<p>jQuery's built-in hover calls handlerIn and handlerOut functions immediately. If you move your cursor back and forth quickly across the tiles you'll see how the immediate execution can lead to problems.</p>
<h3>.hoverIntent( handlerIn, handlerOut )</h3>
<pre>$("#demo2 li").hoverIntent( makeTall, makeShort );</pre>
<ul class="demo" id="demo2">
<li class="p1"> </li>
<li class="p2"> </li>
<li class="p3"> </li>
<li class="p4"><span>hoverIntent also ignores over/out events from children</span></li>
</ul>
<p>hoverIntent is interchangeable with jQuery's hover. It can use the same exact handlerIn and handlerOut functions. It passes the same <strong>this</strong> and <strong>event</strong> objects to those functions.</p>
<h3>.hoverIntent( handlerInOut )</h3>
<pre>$("#demo3 li").hoverIntent( toggleHeight );</pre>
<ul class="demo" id="demo3">
<li class="p1"> </li>
<li class="p2"> </li>
<li class="p3"> </li>
<li class="p4"> </li>
</ul>
<p>hoverIntent can also take a single handlerInOut, just like jQuery's hover.</p>
<h3>.hoverIntent( handlerIn, handlerOut, selector )</h3>
<pre>$("#demo4").hoverIntent( makeTall, makeShort, 'li' );</pre>
<ul class="demo" id="demo4">
<li class="p1"> </li>
<li class="p2"> </li>
<li class="p3"> </li>
<li class="p4"> </li>
</ul>
<p>Unlike jQuery's hover, hoverIntent supports event delegation! Just pass in a selector of a descendant element.</p>
<h3>.hoverIntent( handlerInOut, selector )</h3>
<pre>$("#demo5").hoverIntent( toggleHeight, 'li' );</pre>
<ul class="demo" id="demo5">
<li class="p1"> </li>
<li class="p2"> </li>
<li class="p3"> </li>
<li class="p4"> </li>
</ul>
<p>Unlike jQuery's hover, hoverIntent supports event delegation with handlerInOut.</p>
<h3>.hoverIntent( object )</h3>
<pre>
$("#demo6").hoverIntent({
over: makeTall,
out: makeShort,
selector: 'li'
});
</pre>
<ul class="demo" id="demo6">
<li class="p1"> </li>
<li class="p2"> </li>
<li class="p3"> </li>
<li class="p4"> </li>
</ul>
<p>To control hoverIntent more precisely and override the default configuration options, pass it an object as the first parameter. The object must at least contain an "over" function. If the "over" function is sent alone, it will act just like handlerInOut.</p>
<h2>Common Configuration Options</h2>
<p>These are the common options you'll want to use. Note, nothing prevents you from sending <a href="http://api.jquery.com/jQuery.noop/">an empty function</a> as the handlerIn or handlerOut functions.</p>
<h3>over:</h3>
<p>Required. The handlerIn function you'd like to call on "mouseenter with intent". Your function receives the same "this" and "event" objects as it would from jQuery's hover method. If the "over" function is sent alone (without "out") then it will be used in both cases like the handlerInOut param.</p>
<h3>out:</h3>
<p>The handlerOut function you'd like to call on "mouseleave after timeout". Your function receives the same "this" and "event" objects as it would from jQuery's hover method. Note, hoverIntent will only call the "out" function if the "over" function has been called.</p>
<h3>timeout:</h3>
<p>A simple delay, in milliseconds, before the "out" function is called. If the user mouses back over the element before the timeout has expired the "out" function will not be called (nor will the "over" function be called). This is primarily to protect against sloppy/human mousing trajectories that temporarily (and unintentionally) take the user off of the target element... giving them time to return. <em>Default timeout: 0</em></p>
<h3>selector:</h3>
<p>A selector string for event delegation. Used to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. Read <a href="http://api.jquery.com/on/#direct-and-delegated-events">jQuery's API Documentation for the .on() method</a> for more information.</p>
<h2>Advanced Configuration Options</h2>
<p>Modify these if you are brave, test tirelessly, and completely understand what you are doing. When choosing the default settings for hoverIntent I tried to find the best possible balance between responsiveness and frequency of false positives.</p>
<h3>sensitivity:</h3>
<p>If the mouse travels fewer than this number of pixels between polling intervals, then the "over" function will be called. With the minimum sensitivity threshold of 1, the mouse must not move between polling intervals. With higher sensitivity thresholds you are more likely to receive a false positive. Note that hoverIntent r7 and earlier perform this comparison using rectilinear distance, whereas more recent versions compare against euclidean (straight-line) distance for better accuracy and intuition. If you are upgrading from an older version, you may want to verify that the desired behavior is preserved. <em>Default sensitivity: 6</em></p>
<h3>interval:</h3>
<p>The number of milliseconds hoverIntent waits between reading/comparing mouse coordinates. When the user's mouse first enters the element its coordinates are recorded. The soonest the "over" function can be called is after a single polling interval. Setting the polling interval higher will increase the delay before the first possible "over" call, but also increases the time to the next point of comparison. <em>Default interval: 100</em></p>
<h2 id="defects">Known Defects</h2>
<p>hoverIntent r5 suffers from <a href="http://code.google.com/p/chromium/issues/detail?id=68629">a defect in Google Chrome that improperly triggers mouseout when entering a child input[type="text"] element</a>. hoverIntent r6 uses the same mouseenter/mouseleave special events as jQuery's built-in hover, and jQuery 1.5.1 patched this issue. Thanks to Colin Stuart for tipping me off about this and for providing isolated code to demonstrate/test.</p>
<p id="chrome9defect" style="background:#eee;margin-bottom:1em;">This page uses jQuery 1.9.1 and hoverIntent r7, so when your cursor goes over the text input nothing should change (it should continue to read "enter parent" because you are still over this paragraph). <br/><input type="text" value=""/><br/> However, if you were using Google Chrome and if this page were using an older version of jQuery or hoverIntent, moving the cursor over the text input would improperly trigger the mouseout event, and the value would change to "leave parent".</p>
<script type="text/javascript">
function enter(e){$("input", e.target).val("enter parent");}
function leave(e){$("input", e.target).val("leave parent");}
$("#chrome9defect").hoverIntent(enter,leave);
</script>
<p>If you place an element flush against the edge of the browser chrome, sometimes Internet Explorer does not trigger a "mouseleave" event if your cursor leaves the element/browser in that direction. hoverIntent cannot correct for this.</p>
<p>Please email me <strong>brian(at)cherne(dot)net</strong> if you have questions or would like to notify me of any defects.</p>
<h2>Release History</h2>
<ul>
<li><a href="jquery.hoverIntent-1.8.0.js" title="jQuery hoverIntent r8 (v1.8.0)">v1.8.0</a> = (2014) Changed to <a href="http://semver.org">Semantic Versioning</a> (from r8 to v1.8.0). Removed <a href="https://en.wikipedia.org/wiki/Zero-width_no-break_space">U+FEFF character</a> from beginning of JS file. Removed stray "jQuery" in favor of "$" for noConflict situations. Changed measurements to use euclidean (instead of rectilinear) distance. Thanks to github community for patches, suggestions, and fixes!</li>
<li><a href="jquery.hoverIntent-1.7.0.js" title="jQuery hoverIntent r7 (v1.7.0)">r7</a> = (2013) Added event delegation via "selector" param/property. Added namespaced events for better isolation. Added handlerInOut support.</li>
<li><a href="jquery.hoverIntent-1.6.0.js" title="jQuery hoverIntent r6 (v1.6.0)">r6</a> = (2011) Identical to r5 except that the Google Chrome defect is fixed once you upgrade to jQuery 1.5.1 (or later).</li>
<li><a href="jquery.hoverIntent-1.5.0.js" title="jQuery hoverIntent r5 (v1.5.0)">r5</a> = (2007) Added state to prevent unmatched function calls. This and previous releases suffer from <a href="http://code.google.com/p/chromium/issues/detail?id=68861">a defect in Google Chrome that improperly triggers mouseout when entering a child input[type=text] element</a>.</li>
<li>r4 = Fixed polling interval timing issue (now uses a self-calling timeout to avoid interval irregularities).</li>
<li>r3 = Developer-only release for debugging.</li>
<li>r2 = Added timeout and interval references to DOM object -- keeps timers separate from each other. Added configurable options. Added timeout option to delay onMouseLeave function call. Fixed two-interval mouseOver bug (now setting pX and pY onMouseEnter instead of hardcoded value).</li>
<li>r1 = Initial release to jQuery discussion forum for feedback.</li>
</ul>
</div><!-- close #main -->
</div><!-- close #pageContent -->
</body>
Can someone explain this? Document ready is "too fast" while serving local files or what? How to fix that, at least locally? :> Thank you!
Upvotes: 0
Views: 6060
Reputation: 336
For loading your jQuery, you usually use the $(document).ready()
method to ensure it loads AFTER the content of the site is loaded. So anything in there will not work straight away, but only after everything else is loaded (which, according to your question, takes some time). To fix this, you might try placing
$("#link").on("mouseover", function() { ... }
- which indeed is the long form of $("#link").hover()
- directly after the element with the link id instead of inside the $(document).ready()
block.
As for the moving-out-moving-in part: mouseover is triggered when you move the pointer from outside of the element to the inside. So having the pointer already inside the element will not trigger anything, even if moving around in that element.
Try using mousemove instead:
$("#link").on("mousemove", function() {
console.log("hover");
})
Also, you might want to dive into the jQuery API documentation, as the functions and mouse events (and how they work) are explained there in detail: http://api.jquery.com/
EDIT: If using mouseenter
, as suggested, it's basically the same problem: You will have to move out of the element and back in to trigger the event. The difference between mouseover
and mouseenter
is explained here: What is the difference between the mouseover and mouseenter events?
Upvotes: 4
Reputation: 4744
Try "mouseenter" instead of "mouseover", it works.
$(document).ready(function() {
$("#link").on("mouseenter", function() {
console.log("hover");
});
});
#link {
padding: 20px;
background: #FFF000;
font-size: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="link">link here</a>
Upvotes: 2