Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

without jquery i need to find out if the mouse is over an element, not determine when it becomes over (in case it doesn't move to trigger onmouseover)

Tags:

javascript

without jquery

basically what I am looking for is the ability to see if the mouse is over a div when a countdown finishes

if the user is over the div then perform action for that div

onmouseover only triggers when the mouse crosses the threshold of the div, if the mouse hasn't moved it wouldn't trigger, so that wouldn't work

I need to determine if the mouse is currently over a div at a specific point in time, if it has moved or not from the starting point

all of my hunting has only found onmousover, and nothing to see if the mouse just happens to be there to begin with

I don't have the javascript skills to determine overall coords of div, then map mouse coords and see if it fits there... which is what I believe I need to do

like image 525
Kender Avatar asked Aug 19 '13 18:08

Kender


People also ask

How do you check if the mouse is over an element?

You can simply use the CSS :hover pseudo-class selector in combination with the jQuery mousemove() to check whether the mouse is over an element or not in jQuery. The jQuery code in the following example will display a hint message on the web page when you place or remove the mouse pointer over the DIV element's box.

Is triggered when the mouse moves over an element?

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.

Is a mouse event that will be triggered when the mouse leaves an object?

The mouseover event occurs when a mouse pointer comes over an element, and mouseout – when it leaves. These events are special, because they have property relatedTarget . This property complements target . When a mouse leaves one element for another, one of them becomes target , and the other one – relatedTarget .

Which event triggers when we bring mouse cursor over any element?

on( "mouseover", handler ) in the first two variations, and . trigger( "mouseover" ) in the third. The mouseover event is sent to an element when the mouse pointer enters the element. Any HTML element can receive this event.


2 Answers

After reading the second answer (the one with millions of a elements) on this SO question, I've came up with this method works without moving the mouse on page load, without involving millions of elements.

HTML

<div id=t></div>

CSS

#t {
    /* for illustrative purposes */
    width: 10em;
    height: 5em;
    background-color: #0af;
}
#t:hover {
    border-top-style: hidden;
}

JavaScript

document.addEventListener('click', function () {
    var c = window.getComputedStyle(document.getElementById('t')).getPropertyValue('border-top-style');

    if (c === 'hidden') {
        alert('Mouse in box');
    } else {
        alert('Mouse not in box');
    }
}, false);

As stated earlier, bind to the finish event of your countdown instead of the click event on the document.

You may also use any CSS style that's changed on :hover, I chose border-top-style as it is conspicuous. If you're using a border, choose something else.

Here's a jsFiddle.

like image 53
rink.attendant.6 Avatar answered Oct 15 '22 07:10

rink.attendant.6


set a flag to true onmouseover and to false onmouseleave. when countdown finishes if flag is true then it is over element.

HTML

<div id="div-name">the section of the code i am working with has a countdown timer, when it reaches 0 i need to know if the mouse is over a specific box</div>
<button id="notification" onclick="javascript: letsCountIt(5);">click to start countdown</button>

JS

window.ev = false;

document.getElementById('div-name').onmouseover = function () {
    window.ev = true;
    console.log(window.ev);
}

document.getElementById('div-name').onmouseout = function () {
    window.ev = false;
    console.log(window.ev);
}

window.letsCountIt = function (cdtimer) {
    cdtimer--;
    document.getElementById('notification').innerHTML = cdtimer;

    if (cdtimer == 0) {
        if (window.ev === true) {
            alert('over');
        } else {
            alert('not over');
        }
    } else {
        setTimeout(function(){letsCountIt(cdtimer);}, 1000);
    }
}
like image 39
km6zla Avatar answered Oct 15 '22 07:10

km6zla