hide()和hide("slow")的区别

6

我需要隐藏一个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);
}

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

使用带有和不带有"slow"参数的hide之间是否有区别?

谢谢, 安德烈亚

3个回答

7

来自官方网站

匹配的元素将立即隐藏,没有动画效果。这基本上相当于调用.css('display', 'none'),除了jQuery将display属性的值保存在数据缓存中,以便以后可以恢复其初始值。如果一个元素具有内联显示值,那么它被隐藏和显示后,它将再次以内联方式显示。

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

因此,如果没有延迟使用hide,它会立即隐藏而不进行动画--比如说,瞬间消失。

如果与时间一起使用,它变成了动画,因此它会随着时间的推移消失。

针对您的问题,没有相应的html代码很难进行判断。


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"

});

1
谢谢Evan!那就是问题所在,由于“slow”隐藏后,对象的范围不知何故丢失了。这解决了我的问题! - Andrea Girardi

1
根据jQuery文档,可以使用字符串'fast'和'slow'来表示200毫秒和600毫秒的持续时间。也可以提供以毫秒为单位的持续时间。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接