jQuery/ Javascript : Text fade in / out
I need to get some text to fade in and out on a page (continuously) but I am getting compatibility problems. Here is the code I am using:
function effectFadeIn(classname) {
$("#id").fadeOut(800).fadeIn(800, effectFadeOut(classname))
}
function effectFadeOut(classname) {
$("#id").fadeIn(800).fadeOut(800, effectFadeIn(classname));
}
$(document).开发者_JAVA技巧ready(function () { effectFadeIn('box');});
Any idea how to do this differently? Or get it to work using the above in I.E8?
This is surely what you want:
function effectFadeIn(classname) {
$("." + classname).fadeOut(800).fadeIn(800, function () {effectFadeOut(classname)})
}
function effectFadeOut(classname) {
$("." + classname).fadeIn(800).fadeOut(800, function () {effectFadeIn(classname)}));
}
$(document).ready(function () { effectFadeIn('box');});
To call a function with arguments, you want to put it in an anonymous function.
Although there are far more code-efficient ways of doing this. But this gets your code working.
The FadeIn and fadeOut second parameter takes a reference to the function and will not execute it directly so what you can do is to create an anonymous function and then call the function inside that.
hello
$(document).ready(function () {
effectFadeIn('box');
});
function effectFadeIn(classname) {
$("."+ classname).fadeOut(800).fadeIn(800,function(){
effectFadeOut(classname)
});
}
function effectFadeOut(classname) {
$("."+ classname).fadeIn(800).fadeOut(800,function(){
effectFadeIn(classname)
});
}
First hide all elements which you want to fade in.
jQuery(document).ready(function() {
jQuery('.box').hide();
elementFadeIn('box');
});
now the box starts as hidden, then it initialises the loop.
精彩评论