when I set opacity to 0 in css() the following fadeIn() doesn\'t work
if I set opacity to 1 the loader
element is showed but there will of course not be
Try using fadeTo()
loader.css({
top : ($(window).height() - loader.height()) / 2+'px',
left : ($(window).width() - loader.width()) / 2+'px',
opacity : 0
})
.fadeTo(1000, 1);
The problem is that jQuery only does functions like fadeIn
if the element is not visible. jQuery internally does $(this).is(':hidden') to decide whether or not to run the animation. The hidden
filter clearly does not check to see if opacity is 0; it probably should.
The obvious solution is to set display: none
as you have, or to call hide()
before your call to fadeIn()
if you are sure the element is hidden.
The other solution is to redefine the hidden
filter to check for opacity:
jQuery.expr.filters.hidden = function(elem) {
var width = elem.offsetWidth,
height = elem.offsetHeight;
return (width === 0 && height === 0) || (!jQuery.support.reliableHiddenOffsets && (elem.style.display || jQuery.css(elem, "display")) === "none" || (elem.style.opacity || jQuery.css(elem, 'opacity')) === "0");
};
Running .is(':hidden')
will now check for opacity. See jsFiddle.
I've filed a bug report to cover this issue.
.fadein() is used to show a hidden element, I dont know if it works with a opacity set to 0. Rather then opacity: 0 try display:none
edit: looks like you figured this out before my post went through