How to use if with Eventlistener

Asked

Viewed 27 times

1

Hello, I wonder if there is a possibility to give an ' if(element.Eventlistener('mouseover')... ' in Javascript!

I need to do the following action: When passing the mouse over the x element happens something and removing the mouse happens another.

Follow Javascript code

        <script>
        function effect () {
            var element = document.getElementById('caracters');

            if (element.addEventListener == true) {
                document.getElementById('effect').style.animation = "effectload 2s infinite";
            }else {
                document.getElementById('effect').style.animation = "none";
            }
        }

        document.getElementById('caracters').addEventListener('mouseover', () => effect());
    </script>

Immediately, thank you for your help! :)

  • 1

    onmouseover object = function(){ logic };

  • What is that if should do? What condition are you trying to verify?

1 answer

1

You can use the event mouseleave which will be executed when your cursor is over your element.

Example:

document.getElementById('caracters').addEventListener('mouseleave', () => console.log('saiu'));
  • Oops, thank you very much Bruno for the help!

Browser other questions tagged

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