Reputation: 45
I'm trying to use this method to get a div to fadeIn
when a user mouses over a text field that tells them what sort of information goes into the field. I've got the <div>
to follow the cursor while in the text field if the user moves the mouse up or left, but when I move the mouse down or right the <div>
disappears during the motion to reappear when the mouse stops. Here is a JSFiddle showing my relevant code and the odd behavior it's yeilding.
$(document).ready(function() {
$(document).bind("mousemove", function(e) {
$(".hover").css({
"left" : e.pageX,
"top" : e.pageY
});
});
$(".num").hover(function() {
$(".hover").stop().html("Enter a number").delay(500).fadeIn(150);
}, function() {
$(".hover").stop().hide();
});
});
.hover {
display: none;
position: absolute;
float: left;
font-family: Calibri, sans-serif;
font-size: 0.7em;
background-color: rgb(255,255,230);
border: 1px solid black;
-webkit-box-shadow: 0 0 2px rgba(0,0,0,0.4);
box-shadow: 0 0 2px rgba(0,0,0,0.4);
padding: 1px 3px;
z-index: 50;
}
input {
margin: 30px;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class='hover'></div>
<input type="text" class="num" size="2">
.hover()
JQuery method to other elements in my web page to see if it's a problem with applying it to a text field, but the down/right vanishing behavior persisted.
In my full code, the hovering <div>
is created dynamically when the user first mouse-overs an element which produces the effect, but I couldn't get that aspect to work is JSFiddle.
Since this effect seems easily-produced by others I'd like to know not only how to achieve the correct behavior but also understand why my attempt came out so wonky.
Upvotes: 2
Views: 3705
Reputation: 93561
Basically if your mouse moves over the .hover
overlay, it generates a hover "out" on the item underneath your mouse. Moving the mouse left/right causes the cursor to move on and off the overlaid div.
Add pointer-events: none;
to your .hover
style. This will stop it being visible to the mouse and avoid generating any events on the .hover
:
http://jsfiddle.net/4ba70vy3/6/
.hover {
pointer-events: none;
display: none;
position: absolute;
float: left;
font-family: Calibri, sans-serif;
font-size: 0.7em;
background-color: rgb(255, 255, 230);
border: 1px solid black;
-webkit-box-shadow: 0 0 2px rgba(0, 0, 0, 0.4);
box-shadow: 0 0 2px rgba(0, 0, 0, 0.4);
padding: 1px 3px;
z-index: 50;
}
Update: If the .hover was below the mouse, vertical movement would cause the same problem.
http://jsfiddle.net/4ba70vy3/7/
Also, as DevlshOne
suggests, you might as well use the title=
attribute on the controls and maybe just do yours for older browsers?
Upvotes: 4
Reputation: 3820
Just add a pointer-event:none
to the .hover
class
Working example: http://jsfiddle.net/4ba70vy3/
Upvotes: 0