So I have this code:
HTML
<p id="lorem">
<span class="red" id="n01">L</span>
<span class="red" id="n02">o</span>
<span id="n03">r</span>
<span id="n04">e</span>
<span class="blue" id="n05">m</span>
<span id="n06">i</span>
<span class="blue" id="n07">p</span>
<span class="red" id="n08">s</span>
<span id="n09">u</span>
<span class="blue" id="n10">m</span>
</p>
<br />
<div id="bar-red"></div>
<div id="bar-blue"></div>
JS
function legend( elem ) {
var timer;
var k;
var h = $( elem );
if( elem == ".red" ) {
k = $("#bar-red");
}
if( elem == ".blue" ) {
k = $("#bar-blue");
}
h.hover(function() {
console.log(h);
if(timer) {
clearTimeout(timer);
timer = null
}
timer = setTimeout(function(){
k.animate({width: "30px"}, 200);
}, 300)
},
function(){
k.animate({width: "10px"}, 200);
});
}
legend(".blue");
legend(".red");
To understand the problem a bit better I have also created a Fiddle. When you hover over a blue letter and let the mouse rest there for a short amount of time, the blue bar expands. Same thing for red letters and the red bar. The problem is, sometimes is lags, the bar stays expanded or won't really expand. Why is it like this?
In my production environment I have about 150 letters and the hover is basically unusable. Or could this be because I add the classes dynamically (not in Fiddle)?
Also, while debugging with Firebug, I noticed something strange. I hover over one element and log h, it shows all elements with the class. That's why I think I maybe have some kind of logic error in this function. Maybe it fires multiple times at once and that's why it lags?
Any held is appreciated!