Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent hovering effect after pointer moves away

I want to display an element on hover. Following code does it. However there is a problem, if you hover over multiple times, the element keeps displaying as many times when the cursor goes away.

Hover over the link multiple times in the demo to understand the problem.

$('li').hover(function() {
  $(this).find('.box').delay(100).fadeIn();
}, function() {
  $(this).find('.box').delay(100).fadeOut('fast');
});
.box {
  color: #FFF;
  width: 200px;
  background: #000;
  position: absolute;
  display: none;
  margin-top: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="images">
  <li>
    Show
    <div class="box">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce eu condimentum odio, non congue quam. Cras feugiat nulla at mattis semper.
    </div>
  </li>
</div>

JSFiddle https://jsfiddle.net/e99afrg8/

like image 511
sam Avatar asked Sep 10 '26 12:09

sam


2 Answers

Nothing wrong with jQuery, but you can go lighter with CSS :hover and transition.

No more post-hovering display effect.

.box {
    color: #FFF;
    width: 0;
    height: 0;
    background: #000;
    position: absolute;
    /* display: none; */
    margin-top: 50px;
}

div:hover .box {
    width: 200px;
    height: 100px;
    transition: all .1s linear .1s; /* property, duration, timing-function, delay */
}
<div id="images">
    <li>
        Show
        <div class="box">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
            Fusce eu condimentum odio, non congue quam. 
            Cras feugiat nulla at mattis semper.
        </div>
    </li>
</div>
like image 173
Michael Benjamin Avatar answered Sep 13 '26 01:09

Michael Benjamin


jQuery animations are queued by default. So, as you hover on and off rapidly, all those animations are queued up and continue playing after you stop hovering.

I suggest using stop() to "[s]top the currently-running animation on the matched elements."

In my example, I have also chosen to clear the queue but not jump to the end of the current animation. You may want to experiment with those values and see which behavior you prefer.

.stop( [clearQueue ] [, jumpToEnd ] )

$('li').hover(function() {
  $(this).find('.box').stop(true,false).delay(100).fadeIn();
}, function() {
  $(this).find('.box').stop(true,false).delay(100).fadeOut('fast');
});
.box {
  color: #FFF;
  width: 200px;
  background: #000;
  position: absolute;
  display: none;
  margin-top: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="images">
  <li>
    Show
    <div class="box">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce eu condimentum odio, non congue quam. Cras feugiat nulla at mattis semper.
    </div>
  </li>
</ul>
like image 20
showdev Avatar answered Sep 13 '26 00:09

showdev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!