Reputation: 11468
I am in a need of having a blinking function in my application. Basically, I am creating a calculator and I need something like "|" to blink after the user inputs each numbers from the button. For firefox and opera, something like:
var str = "Hello world!";
document.write(str.blink());
This is exactly what I want. My problem is they don't work in Chrome, IE or safari. Is there any easy substitution for this or I have to create blinking functionality myself if I want my application to run on cross-browsers?
Upvotes: 2
Views: 1995
Reputation: 17757
Try this-:SOLUTION NO 1
You can create your own custom blink function.This can also be done by creating css class and adding them to our element.
function blink()
{
setInterval(function () {
document.getElementById("blink").style.webkitTransitionDuration = "0.7s";
document.getElementById("blink").style.opacity = 0;
setTimeout(function () {
document.getElementById("blink").style.webkitTransitionDuration = "0.7s";
document.getElementById("blink").style.opacity = 1;
}, 700);
},1400);
}
OR
try this-->SOLUTION NO.2
JAVASCRIPT
function blink()
{
document.getElementById('blink').className = "animated blink_css";
}
In css
.animated {
-webkit-animation-fill-mode:both;
-moz-animation-fill-mode:both;
-ms-animation-fill-mode:both;
-o-animation-fill-mode:both;
animation-fill-mode:both;
-webkit-animation-duration:1s;
-moz-animation-duration:1s;
-ms-animation-duration:1s;
-o-animation-duration:1s;
animation-duration:1s;
}
@keyframes 'blink' {
0% { background: rgba(255,0,0,0.5); }
50% { background: rgba(255,0,0,0); }
100% { background: rgba(255,0,0,0.5);
}
//try moz for mozilla,o for opera and webkit for safari and chrome
.blink_css {
-webkit-animation-name: blink;
-moz-animation-name: blink;
-o-animation-name: blink;
animation-name: blink;
}
Both of them will work.Tested!!!
Upvotes: 1
Reputation: 195
If you truly want cross-browser compatibility, you should use a library, jQuery for instance.
Here is a nice little script that accomplishes what you want- http://www.silverphp.com/simple-jquery-blink-script-alternative-to-html-blink-tag.html
On the other hand, if you prefer vanilla JS, you may have to implement it yourself. Maybe use setInterval()
with a function that makes the element disappear?
Upvotes: 1
Reputation: 1721
you could use just css3 :
@keyframes 'blink' {
0% { background: rgba(255,0,0,0.5); }
50% { background: rgba(255,0,0,0); }
100% { background: rgba(255,0,0,0.5); }
}
Upvotes: 4