Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If mouse over for over 2 seconds then show else don't?

Here is a jQuery slide function I have applied to a div on hover in order to slide a button down.

It works fine except that now everytime someone moves in and out of it, it keeps bobbing up and down.

I figured if I put a one or two second delay timer on it it would make more sense.

How would I modify the function to run the slide down only if the user is on the div for over a second or two?

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js "></script> <script type="text/javascript">  $("#NewsStrip").hover( function () {     $("#SeeAllEvents").slideDown('slow');    }, function () {     $("#SeeAllEvents").slideUp('slow'); });  </script> 
like image 390
user1266515 Avatar asked Jun 29 '12 14:06

user1266515


People also ask

How mouseover works?

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 .

What is the name of the event that occurs when the mouse pointer hovers over an element?

The onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children.

How to use mouseover and mouseout in jQuery?

The mouseout() method triggers the mouseout event, or attaches a function to run when a mouseout event occurs. Note: Unlike the mouseleave event, the mouseout event is triggered if a mouse pointer leaves any child elements as well as the selected element.

How to use jQuery mouseover?

jQuery mouseover() MethodThe mouseover() method triggers the mouseover event, or attaches a function to run when a mouseover event occurs. Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element.


1 Answers

You need to set a timer on mouseover and clear it either when the slide is activated or on mouseout, whichever occurs first:

var timeoutId; $("#NewsStrip").hover(function() {     if (!timeoutId) {         timeoutId = window.setTimeout(function() {             timeoutId = null; // EDIT: added this line             $("#SeeAllEvents").slideDown('slow');        }, 2000);     } }, function () {     if (timeoutId) {         window.clearTimeout(timeoutId);         timeoutId = null;     }     else {        $("#SeeAllEvents").slideUp('slow');     } }); 

See it in action.

like image 157
Jon Avatar answered Sep 23 '22 17:09

Jon