I want to measure the time that a mouse cursor is in a specific area (div-container). See code below.
Until now, the leave and enter times (hover-times) are only measures when the mouse cursor enters and leaves the respective container (due to the mouseleave and mouseenter event) but not when the user uses mouse wheel or scrolls the window. My goal is to change the code in a way that the hover time is also measured when the window is scrolled (e.g. the user uses the mouse wheel).
I already tried to implement a .scroll() event with the selector 'document'.
Has anyone already encountered the same problem and has a solution for the problem?
var hover_events = {};
var enter_time;
var leave_time;
$(".element").on('mouseenter', function(d) {
enter_time = Date.now();
hover_events[d.target.id] = {
element_id: d.target.id,
enter_time: enter_time,
leave_time: undefined,
};
});
$(".element").on('mouseleave', function(d) {
leave_time = Date.now();
hover_events[d.target.id]["leave_time"] = leave_time;
console.log(hover_events[d.target.id])
delete(hover_events[d.target.id]);
});
.element {
margin: 100px;
width: 100px;
height: 100px;
background-color: lightblue;
}
#scroll {
width: 1000px;
height: 1000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="scroll">
<div class="element" id="test">
</div>
</div>