简体   繁体   English

jQuery,CSS:显示后对以下DOM元素进行动画处理:无

[英]jQuery, CSS: Animating following DOM Elements after display: none

One quick question. 一个快速的问题。
If I set a div to display: none , the following Elements in the DOM will jump up on the place where the div was. 如果我将div设置为display: none ,则DOM中的以下元素将跳到div所在的位置。
How can I animate that jump, that it just moves gently up? 我如何为跳跃动画,它只是轻轻地向上移动?

Thnaks in advance for each help! 提前感谢每个帮助!

You cannot. 你不能。 Instead of display: none; 代替显示:none; make it with height: 0; 使其高度为0; and transition: height .5s; 和过渡:高度.5s; overflow: hidden; 溢出:隐藏;

Then you will have slightly move of other divs next to others. 然后,您将在其他div旁边略微移动其他div。 :) :)

If you wanna, I can made a simple codepen. 如果您想,我可以制作一个简单的Codepen。

Use slideUp() instead of hide(); 使用slideUp()代替hide(); if you use jquery 如果您使用jQuery

$('.hidden').slideUp();

Just animate it. 只是动画。 jQuery has many ways of doing it, eg: jQuery有许多实现方法,例如:

$('#myDiv').hide(200); // 200ms

To avoid to keep an hidden element with a width that could interfere with other elements on the page, you should simply: 为了避免隐藏元素的宽度可能会干扰页面上的其他元素,您应该简单地:

$('yourElement').slideUp();

That way you will make the yourElement height animate and, finally, get it hidden actually removing it from the elements flow. 这样,您将使yourElement高度动画化,最后将其隐藏起来,实际上将其从元素流中删除。

Edit: Thanks A. Wolff for the comment. 编辑:感谢A. Wolff的评论。 Removed un-needed .hide() function after .slideUp() . 去除未所需.hide()函数后.slideUp()

Got even a better solution for you. 为您提供了更好的解决方案。 You can show and hide the blue box with every time you click on the pink box. 每次单击粉红色框时,可以显示和隐藏蓝色框。

 $('.pinkBox').click(function() { $('.pinkBox').addClass('show'); $('.blueBox').addClass('displayed'); }); $('.blueBox').click(function() { $('.pinkBox').removeClass('show'); setTimeout(function() { $('.blueBox').removeClass('displayed'); }, 300); }); 
 .blueBox { height: 100px; width: 100px; background-color: blue; transition: all .3s ease; position: absolute; display: none; } .pinkBox { height: 100px; width: 100px; background-color: pink; transition: all .3s ease; position: absolute; } .show { transform: translateY(100px); } .displayed { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='blueBox'> </div> <div class='pinkBox'> </div> 

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

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