Despite all the posts on SO re blinking text, I didn't find a perfect solution. Most blink the text by hiding it - which means the user cannot reliably click on it (during the hidden cycle, it is not clickable).
I've scraped bits and pieces from other examples to create the beginnings of a solution, but I'm having difficulty with the parameters.
DESIRED:
$('#selector').click(function() {
blink(this, speed, iterations); //iterations==0 for unlimited iterations
});
PROBLEM:
- Blinking does not stop after desired number of iterations (or
0
for blink forever).
Note that I do not wish to use .hide
, .toggle
, .fade
or any other jQ method that renders the div unclickable during the "invisible" period of the blinking cycle.
Here's the code I have so far: jsFiddle
<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 pad = 0;
$('#clignotant').click(function() {
if (pad==0) {
pad++;
blink(this, 800, 15);
}else{
alert('Not document.load, so now send user to the streaming video');
}
});
function blink(selector, blink_speed, iterations){
counter = 0;
$(selector).animate({opacity:0}, 50, "linear", function(){
$(this).delay(blink_speed);
$(this).animate({opacity:1}, 50, function(){
if (iterations == 0) {
blink(this, blink_speed, iterations);
}else if (counter>iterations) {
return false;
}else{
counter++;
blink(this, blink_speed, iterations);
}
});
$(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;line-height:100px;text-align:center;">
This box, and the text in it, are blinking
</div>
</body>
</html>
Sources:
Moses Christian's answer in this SO post
emgee's jsFiddle