css() and opacity.. following fadeIn() not working

前端 未结 3 1310
死守一世寂寞
死守一世寂寞 2020-12-17 21:58

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

相关标签:
3条回答
  • 2020-12-17 22:29

    Try using fadeTo()

    loader.css({
        top : ($(window).height() - loader.height()) / 2+'px',
        left : ($(window).width() - loader.width()) / 2+'px',
        opacity : 0
    })
    .fadeTo(1000, 1);
    
    0 讨论(0)
  • 2020-12-17 22:31

    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.

    0 讨论(0)
  • 2020-12-17 22:32

    .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

    0 讨论(0)
提交回复
热议问题