Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if mouse is still over element in Javascript

I want to write to the console is the user is keeping the mouse over an element for more than 2 seconds, this is the code:

var $els = document.querySelectorAll('#myDiv');
    for(i = 0; i < $els.length; i++) {
        if ($($els[i]).data('track') == 'hover') {
            $els[i].addEventListener('mouseover', function (e) {
                setTimeout(function() {
                    if ($($els[i]).is(':hover')) {
                        console.log('mouse is still over this element');
                    }
                }, 2000);
            });
        }
    }

the message is written to the console after 2 seconds, even if I keep the mouse on the element less than 2 seconds. I am probably missing something in here:

if ($($els[i]).is(':hover')) {

thanks for your help

like image 501
user3174311 Avatar asked Apr 12 '17 10:04

user3174311


People also ask

What is mouse over in Javascript?

The onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children. Tip: This event is often used together with the onmouseout event, which occurs when a user moves the mouse pointer out of an element.

What is the opposite of mouseover in Javascript?

The mouseover event fires when the user moves the mouse onto an element. The mouseout event fires when the user moves the mouse out of an element.

What is difference between Mouseenter () and mouseover () event?

The mouseover event triggers when the mouse pointer enters the div element, and its child elements. The mouseenter event is only triggered when the mouse pointer enters the div element. The onmousemove event triggers every time the mouse pointer is moved over the div element.

Which of the following event occurs when the mouse pointer is moved exactly onto an element but not to its children?

mouseover: The onmouseover event triggers when the mouse pointer enters an element or any one of its child elements. mouseenter: The onmouseenter event is triggered only when the mouse pointer hits the element.


2 Answers

Create a global variable as

var isMouseHover = false;

Call function on the mouseover call mouseover function and set

isMouseHover = true;

And call mouseout, when mouse out and set

isMouseHover = false;

Use this isMouseHover variable whenever you want.

Try this:

let isMouseHover = false
let test = document.getElementById("test");
test.addEventListener("mouseleave", function (event) {
  isMouseHover = false
  event.target.textContent = "mouse out"
  console.log(isMouseHover)
}, false);
test.addEventListener("mouseover", function (event) {
  isMouseHover = true
  event.target.textContent = "mouse in"
  console.log(isMouseHover)
}, false);
  <div id="test">hover me</div>
like image 96
Saurabh Agrawal Avatar answered Oct 01 '22 22:10

Saurabh Agrawal


Edit : I updated my previous answer with a better solution.

First, you propably have several elements with the same id and it's a bad things, so I changed to a class. Then I updated your code to simplify it and to make it more in the spirit of jQuery :

HTML :

<div class="myDiv" data-track='hover'>
test
</div>
<div class="myDiv" data-track='hover'>
test2
</div>

JavaScript :

$('.myDiv[data-track=hover]').on("mouseenter",function(){
    var $elem = $(this);
  $elem.attr("data-hover",true);
  window.setTimeout(function() {
    if ($elem.attr("data-hover")) {
        console.log('mouse is still over this element');
        console.log($elem);
    }
  }, 2000);
})

$('.myDiv[data-track=hover]').on("mouseenter",function(){
  $(this).attr("data-hover",false);
});

Here is a working jsFiddle.

like image 24
Nico_ Avatar answered Oct 02 '22 00:10

Nico_