0

What is the syntax to get this bit of jQuery to call when say, h2.myClass is hovered?

$(document).ready(function(){
  setTimeout(function(){
  $("div.clickme, div.clickMeTimes").fadeOut("slow", function () {
  $("div.clickme, div.clickMeTimes").remove();
  });

  }, 2500);
});

Thanks to all the answers, alot of them were very good but @SKS went the extra mile with my extra requirements. The below fades in my div in and out on mouse hover instead of the initial on page load.

$(document).ready(function(){
  $('h2.myClass').hover (function() {
       $("div.clickme, div.clickMeTimes").stop(true).fadeOut("slow");
  }, function () {
       $("div.clickme, div.clickMeTimes").stop(true).fadeIn("slow");       
  });
});

5 Answers 5

2

Since you want something to be executed only on mouse enter. You can just use mouseenter function.

I think below is what you want,

$(document).ready(function(){
  $('h2.myClass').mouseenter(function() {
    setTimeout(function(){
         $("div.clickme, div.clickMeTimes").fadeOut("slow", function () {
            $(this).remove();
         });
    }, 2500);
  });
});

Also modified $("div.clickme, div.clickMeTimes").remove(); inside the callback to $(this).remove() which will remove the corresponding element instead of trying to remove both element.

Edit: Try below, If you it to fadeIn and fadeOut.

$(document).ready(function(){
  $('h2.myClass').hover (function() {
       $("div.clickme, div.clickMeTimes").stop(true).fadeOut("slow");
  }, function () {
       $("div.clickme, div.clickMeTimes").stop(true).fadeIn("slow");       
  });
});
Sign up to request clarification or add additional context in comments.

6 Comments

Nice - I think i'll accept this, but do you know how I can .show(); the div on mouseleave?
@SMacFadyen If you have something to do on mouse leave then you should use hover. But you are removing those div's? .remove will remove those divs from the DOM.
ok.. I just wanted it to toggle.. so maybe just .css(display: block;) and display: none; ?
Possibly, the div is shown and fadeOut on mouseenter. Is it possible to add the div back on mouseleave? essentially returning to the first state
@SMacFadyen See my edited answer at the end of my post. Let me know if it works for you.
|
1
$(".myClass").hover(function(eIn) { // this is the function for when the mouse is hovered over the item
    //do work
},
function(eOut) {  // this is the func when the mouse leaves the item
    //do work
});

Comments

1
$("h2.myClass").hover(function(){
  setTimeout(function(){
  $("div.clickme, div.clickMeTimes").fadeOut("slow", function () {
  $("div.clickme, div.clickMeTimes").remove();
  });

  }, 2500);
}

Comments

1

Put your logic in a function :

var fadeAndRemove = function() {

     setTimeout(function(){
         $("div.clickme, div.clickMeTimes").fadeOut("slow", function () {
              $("div.clickme, div.clickMeTimes").remove();
         });

      }, 2500);
}

then bind it to the hover event :

$("h2.myClass").on('mouseenter', function() {
     fadeAndRemove();
});

Use hover if you have somthing to do on mouseleave also.

Comments

1

If you just need to make the hover work:

$("h2.myClass").mouseover(function(){
  $("div.clickme, div.clickMeTimes").fadeOut("slow", function () {
  $("div.clickme, div.clickMeTimes").remove();
});

Otherwise if you need to make it work AND stop the timer:

$(document).ready(function(){
  timer = setTimeout(function(){

$("h2.myClass").mouseover(function(){
  clearTimeout(timer);
  $("div.clickme, div.clickMeTimes").fadeOut("slow", function () {
  $("div.clickme, div.clickMeTimes").remove();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.