Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call JS function on "hover" from HTML?

I have to catch "hover" and after call JS function. I call it from html. But nothing happen. I tried also to use mouseover - also doesn't work from html. I have to catch "hover",but can't make event listener in JS file on "hover".I can put event listener on "mouseover" but it doesn't work correct when mouse moves fast). What mistake I do that I don't have any reaction on changeDef(event)?

function changeDef(event){
  console.log(event.target);
}
<img class="img-default" src="./img/footer1.png" hover="changeDef(event)">
like image 629
Natalia Avatar asked Mar 18 '18 16:03

Natalia


People also ask

How trigger a function on hover in JS?

jQuery hover() Method The hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events. Note: If only one function is specified, it will be run for both the mouseenter and mouseleave events.

Can we call a function on hover?

There is no "hover" event. You do use the mouseover event, which (when using HTML attributes to set up) is referenced with on in front of the event name. There won't be a problem with this event triggering even if the mouse is moving fast.

Can you call a JS function in HTML?

The first method is to call the JavaScript function in HTML. For this, you have to create a function then define this function either in the head section or body section of the HTML document. You can either create a link or a button and then an onclick() event is associated with them in order to call this function.

How do I use mouseover in HTML?

The :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.


Video Answer


2 Answers

There is no "hover" event. You do use the mouseover event, which (when using HTML attributes to set up) is referenced with on in front of the event name. There won't be a problem with this event triggering even if the mouse is moving fast.

function changeDef(event){
  console.log(event.target);
}
<img class="img-default" src="./img/footer1.png" onmouseover="changeDef(event)">

But, you really should not use the 25+ year old technique of setting up event handlers via HTML attributes (which introduces a variety of issues) and, instead, follow modern standards and best practices by separating the JavaScript from the HTML:

// Get a reference to the element that you want to work with
var img = document.querySelector("img.img-default");

// Set up an event handler. Notice that we don't use "on" in front
// of the event name when doing it this way.
img.addEventListener("mouseover", changeDef);

function changeDef(event){
  console.log(event.target);
}
img { width:50px; }
<img class="img-default" src="http://cdn.schoolstickers.com/products/en/819/GREEN-SMILE-00.png">

Now, in CSS, there is a hover "state" that an element can be in, and if you just want to change styling of the element, you don't need any JavaScript at all:

img { width:50px; border:2px solid rgba(0,0,0,0); }

img:hover { border:2px solid red; }
<img class="img-default" src="http://cdn.schoolstickers.com/products/en/819/GREEN-SMILE-00.png">
like image 61
Scott Marcus Avatar answered Oct 02 '22 06:10

Scott Marcus


To actually mimic the CSS hover with script, you'll need two event handlers, mouseover and mouseout, here done with addEventListener.

Updated based on a comment, showing how to toggle a class that is using transition, and with that make use of its transition effect to make the "hover" look good.

Stack snippet

var images = document.querySelector('.images');

images.addEventListener('mouseover', changeDefOver);
images.addEventListener('mouseout', changeDefOut);

function changeDefOver(e) {
  e.target.classList.toggle('opacity-toggle');
}

function changeDefOut(e) {
  e.target.classList.toggle('opacity-toggle');
}
.images {
  position: relative;
}

.images2 {
  position: absolute;
  left: 0;
  top: 0;
}

.images2 img {
  transition: opacity 1s;
}

.images2 img.opacity-toggle {
  opacity: 0;
}
<div class="images">
  <div class="images1">
    <img class="img-default" src="http://placehold.it/100x100/f00">
    <img class="img-default" src="http://placehold.it/100x100/ff0">
    <img class="img-default" src="http://placehold.it/100x100/f0f">
    <img class="img-default" src="http://placehold.it/100x100/0ff">
    <img class="img-default" src="http://placehold.it/100x100/00f">
  </div>
  <div class="images2">
    <img class="img-default" src="http://placehold.it/100x100">
    <img class="img-default" src="http://placehold.it/100x100">
    <img class="img-default" src="http://placehold.it/100x100">
    <img class="img-default" src="http://placehold.it/100x100">
    <img class="img-default" src="http://placehold.it/100x100">
  </div>
</div>

For comparison with CSS hover, here is its version

.images {
  position: relative;
}

.images2 {
  position: absolute;
  left: 0;
  top: 0;
}

.images2 img {
  transition: opacity 1s;
}

.images2 img:hover {
  opacity: 0;
}
<div class="images">
  <div class="images1">
    <img class="img-default" src="http://placehold.it/100x100/f00">
    <img class="img-default" src="http://placehold.it/100x100/ff0">
    <img class="img-default" src="http://placehold.it/100x100/f0f">
    <img class="img-default" src="http://placehold.it/100x100/0ff">
    <img class="img-default" src="http://placehold.it/100x100/00f">
  </div>
  <div class="images2">
    <img class="img-default" src="http://placehold.it/100x100">
    <img class="img-default" src="http://placehold.it/100x100">
    <img class="img-default" src="http://placehold.it/100x100">
    <img class="img-default" src="http://placehold.it/100x100">
    <img class="img-default" src="http://placehold.it/100x100">
  </div>
</div>
like image 21
Asons Avatar answered Oct 01 '22 06:10

Asons