Reputation: 3165
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");
});
});
Upvotes: 0
Views: 67
Reputation: 79830
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");
});
});
Upvotes: 2
Reputation: 2590
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();
});
Upvotes: 1
Reputation: 5580
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.
Upvotes: 1
Reputation: 2620
$("h2.myClass").hover(function(){
setTimeout(function(){
$("div.clickme, div.clickMeTimes").fadeOut("slow", function () {
$("div.clickme, div.clickMeTimes").remove();
});
}, 2500);
}
Upvotes: 1
Reputation: 22570
$(".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
});
Upvotes: 1