简体   繁体   English

如何使用 javascript、jquery 或 css 为模态调整大小设置动画?

[英]How do I animate a modal resize with javascript, jquery, or css?

I'm using bootstrap to display a modal dialog and jquery to dynamically show/hide some elements of the modal.我正在使用引导程序来显示模态对话框和 jquery 来动态显示/隐藏模态的某些元素。 The problem I have is that when I show or hide an item the modal immediately resizes.我遇到的问题是,当我显示或隐藏一个项目时,模态会立即调整大小。 What I'd like to do is have it transition to the new size either with a jquery or css animation.我想做的是使用 jquery 或 css 动画将其转换为新的大小。

I tried to accomplish this with css with:我试图用 css 来完成这个:

.modal {
    flex-grow: 1;
    -webkit-transition: all 300ms ease-in-out;
    -moz-transition: all 300ms ease-in-out;
    -o-transition: all 300ms ease-in-out;
    transition: all 300ms ease-in-out;
}

But that didn't work.但这没有用。

Here is an example modal I'm using:这是我正在使用的示例模态:

<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
           <p>A paragraph</p>
        </div>
    </div>
</div>

Thanks for your help with this-感谢您对此的帮助-

Apparently animating an element when content is added to it with css or javascript is almost impossible.显然,当使用 css 或 javascript 将内容添加到元素时为元素设置动画几乎是不可能的。 I still didn't find a way to do it with css only.我仍然没有找到仅使用 css 的方法。 It has to do with the browser immediately rendering the content and updating the auto height element.它与浏览器立即呈现内容并更新自动高度元素有关。 This apparently does not trigger any of the css transitions.这显然不会触发任何 css 转换。 To make this worse it's the same when using javascript.更糟糕的是,使用 javascript 时也是如此。 The technique used here is from this answer and is a jQuery solution: https://stackoverflow.com/a/1520352/1819684这里使用的技术来自这个答案,是一个 jQuery 解决方案: https : //stackoverflow.com/a/1520352/1819684

I think this is what you're looking for.我想这就是你要找的。

 $('#add-content').on('click', function() { $('<p>Moar Stuff</p>').css({display: 'none'}).appendTo(('.modal-body')).show('slow'); });
 .modal-body { overflow:hidden; transition:transform 19s ease-out; // note that we're transitioning transform, not height! height:auto; transform:scaleY(1); // implicit, but good to specify explicitly transform-origin:top; // keep the top of the element in the same place. this is optional. }
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong"> Launch demo modal </button> <!-- Modal --> <div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <p id="content">...</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary" id="add-content">Add Content</button> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM