首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示动画不是动画

显示动画不是动画
EN

Stack Overflow用户
提问于 2015-12-23 19:41:59
回答 3查看 64关注 0票数 0

我正在处理一个使用AJAX调用隐藏、显示和替换元素的函数。一切运作完美,它动画关闭,但由于某些原因,不会重新激活,当它打开,它只是出现。这是一个相当长的脚本,所以这是很重要的东西:

代码语言:javascript
复制
 // This is the first part which is executed.
 var bar = $("#bar");

 // This is what happens when the bar is closes and works.

 if (!supressed) {
     bar.animate({height:0});
     bar.parent().animate({height:0}, function() {
         $(this).hide()
     });
 } else {
     // The bar is replaced with an identical div with the same id.
     bar.replaceWith(newBar);
     if (supressed) {
         // When the bar needs to appear again it runs the updateBar function.
        updateBar(true);
     }
}

然后是updateBar函数。#栏div只是出现,但不会动画进入视图。我尝试过改变动画调用的顺序,但这两种方法似乎都没有效果。

代码语言:javascript
复制
function updateBar(reload) {
    // Reassigns the variable
    var bar = $("#bar");

    if (reload === true) {

        // This is where things go wrong.
        bar.animate({height:"45px"});
        bar.parent().animate({height:"45px"}, function() {
            $(this).show();
        });
    }
}

JSFiddle:https://jsfiddle.net/1p80peqq/3/

有什么建议吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-23 23:46:07

您的原始脚本的问题,根据您的小提琴是,您是动画的高度的父母,而它是隐藏的。然后在动画完成后显示出来。因此高度动画是不可见的。您可以在显示父级之前插入console.log($(this).css('display'));来判断这一点。控制台将显示none

我不知道你为什么还想给父母动画。如果只想隐藏/显示条形图,可以这样做:

代码语言:javascript
复制
function reloadBar() {
    var $bar = $("#bar");
    var $newBar = $("#new-bar");
    $bar.replaceWith($newBar);
    if ($("#click").hasClass("clicked")) {
        $newBar.animate({ height: 0 })
    } else {
        $newBar.animate({ height: '45px' });
    }
}

或者,您可以更改您的reload()函数,以便在绘制高度之前首先显示父函数,如下所示:

代码语言:javascript
复制
function reload(option) {
    var $bar = $("#new-bar");
    if (option === true) {
        $bar.animate({
            height: "45px"
        });
        $bar.parent().show().animate({
            height: "45px"
        });
    }
 } 

但是上面有冗余,因为$bar的动画没有意义,因为$bar是不可见的(即$bars的父母有display: none)。

代码语言:javascript
复制
function reload(option) {
    var $bar = $("#new-bar");       
    if (option === true) {
        $bar.css({ height: "45px" });
        $bar.parent().show().animate({
            height: "45px"
        });
    }
 } 

或者,您可以简单地动画酒吧的高度,离开父母的原样,就像我之前做的。

票数 1
EN

Stack Overflow用户

发布于 2015-12-24 05:57:07

在bar的父节点上调用$(this).hide()将其display属性设置为none。因此,当您调用updateBar()使条形条及其父块恢复原样时,父控件将一直隐藏到动画完成后,然后调用$(this).show()

所以在updateBar()中,只需在启动动画之前调用bar.parent().show()即可。

更新小提琴:https://jsfiddle.net/1p80peqq/6/

票数 1
EN

Stack Overflow用户

发布于 2015-12-23 23:05:03

我能够通过在条形图设置显示之前隐藏它的父级来解决这个问题。就像这样:

代码语言:javascript
复制
bar.animate({height:"45px"});
bar.parent().animate({height:"45px"}, function() {
    $(this).hide.show("slow");
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34442631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档