hide()vs hide("slow")

And*_*rdi 6 html javascript jquery

我需要隐藏一个div,使用此代码它可以正常工作:

    var idObj = $(this).attr('key');
var valH = $(this).attr('hideval');
var valS = $(this).attr('showval');

if ($('div[name='+idObj+']').attr('isdisplay') == 'no') {
    $('div[name='+idObj+']').children().show("slow");
    $('div[name='+idObj+']').attr('isdisplay','yes');
    var divTitle = $('div[name='+idObj+']').children().first();
    var divArrow = $(this).children().first();
    //.attr('src',prefixImg+valH);

    //divTitle.show();
    //divArrow.show();
    $(this).children().first().attr('src',prefixImg+valH);
} else {

    var divTitle = $('div[name='+idObj+']').children().first();
    var divArrow = $('div[name='+idObj+']').children().last();
    //.attr('src',prefixImg+valS);

    $('div[name='+idObj+']').children().hide();
    $('div[name='+idObj+']').attr('isdisplay','no');

    divTitle.show();
    divArrow.show();
    $(this).children().first().attr('src',prefixImg+valS);
}
Run Code Online (Sandbox Code Playgroud)

隐藏了我的div,并显示了重新打开div的标题和箭头.但是如果我尝试使用hide("slow"),当div关闭时divTitle和divArrow就不会出现.使用hide(1000)的相同问题.

隐藏有没有"慢"参数之间有区别吗?

谢谢,安德烈

Kam*_*ami 7

来自官方网站

匹配的元素将立即隐藏,没有动画.这大致相当于调用.css('display','none'),除了display属性的值保存在jQuery的数据高速缓存中,以便稍后可以将显示恢复到其初始值.如果元素的显示值为内联,则隐藏并显示,它将再次以内联方式显示.

提供持续时间时,.hide()将成为动画方法..hide()方法同时动画匹配元素的宽度,高度和不透明度.当这些属性达到0时,显示样式属性设置为none,以确保该元素不再影响页面的布局.

因此,如果使用hide而没有延迟,它会立即隐藏而无需动画 - 例如,poof.

如果它与时间一起使用,它会变得生动,因此随着时间的推移它会消失.

对于你的问题,没有相应的html代码很难判断.


Eva*_*ahn 4

$(element).hide()立即隐藏一个元素,并以$(element).hide('slow')动画方式使其消失(缓慢)。

看起来(虽然我不确定)你想在动画完成后做一些事情在这种情况下,请执行以下操作:

var that = this;  // here to preserve scope for the block below
$('div[name='+idObj+']').children().hide('slow', function() {

    // This stuff happens after the hide animation is done.
    $('div[name='+idObj+']').attr('isdisplay','no');

    divTitle.show();
    divArrow.show();
    $(that).children().first().attr('src',prefixImg+valS);  // <= note "that" instead of "this"

});
Run Code Online (Sandbox Code Playgroud)