JQuery Hide function: why speed: 0 still trying to revive?

So, I just tried debugging the following error:

<script>
$(function() {
    div = $('<div />');
    div.text('test');
    div.hide(0);
    div.appendto('body');
});
</script>

When I do this, the DIV is displayed. I want to hide (before we add it to the DOM). The following code:

<script>
$(function() {
    div = $('<div />');
    div.text('test');
    div.hide();
    div.appendto('body');
});
</script>

makes hides div.

When I go to jQuery source code for the hide function, I see the following:

    hide: function( speed, easing, callback ) {
    if ( speed || speed === 0 ) {
        return this.animate( genFx("hide", 3), speed, easing, callback);

    } else {
        for ( var i = 0, j = this.length; i < j; i++ ) {
            var display = jQuery.css( this[i], "display" );

            if ( display !== "none" ) {
                jQuery.data( this[i], "olddisplay", display );
            }
        }

        // Set the display of the elements in a second loop
        // to avoid the constant reflow
        for ( i = 0; i < j; i++ ) {
            this[i].style.display = "none";
        }

        return this;
    }
},

Why does he check availability

if ( speed || speed === 0 ) {

Especially, speed === 0. I would suggest when the speed is zero. You can simply skip the animation function completely and simply add a display: none; to the element.

ps. I assume that since we are giving an element that does not exist in the dom for the animation function, it just fails. Nothing needs to be revived. There, for the animation function, the DIV is not actually hidden.

:) 10 (hide()) hide (0)) . :)

+3
1

, .hide()..., .hide(0), ... .

:

$(".myElem").delay(2000).hide(0);

:

$(".myElem").delay(2000).hide();

.... .hide() - fx .

+8

Source: https://habr.com/ru/post/1782332/


All Articles