How to detect whether mouseout is true or not? - javascript

I have this simple fiddle as a working example-
Jsfiddle
I am trying to detect mouseout event from a div section.
When i mouseover on this image it shows caption; saying "Change Image". After 5 seconds caption goes fadeOut.
I am using setInterval to set it accordingly. Now if I do mouseout of this image, then only I want Interval function should be called.
How do i detect mouseout event in jQuery?
Tried-
$(function () {
$('.image-profile').mouseover(function () {
$('.change-image').stop().show();
if ($('.image-profile').mouseout()== true) {
TimeOut();
}
});
setInterval(function TimeOut() {
$('.change-image').fadeOut()
}, 5000
);
});

var ImageProfileTimer;
$('.image-profile').on('mouseenter',function(){
clearTimeout(ImageProfileTimer);
$('.change-image').stop().show();
}).on('mouseleave',function(){
ImageProfileTimer = setTimeout(function(){
$('.change-image').fadeOut()
}, 5000);
});
Use setTimeout and clearTimeout
Demo : http://jsfiddle.net/xMNTB/9/

$('.image-profile').on('mouseleave', function() {
setTimeout(function() {
$('.change-image').fadeOut()
}, 5000);
});

http://jsfiddle.net/xMNTB/7/
Now the div show up on mouse enter and disappears 5 seconds after mouse leave.
$(function () {
$('.image-profile').mouseenter(function () {
$('.change-image').stop().show();
});
$('.image-profile').mouseleave(function () {
setTimeout(function TimeOut() {
$('.change-image').fadeOut()
}, 5000);
});
});

Try This:
(function () {
$('.image-profile').mouseover(function () {
$('.change-image').stop().show();
if ($('.image-profile').mouseout() == true) {
TimeOut();
}
}).mouseout(function () {
setInterval(function TimeOut() {
$('.change-image').fadeOut()
}, 5000);
});
});
JSFIDDLE DEMO

Related

document.getElementById("st").click(); not working

I want to click a button automatically on every page refresh.But when i have the window.location.reload function the button click is not happening.I am really confused on this one.What to do to get a button clicked automatically on page refresh?
window.setTimeout(function () {
window.location.reload();
repeat();
}, 2000);
function repeat(){
document.getElementById("st").click();
}
window.setTimeout(function () { repeat();
window.location.reload();
}, 2000);
Call repeat before reload function.
This is working!
document.getElementById("st").onclick = function() {
alert('Clicked');
};
window.setTimeout(function() {
window.location.reload();
repeat();
}, 2000);
function repeat() {
document.getElementById("st").click();
}
<div id="st">Div</div>

JQuery .slideUp() after time OR mouseleave

I got an element that is slided down by JQuery using .slideDown() method
$('#dropdown_shopping_cart').slideDown(800);
Now i want it to slide up after 6 seconds, but only if there is no hover on the element, if there is an hover, it should not .slideUp().
So far i worked with a timeout that added display:none to the element while i was giving the element´s hover display:block!important; in CSS so it would not get display: none until the hover is over.
JS
setTimeout(function () {
$('#dropdown_shopping_cart').css('display', 'none');
}, 6000);
_______________________________________________________
CSS
#dropdown_shopping_cart:hover {
display: block!important;
}
Now i want to add the .slideUp() to this.
Check this:
var myVar;
myVar = setTimeout(function() {
$('#dropdown_shopping_cart').slideUp(800)
}, 6000);
$("#dropdown_shopping_cart").hover(
function() {
clearTimeout(myVar);
},
function() {
myVar = setTimeout(function() {
$('#dropdown_shopping_cart').slideUp(800)
}, 6000);
}
);
By default shopping cart will slideUp() after 6 seconds, if mouse hover action occured, setTimeOut will be cleared, after mouse leave the shopping cart, setTimeOut will setted automatically
You can clear the timeout on mouseenter and reset it on mouseleave like this:
var hide_div_to;
function hideDiv(){
hide_div_to = setTimeout(function () {
$('#dropdown_shopping_cart').slideUp(800);
}, 6000);
}
$('#dropdown_shopping_cart').slideDown(800,hideDiv());
$('#dropdown_shopping_cart').mouseenter(function(){
clearTimeout(hide_div_to);
});
$('#dropdown_shopping_cart').mouseleave(function(){
hideDiv();
});
Here is a working JSFiddle
UPDATE
If you don't wan't to wait the timeout again when you leave, after the timeout is reached, you can do this:
$('#dropdown_shopping_cart').slideDown(800);
setTimeout(function () {
if(!$('#dropdown_shopping_cart').is(':hover')){
$('#dropdown_shopping_cart').slideUp(800);
}
else{
$('#dropdown_shopping_cart').mouseleave(function(){
$('#dropdown_shopping_cart').slideUp(800);
});
}
}, 3000);
And here is a JSFiddle and here is another one that shows how this can be triggered multiple times.
Id suggest you work with mouseover and a class:
$('#dropdown_shopping_cart').hover(function(){
if(!$('#dropdown_shopping_cart').hasClass('active'))
{
$(this).addClass('active');
}
else
{
$(this).removeClass('active');
}
},
function() {
var myVar = setTimeout(function() {
if(!$('#dropdown_shopping_cart').hasClass('active'))
{
$('#dropdown_shopping_cart').slideUp()
}
}, 6000);
})
And than in your setTimeout Function you add:
demo: http://jsfiddle.net/yo5gnvy3/7/
$('#dropdown_shopping_cart').hide().slideDown(800, function () {
var events = $._data($(this)[0], "events") || {};
if (events.mouseover === undefined) {
$(this).delay(1000).slideUp()
}
});

How do I stop text scrolling?

This is my code:
$(document).ready(function() {
function ticker() {
$('#ticker li:first').slideUp(function() {
$(this).appendTo($('#ticker')).slideDown();
});
}
setInterval(function(){ ticker(); }, 3000);
});
I don't know how to stop the text scrolling when I place the mouse over a particular title.
DEMO
Use a hover() to clear the interval and when mouse leaves then again start the ticker like,
$(document).ready(function () {
function ticker() {
$('#ticker li:first').slideUp(function () {
$(this).appendTo($('#ticker')).slideDown();
});
}
var clr = null;
function animate(){
clr=setInterval(function () {
ticker();
}, 3000);
}
animate();
$('#ticker li').hover(function () {
// clear interval when mouse enters
clearInterval(clr);
},function(){
// again start animation when mouse leaves
animate();
});
});
Live Demo
Minor update in your answer. Use mouseover and out function.
$(document).ready(function() {
function ticker() {
$('#ticker li:first').slideUp(function() {
$(this).appendTo($('#ticker')).slideDown();
});
}
var ticke = setInterval(function(){ ticker(); }, 3000);
$('#ticker li').mouseover(function() {
clearInterval(ticke);
}).mouseout(function() {
ticke= setInterval(function(){ ticker(); }, 3000);
});
});
See here Demo
please try this one:
$(document).ready(function () {
function ticker() {
$('#ticker li:first').slideUp(function () {
$(this).appendTo($('#ticker')).slideDown();
});
}
var clr = null;
function animate(){
clr=setInterval(function () {
ticker();
}, 3000);
}
animate();
$('#ticker li').hover(function () {
// clear interval when mouse enters
clearInterval(clr);
},function(){
// again start animation when mouse leaves
animate();
});
});
The setInterval method returns an id value which you can then pass to clearInterval to cancel the calls to ticker().

set timer on jquery pop up

I am opening a jquery popUp div using
$("#saveDialogSingleFeature").dialog();
I only want the div to appear for 3 seconds then close automatically
tried this using $("#saveDialogSingleFeature").dialog('close')}, 3000);
but this doesnt do anything....shouldnt it close?
$(function () {
$("#saveDialogSingleFeature").dialog();
$("#saveDialogSingleFeature").dialog('close')}, 3000);
});
$(function () {
$("#saveDialogSingleFeature").dialog();
setTimeout(function(){
$("#saveDialogSingleFeature").dialog('close')
}, 3000);
});
$(document).on('popupafteropen', '.ui-popup', function() {
setTimeout(function () {
$(this).popup('close');
}, 3000);
});
Plz Let me know this works for you or not
You can do it in "open" section with use of setTimeout:
$("#saveDialogSingleFeature").dialog({
open: function() {
var a = $(this);
setTimeout(function() {
a.dialog('close');
}, 3000);
}
});

How to change few css class on button in jQuery by timer?

When document is ready, there is a button that gets a new class names frame1. I want to set another class on this button after two seconds.
$(document).ready(function () {
setTimeout(function () {
$('#button').addClass('frame1')
}, 2000);
});
I want to add another class 'frame2' after two seconds if that is possible?
If you just want to add a class every coule of seconds until you have no more classes to add, you could use an interval and then clear it when the classes have all been added:
var group = [ 'whiteFG', 'redBG' ];
var intvl= setInterval(function(){
group.length
? $("#foo").addClass(group.pop())
: clearInterval(intvl) ;
}, 2000);
Fiddle: http://jsfiddle.net/EvXPy/
Or you could simply iterate over your array of class names and set timeouts from there:
$(['redBG','whiteFG']).each(function(i,o){
setTimeout(function(){
$("#foo").addClass(o);
}, i * 2000 );
});​​​​​​​​
Note that the first effect will run immediately since i is equal to the index of 0, meaning the timeout happens instantly. If you want this to be delayed, you can increment i as you see fit before multiplying by 2000.
Fiddle: http://jsfiddle.net/EvXPy/1/
You can do it this way.
$(document).ready(function () {
setTimeout(frame1(), 2000);
});
function frame1() {
$('#button').addClass('frame1');
setTimeout(frame2(), 2000);
}
function frame2() {
$('#button').addClass('frame2');
}
Or you could do it this way.
$(document).ready(function () {
setTimeout(function () {
$('#button').addClass('frame1')
}, 2000);
setTimeout(function () {
$('#button').addClass('frame2')
}, 4000);
});
setTimeout(function () {
$('#button').addClass('frame1');
}, 2000);
setTimeout(function () {
$('#button').addClass('frame2');
}, 2000);
UPDATE:
If you want to check whether class is exists then do this way.
setTimeout(function () {
$('#button').addClass('frame1');
if($('#button').hasClass('frame1')) {
setTimeout(function () {
$('#button').removeClass('frame1').addClass('frame2');
}, 2000);
}
}, 2000);
Refer LIVE DEMO

Categories