Block fadeout when mouse over or click event on div - javascript

I used below code to fade-out #widget in 10 secs and fade-in #floating_widget in 10.2secs. This works fine.
But, #widget container should not fade-out when i clicked or mouseover #widget container. Now, it fade-out automatically because of setTimeout code.
Please let me know how to fix this issue? Thanks
HTML:
<div id="widget">
<h1>Hello World!</h1>
</div>
<div id="floating_widget">
<h2>Hello World 2!</h2>
</div>
JS:
setTimeout(function() {
$('#widget').fadeOut('slow');
}, 10000);
setTimeout(function() {
$('#floating_widget').fadeIn('slow');
}, 10200);
$('#floating_widget').click(function(){
$(this).hide();
$('#widget').show();
});

I hope I got the right understanding of your question. It seems that you have a problem with the timeouts. You will have to clear them in a onmouseover function. Here is an example of the JavaScript:
var $widget = $('#widget')
var $floatingWidget = $('#floating_widget')
var widgetFadeTimeout = setTimeout(function() {
$widget.fadeOut('slow')
}, 10000)
var floatingWidgetFadeTimeout = setTimeout(function() {
$floatingWidget.fadeIn('slow')
}, 10200)
$widget.mouseover(function() { // https://api.jquery.com/mouseover
// Remove the timeout to avoid triggering the fadeOut and fadeIn
clearTimeout(widgetFadeTimeout) // https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout
clearTimeout(floatingWidgetFadeTimeout)
// In case the element began the transitions stop them
$widget.stop()
$floatingWidget.stop()
$floatingWidget.hide()
$widget.show()
})
$floatingWidget.click(function() {
$(this).hide()
$widget.show()
})
I hope this will be useful for you!

Related

Blinking Text that Can Also Be Hidden

I would like to use blinking text to signify that data is loading. Then
hide it once the data is loaded.
I'm also using Flask.
I'm a JQuery newbie and found a recipe for flashing text, but it has the side effect, that hide didn't work.
SO answer
setInterval(function() {
$( "#blink" ).fadeToggle();
}, 500);
The I tried to hide after loading data into a div.
$("#data").load("/load_data/", function() {
$("#blink").hide('fast')
});
HTML:
<p id="blink">Loading Data</p>
<div id="data"></div>
Does this not work or am I just screwing it up...?
Is there another simple solution ?
TIA !!
Happy NY's
UPDATE:
Debugging Andrew Brooke's answer customized for a callback on load.
$("#data").load("/load_data/", function() {
$.clearInterval(blink);
$("#blink).hide("fast")
});
Assign your interval to a variable, then clear it in the .load callback with clearInterval. Then you can hide the blinking text with .hide
var blink = setInterval(function() {
$('#blink').fadeToggle();
}, 500);
$('#data').load('/load_data/', function() {
clearInterval(blink);
$('#blink').hide('fast');
});
Here's a working example
var blink = setInterval(function() {
$('#blink').fadeToggle();
}, 500);
$('#hide').on('click', function() {
clearInterval(blink);
$('#blink').hide('fast');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="Hide" id="hide">
<p id="blink">
This is blinking
</p>
You forgot some quotation marks.
("#data").load("/load_data/", function() {
$("#blink").hide('fast')
});
https://jsfiddle.net/2pf8v0nc/1/

Jquery timeout change div

I have a bunch of divs like so
<div class="listingImage">
<div style="background-image:url('listings/listing1/1/image8Main.jpeg')"></div>
<div style="background-image:url('listings/listing1/1/image2.jpeg')"></div>
<div style="background-image:url('listings/listing1/1/image3.jpeg')"></div>
<div style="background-image:url('listings/listing1/1/image4.jpeg')"></div>
<div style="background-image:url('listings/listing1/1/image5.jpeg')"></div>
<div style="background-image:url('listings/listing1/1/image6.jpeg')"></div>
<div style="background-image:url('listings/listing1/1/image7.jpeg')"></div>
<div style="background-image:url('listings/listing1/1/image9.jpeg')"></div>
</div>
With css:
.listingImage>div: {
position:absolute;
z-index:98;
bottom:0;
right:0;
left:0;
top:0;
background:50% 50%/cover;
}
.listingImage>div.active {
z-index:99;
}
I have a jquery script to cycle through these divs and change the z-index to put one on top of all the rest.
<script>
var timer
$(".listingImage").on("mouseenter", function() {
var element = $(this)
timer = window.setTimeout(function() {
if (element.children(".active").length) {
element.children(".active").removeClass("active").next().addClass("active")
} else {
element.children().first().addClass("active")
}
}, 500)
})
$(".listingImage").on("mouseleave", function() {
$(".active", this).removeClass("active")
clearTimeout(timer)
})
</script>
This script will go to next div and stop working. I believe I have two problems. It might have something to do with var element=$(this). Also, my mouseleave is being triggered by changes in z-index. How can I achieve cycling through divs and then returning to normal onmouseleave? Any help is appreciated, thank you.
Task, on mouseenter, start cycle through boxes. On mouseleave, end cycle and restart
https://jsfiddle.net/sy5br7d0/
If you want to cycle through all the div's then u should use timer = window.setInterval rather than using window.setTimeout.
moidified script:
<script>
var timer
$(".listingImage").on("mouseenter", function() {
var element = $(this)
timer = window.setInterval(function() {
if (element.children(".active").length) {
element.children(".active").removeClass("active").next().addClass("active")
} else {
element.children().first().addClass("active")
}
}, 500)
})
$(".listingImage").on("mouseleave", function() {
$(".active", this).removeClass("active")
clearTimeout(timer)
})
</script>
User setInterval() instead of setTimeout()
var timer
$(".listingImage").on("mouseenter", function () {
var element = $(this)
timer = window.setInterval(function () {
if (element.children(".active").length>0) {
element.children(".active").removeClass("active").next().addClass("active")
} else {
element.children().first().addClass("active")
}
}, 500)
})
$(".listingImage").on("mouseleave", function () {
$(".active", this).removeClass("active")
clearInterval(timer)
})
Fiddle

timeout hover delay on hover different div

I am trying to make the first square box onhover, then show the bigger box a little bit delay then when I move the mouse hover the big box, the big box still appears.However, my code works incorrectly.
If someone could please help me out? Thanks
Sample http://jsfiddle.net/9oLs3fyh/
var timeout;
$("#box1").hover(function () {
clearTimeout(timeout);
$("#box2").delay(500).show();
}, function () {
timeout = setTimeout(function(){
$("#box2").delay(500).hide();
},500);
});
if($("#box2").is(':hover')){
$("#box2").show();
}
var timeout;
$("#accountIcon").hover(function () {
clearTimeout(timeout);
$("#accountPopup").delay(500).show(500);
}, function () {
setTimeout(function(){
$("#accountPopup").delay(500).hide(500);
},500);
});
if($("#accountPopup").is(':hover')){
$("#accountPopup").show();
}
DEMO
I think this is a good way to achieve that by wrap your divs inside one div
<div class="conatiner">
<div id="accountIcon"></div>
<div id="accountPopup"></div>
</div>
.conatiner{
overflow:hidden;
}
var timeout;
$(".conatiner").hover(function () {
clearTimeout(timeout);
$("#accountPopup").delay(500).show(500);
}, function () {
setTimeout(function(){
$("#accountPopup").delay(500).hide(500);
},500);
});
DEMO HERE
and while you use hover .. you can just use css with visibility and transition delay
<div class="conatiner">
<div id="accountIcon"></div>
<div id="accountPopup"></div>
</div>
.conatiner:hover #accountPopup{
display: block;
-webkit-transition-delay: 1s; /* Safari */
transition-delay: 1s;
}
DEMO
Try assiging class hovered (check the DEMO):
CSS
.hovered { display:block }
On mouseenter of #accountIcon assign class hovered on #accountPopup element.
Then, on mouseleave, if the target element (#accountPopup) is not visisble, just remove the class.
$('#accountIcon').on('mouseenter', function() {
$('#accountPopup').addClass('hovered');
}).on('mouseleave', function() {
var target = $('#accountPopup');
if (!target.is(':visible')) {
target.removeClass('hovered');
}
});
$('#accountPopup').on('mouseleave', function() {
$(this).removeClass('hovered');
});

Hover on one of multiple div and delay, then drop down the child div

I know there is lots of similar question like this, I've been searching and tried for hours but no luck.
What I'm trying to do is easy,there is multiple block in my page like this
and the .description class will pop up when user hover on .frame element
<div class="row">
<div class="col-xs-4">
<div class="frame">
<div class="description">
<h3>Title</h3>
<p>Description</p>
</div>
</div>
</div>
<div class="col-xs-4">
<div class="frame">
<div class="description">
<h3>T2</h3>
<p>Description</p>
</div>
</div>
</div>
</div>
I think it's quite annoying when user swipe over the screen and every description gets triggered, so I tried to add a "delay condition" through setTimeout(), thus, only when user stop on one block for atleast 1sec, the description pops up
But it just don't go well and here's my
Demo
It's the best I can do, the delay function works, but it seems have two trigger on it.
jQuery code:
var timer;
$(document).ready(
function(){
$(".frame").hover(
function() {
var descrip_in=$(".description",$(this))
timer = setTimeout(function(){descrip_in.slideToggle();}, 1000);
},
function() {
var descrip_out=$(".description",$(this))
clearTimeout(timer);
descrip_out.slideToggle(1000)
}
);
}
);
Ah, it was subtle.
By using slideToggle instead of slideDown and slideUp, you were toggling the popup on exit if you left the element before the timeout went off!
var timer;
$(document).ready(
function(){
$(".frame").hover(
function() {
var descrip_in=$(".description",$(this))
timer = setTimeout(function(){descrip_in.slideDown();}, 1000);
},
function() {
var descrip_out=$(".description",$(this))
clearTimeout(timer);
descrip_out.slideUp(1000)
}
);
}
);
http://jsfiddle.net/1esrLrfs/71/
The same can also used with show and hide functions too. The slideToggle function gets called even before you reach the timeout and it wont work.
You can see the Demo
$(function() {
var timeoutId;
$(".frame").hover( showDesc, hideDesc );
});
function showDesc(){
var descrip_in=$(".description",$(this))
timer = setTimeout(function(){descrip_in.show(200);}, 1000);
}
function hideDesc(){
var descrip_out=$(".description",$(this))
clearTimeout(timer);
descrip_out.hide(300)
}
You can check in the handler out function that whether your element is displaying or not, then apply toggle function.
From your code
var timer ,isToggle;
$(document).ready(
function(){
$(".frame").hover(
function() {
var descrip_in=$(".description",$(this))
timer = setTimeout(function(){descrip_in.slideToggle();}, 1500);
},
function() {
var descrip_out=$(".description",$(this))
clearTimeout(timer);
if(descrip_out.css('display') !== 'none')
descrip_out.slideToggle(1000)
}
);
}
);

Text blinking jQuery

What is an easy way to make text blinking in jQuery and a way to stop it? Must work for IE, FF and Chrome. Thanks
A plugin to blink some text sounds a bit like overkill to me...
Try this...
$('.blink').each(function() {
var elem = $(this);
setInterval(function() {
if (elem.css('visibility') == 'hidden') {
elem.css('visibility', 'visible');
} else {
elem.css('visibility', 'hidden');
}
}, 500);
});
Try using this blink plugin
For Example
$('.blink').blink(); // default is 500ms blink interval.
//$('.blink').blink(100); // causes a 100ms blink interval.
It is also a very simple plugin, and you could probably extend it to stop the animation and start it on demand.
here's blinking with animation:
$(".blink").animate({opacity:0},200,"linear",function(){
$(this).animate({opacity:1},200);
});
just give a blink class whatever u want to blink:
<div class="someclass blink">some text</div>
all regards to DannyZB on #jquery
features:
doesn't need any plugins (but JQuery itself)
does the thing
If you'd rather not use jQuery, this can be achieved with CSS3
#-webkit-keyframes blink {
from { opacity: 1.0; }
to { opacity: 0.0; }
}
blink {
-webkit-animation-name: blink;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: cubic-bezier(1.0,0,0,1.0);
-webkit-animation-duration: 1s;
}
Seems to work in Chrome, though I thought I heard a slight sobbing noise.
Combine the codes above, I think this is a good solution.
function blink(selector){
$(selector).animate({opacity:0}, 50, "linear", function(){
$(this).delay(800);
$(this).animate({opacity:1}, 50, function(){
blink(this);
});
$(this).delay(800);
});
}
At least it works on my web.
http://140.138.168.123/2y78%202782
Here's mine ; it gives you control over the 3 parameters that matter:
the fade in speed
the fade out speed
the repeat speed
.
setInterval(function() {
$('.blink').fadeIn(300).fadeOut(500);
}, 1000);
You can also use the standard CSS way (no need for JQuery plugin, but compatible with all browsers):
// Start blinking
$(".myblink").css("text-decoration", "blink");
// Stop blinking
$(".myblink").css("text-decoration", "none");
W3C Link
This is the EASIEST way (and with the least coding):
setInterval(function() {
$( ".blink" ).fadeToggle();
}, 500);
Fiddle
Now, if you are looking for something more sophisticated...
//Blink settings
var blink = {
obj: $(".blink"),
timeout: 15000,
speed: 1000
};
//Start function
blink.fn = setInterval(function () {
blink.obj.fadeToggle(blink.speed);
}, blink.speed + 1);
//Ends blinking, after 'blink.timeout' millisecons
setTimeout(function () {
clearInterval(blink.fn);
//Ensure that the element is always visible
blink.obj.fadeIn(blink.speed);
blink = null;
}, blink.timeout);
Fiddle
You can also try these:
<div>some <span class="blink">text</span> are <span class="blink">blinking</span></div>
<button onclick="startBlink()">blink</button>
<button onclick="stopBlink()">no blink</button>
<script>
function startBlink(){
window.blinker = setInterval(function(){
if(window.blink){
$('.blink').css('color','blue');
window.blink=false;
}
else{
$('.blink').css('color','white');
window.blink = true;
}
},500);
}
function stopBlink(){
if(window.blinker) clearInterval(window.blinker);
}
</script>
$.fn.blink = function(times, duration) {
times = times || 2;
while (times--) {
this.fadeTo(duration, 0).fadeTo(duration, 1);
}
return this;
};
Here you can find a jQuery blink plugin with its quick demo.
Basic blinking (unlimited blinking, blink period ~1 sec):
$('selector').blink();
On a more advanced usage, you can override any of the settings:
$('selector').blink({
maxBlinks: 60,
blinkPeriod: 1000, // in milliseconds
onBlink: function(){},
onMaxBlinks: function(){}
});
There you can specify the max number of blinks as well as have access to a couple of callbacks: onBlink and onMaxBlinks that are pretty self explanatory.
Works in IE 7 & 8, Chrome, Firefox, Safari and probably in IE 6 and Opera (although haven't tested on them).
(In full disclosure: I'm am the creator of this previous one. We had the legitimate need to use it at work [I know we all like to say this :-)] for an alarm within a system and I thought of sharing only for use on a legitimate need ;-)).
Here is another list of jQuery blink plugins.
this code is work for me
$(document).ready(function () {
setInterval(function(){
$(".blink").fadeOut(function () {
$(this).fadeIn();
});
} ,100)
});
You can try the jQuery UI Pulsate effect:
http://docs.jquery.com/UI/Effects/Pulsate
Easiest way:
$(".element").fadeTo(250, 0).fadeTo(250,1).fadeTo(250,0).fadeTo(250,1);
You can repeat this as much as you want or you can use it inside a loop. the first parameter of the fadeTo() is the duration for the fade to take effect, and the second parameter is the opacity.
$(".myblink").css("text-decoration", "blink");
do not work with IE 7 & Safari. Work well with Firefox
This stand-alone solution will blink the text a specified number of times and then stop.
The blinking uses opacity, rather than show/hide, fade or toggle so that the DIV remains clickable, in case that's ever an issue (allows you to make buttons with blinking text).
jsFiddle here (contains additional comments)
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var init = 0;
$('#clignotant').click(function() {
if (init==0) {
init++;
blink(this, 800, 4);
}else{
alert('Not document.load, so process the click event');
}
});
function blink(selector, blink_speed, iterations, counter){
counter = counter | 0;
$(selector).animate({opacity:0}, 50, "linear", function(){
$(this).delay(blink_speed);
$(this).animate({opacity:1}, 50, function(){
counter++;
if (iterations == -1) {
blink(this, blink_speed, iterations, counter);
}else if (counter >= iterations) {
return false;
}else{
blink(this, blink_speed, iterations, counter);
}
});
$(this).delay(blink_speed);
});
}
//This line must come *AFTER* the $('#clignotant').click() function !!
window.load($('#clignotant').trigger('click'));
}); //END $(document).ready()
</script>
</head>
<body>
<div id="clignotant" style="background-color:#FF6666;width:500px;
height:100px;text-align:center;">
<br>
Usage: blink(selector, blink_speed, iterations) <br />
<span style="font-weight:bold;color:blue;">if iterations == -1 blink forever</span><br />
Note: fn call intentionally missing 4th param
</div>
</body>
</html>
Sources:
Danny Gimenez
Moses Christian
Link to author
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<div id="msg"> <strong><font color="red">Awesome Gallery By Anil Labs</font></strong></p> </div>
<script type="text/javascript" >
function blink(selector){
$(selector).fadeOut('slow', function(){
$(this).fadeIn('slow', function(){
blink(this);
});
});
}
blink('#msg');
</script>
I was going to post the steps-timed polyfill, but then I remembered that I really don’t want to ever see this effect, so…
function blink(element, interval) {
var visible = true;
setInterval(function() {
visible = !visible;
element.style.visibility = visible ? "visible" : "hidden";
}, interval || 1000);
}
I feel the following is of greater clarity and customization than other answers.
var element_to_blink=$('#id_of_element_to_blink');
var min_opacity=0.2;
var max_opacity=1.0;
var blink_duration=2000;
var blink_quantity=10;
var current_blink_number=0;
while(current_blink_number<blink_quantity){
element_to_blink.animate({opacity:min_opacity},(blink_duration/2),"linear");
element_to_blink.animate({opacity:max_opacity},(blink_duration/2),"linear");
current_blink_number+=1;
}
This code will effectively make the element(s) blink without touching the layout (like fadeIn().fadeOut() will do) by just acting on the opacity ; There you go, blinking text ; usable for both good and evil :)
setInterval(function() {
$('.blink').animate({ opacity: 1 }, 400).animate({ opacity: 0 }, 600);
}, 800);
Blinking !
var counter = 5; // Blinking the link 5 times
var $help = $('div.help');
var blinkHelp = function() {
($help.is(':visible') ? $help.fadeOut(250) : $help.fadeIn(250));
counter--;
if (counter >= 0) setTimeout(blinkHelp, 500);
};
blinkHelp();
This code might help to this topic. Simple, yet useful.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
setInterval("$('#myID/.myClass').toggle();",500);
});
</script>
I like alex's answer, so this is a bit of an extension of that without an interval (since you would need to clear that interval eventually and know when you want a button to stop blinking. This is a solution where you pass in the jquery element, the ms you want for the blinking offset and the number of times you want the element to blink:
function blink ($element, ms, times) {
for (var i = 0; i < times; i++) {
window.setTimeout(function () {
if ($element.is(':visible')) {
$element.hide();
} else {
$element.show();
}
}, ms * (times + 1));
}
}
Some of these answers are quite complicated, this is a bit easier:
$.fn.blink = function(time) {
var time = typeof time == 'undefined' ? 200 : time;
this.hide(0).delay(time).show(0);
}
$('#msg').blink();
Seeing the number of views on this question, and the lack of answers that cover both blinking and stopping it, here goes: try jQuery.blinker out (demo).
HTML:
<p>Hello there!</p>
JavaScript:
var p = $("p");
p.blinker();
p.bind({
// pause blinking on mouseenter
mouseenter: function(){
$(this).data("blinker").pause();
},
// resume blinking on mouseleave
mouseleave: function(){
$(this).data("blinker").blinkagain();
}
});
Indeed a plugin for a simple blink effect is overkill. So after experimenting with various solutions, I have choosen between one line of javascript and a CSS class that controls exactly how I want to blink the elements (in my case for the blink to work I only need to change the background to transparent, so that the text is still visible):
JS:
$(document).ready(function () {
setInterval(function () { $(".blink").toggleClass("no-bg"); }, 1000);
});
CSS:
span.no-bg {
background-color: transparent;
}
Full example at this js fiddle.
Blink functionality can be implemented by plain javascript, no requirement for jquery plugin or even jquery.
This will work in all the browsers, as it is using the basic functionality
Here is the code
HTML:
<p id="blinkThis">This will blink</p>
JS Code:
var ele = document.getElementById('blinkThis');
setInterval(function () {
ele.style.display = (ele.style.display == 'block' ? 'none' : 'block');
}, 500);
and a working fiddle
This is what ended up working best for me. I used jQuery fadeTo because this is on WordPress, which already links jQuery in. Otherwise, I probably would have opted for something with pure JavaScript before adding another http request for a plugin.
$(document).ready(function() {
// One "blink" takes 1.5s
setInterval(function(){
// Immediately fade to opacity: 0 in 0ms
$(".cursor").fadeTo( 0, 0);
// Wait .75sec then fade to opacity: 1 in 0ms
setTimeout(function(){
$(".cursor").fadeTo( 0, 1);
}, 750);
}, 1500);
});
I have written a simple jquery extension for text blink whilst specifying number of times it should blink the text, Hope it helps others.
//add Blink function to jquery
jQuery.fn.extend({
Blink: function (i) {
var c = i; if (i===-1 || c-- > 0) $(this).fadeTo("slow", 0.1, function () { $(this).fadeTo("slow", 1, function () { $(this).Blink(c); }); });
}
});
//Use it like this
$(".mytext").Blink(2); //Where 2 denotes number of time it should blink.
//For continuous blink use -1
$(".mytext").Blink(-1);
Text Blinking start and stop on button click -
<input type="button" id="btnclick" value="click" />
var intervalA;
var intervalB;
$(document).ready(function () {
$('#btnclick').click(function () {
blinkFont();
setTimeout(function () {
clearInterval(intervalA);
clearInterval(intervalB);
}, 5000);
});
});
function blinkFont() {
document.getElementById("blink").style.color = "red"
document.getElementById("blink").style.background = "black"
intervalA = setTimeout("blinkFont()", 500);
}
function setblinkFont() {
document.getElementById("blink").style.color = "black"
document.getElementById("blink").style.background = "red"
intervalB = setTimeout("blinkFont()", 500);
}
</script>
<div id="blink" class="live-chat">
<span>This is blinking text and background</span>
</div>

Categories