Animation zoomIn is flashing with mouseenter

Asked

Viewed 231 times

0

I’m using Animate.css to zoom in on an image. The idea is that the animation only works with the Hover/mouseenter, that is, when the user hovers over it. When I use any animation other than zoomIn, it works normally; however, with the code below, although the zoom works, the image is blinking when I hover the mouse in certain places. If I shoot the . mouseout, the event only occurs once. What to do to resolve?

<img src="imagens/icone.png" class="hover" />

    <script>
        $(document).ready(function() {
            $('.hover').mouseenter(function() {
                var mv = $(this);
                mv.addClass('animated zoomIn');
            });
            $('.hover').mouseout(function(){
              var mv = $(this);
              mv.removeClass('animated zoomIn');
            });
        });
    </script>
  • Hello Rafaela, this happens because when zoomIn class is added the image is resized from Scale(0) to Scale(1). In this transformation, the image is so small that the mouse comes out of it. Actually the image that comes out of the mouse. Then the second mouseout event is triggered, causing the image to return to Scale(1) again. Then when you touch the mouse, the first function is triggered and this is in an infinite loop. I’m trying to think of a solution for you.

  • got it. It makes perfect sense, since it just doesn’t work THAT animation and with the Hover. Other animations and other means of making it work (click and scroll, for example), work normally.

2 answers

1


As I had commented, this error was happening by the following:

When the class zoomIn is added, it changes the properties of the element like scale, that goes from 0 to 1. That’s what makes animation occur.

Just as you’re listening to 2 events, one of mouseenter and one of mouseleave, the Handler of mouseenter is executed at the moment the user passes the mouse. But how the scale is lowered to 0, the image automatically exits from below the mouse and triggers the Handler mouseleave. That one handler in turn, take the class zoomIn.

These events keep repeating over and over again That’s why you see the blinking image.

One solution is as follows::

$('.hover').mouseenter(function() {
    var mv = $(this);
    mv.addClass('animated zoomIn').one("mozAnimationEnd webkitAnimationEnd oanimationend animationend", function(){
      mv.removeClass('animated zoomIn');
    });
});

In the documentation it is explained that the method one() Jquery is very useful when you want to at least run one handler for a function and is what you need. You need the zoomIn is executed at least once before you can remove it.

You can see how it works here.

I hope this solves your problem.

Hugs.

  • worked out! thank you very much

0

Andrew’s answer is perfect, really solves the problem, I leave an alternative here if you choose not to use an entire library with effects that can be performed easily (even with more visual implementations) only with CSS.

img:hover {
  cursor: pointer;
  animation-name: imgAnime;
  animation-duration: 1.2s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: 1;
  animation-play-state: running;
}

@keyframes imgAnime {
  0% { 
    width: 200px;
    height: 200px;
    opacity: 0.2;
  }
  25% {
    width: 80px;
    height: 80px;
    opacity: 0.4;
  }
  50% {
    width: 120px;
    height: 120px;
    opacity: 0.6;
	}
  75% {
    width: 160px;
    height: 160px;
    opacity: 0.8;
  }
  100% {
    width: 200px;
    height: 200px;
    opacity: 1.0;
  }
}
<img src="https://images.pexels.com/photos/1028674/pexels-photo-1028674.jpeg?cs=srgb&dl=apple-black-braided-1028674.jpg&fm=jpg" class="hover" width="200" height="200"/>

  • Young u saw what happens when you put the mouse on the right edge or at the bottom edge of your image. Your animation is pretty messed up...

  • I just implemented some css properties, to demonstrate how it’s more flexible to do with css. I hadn’t really noticed the problem at the edge, plus some bug?

  • If the author came trying to solve a bug in the animation, wouldn’t it be nice to respond with an option that tb is bugged don’t you agree? I also think that the ideal would be to do only with CSS unless it already has jQuery in the project etc. In the case of your animation is bugged I believe that it is by the way you assembled everything. If Hover is in a div that has the image inside you should be able to fix the bug. If you can’t, open a Question that I or another user helps you solve.

  • I tested with the img inside the div first, but wanted to keep its html since the query has more to do with the jQuery problem, which was solved in Andrew’s answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.