简体   繁体   English

在Jquery中使用slideToggle时,请按时间降低边距

[英]Lower the margin by time when using slideToggle in Jquery

I have this example 我有这个例子

 $(document).ready(function() { $("#btn").click(function() { removeSecondContainer(); }); }); function removeSecondContainer() { var container = $("#c2"); container.slideToggle(500, function() { container.remove(); }); } 
 .container { margin: 20px auto; height: 20px; width: 100px; background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btn">Remove the second container</button> <div class="container" id="c1"> </div> <div class="container" id="c2"> </div> <div class="container" id="c3"> </div> 

As you can see, the process is not smooth at all. 如您所见,这个过程根本不顺利。 When the second container gets removed, the margin of the second container gets removed too. 当第二个容器被移除时,第二个容器的边缘也会被移除。 This causes a pull from the top. 这导致从顶部拉。

How can I get this smoothly? 我怎样才能顺利完成这项工作? I thought about lowering the margin by time to 0 when removing the container. 我想在拆卸容器时将时间间隔降低到0。

You are facing a margin collapsing issue. 您正面临保证金崩溃问题。 As you may notice you don't have 40px between each container like expected but only 20px. 正如您可能已经注意到,每个容器之间没有像预期的那样40px但只有20px。

As you can read here : 你可以在这里阅读:

In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. 在CSS中,两个或多个框(可能是也可能不是兄弟)的相邻边距可以组合形成单个边距。 Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin. 据说以这种方式组合的边距会崩溃,因此产生的合并边距称为折叠边距。

So when removing the element you decrease both margin at the top and bottom to leave the maring of the first and last element. 因此,当移除元素时,您会减少顶部和底部的边距,以留下第一个最后一个元素的边距。 And when the height of the element reaches 0 and get removed, you create another margin collapsing between the remaining block and thus the jump from 40px to 20px of margin. 当元素的高度达到0并被移除时,您会在剩余的块之间创建另一个边距,从而从40px跳到20px的边距。


And idea to avoid this is to increase height and use linear-gradient to color only the part you want (and leave transparent the part previously used for the margin). 并且避免这种情况的想法是增加高度并使用linear-gradient仅为您想要的部分着色(并保持透明以前用于边距的部分)。 Like that the transition will go smoothly as there is no more margin issue. 就像那样,过渡将顺利进行,因为没有更多的保证金问题。

 $(document).ready(function() { $("#btn").click(function() { removeSecondContainer(); }); }); function removeSecondContainer() { var container = $("#c2"); container.slideToggle(500, function() { container.remove(); }); } 
 .container { margin: auto; height: 60px; width: 100px; background: linear-gradient(to bottom, transparent 33.33%, red 33.33%, red 66.67%, transparent 66.67%);/ box-sizing: border-box; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btn">Remove the second container</button> <div class="container" id="c1"> </div> <div class="container" id="c2"> </div> <div class="container" id="c3"> </div> 

Or use flex by adding another container as there is no margin collpasing with flexbox ( Margin collapsing in flexbox ): 或者通过添加另一个容器来使用flex,因为没有与flexbox相关的边距 (Flexbox中的边距折叠 ):

 $(document).ready(function() { $("#btn").click(function() { removeSecondContainer(); }); }); function removeSecondContainer() { var container = $("#c2"); container.slideToggle(500, function() { container.remove(); }); } 
 .box { display: flex; flex-direction: column; } .container { margin: 20px auto; height: 20px; width: 100px; background: red; box-sizing: border-box; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btn">Remove the second container</button> <div class="box"> <div class="container" id="c1"> </div> <div class="container" id="c2"> </div> <div class="container" id="c3"> </div> </div> 

Its due to clearing. 它由于清算。 The bottom margin is not working well with them. 底部边缘不适合他们。 Either use float or remove margin-bottom or margin-top to 0 使用float或删除margin-bottommargin-top为0

here is Example 这是例子

edit: update with remove div https://jsfiddle.net/f5zw18er/3/ 编辑:更新删除div https://jsfiddle.net/f5zw18er/3/

You could potentially use jQuery animate with a callback function. 您可以使用回调函数来使用jQuery动画。 It's slightly different because it doesn't include the slide toggle, but in your example it's only being removed, so this could work. 它略有不同,因为它不包括幻灯片切换,但在您的示例中它只被删除,所以这可以工作。

Here we're removing the margin and also hiding the element with the animation, and then finally removing the element in the callback. 这里我们删除边距并使用动画隐藏元素,然后最终删除回调中的元素。

 $(document).ready(function() { $("#btn").click(function() { removeSecondContainer(); }); }); function removeSecondContainer() { var container = $("#c2"); container.animate({ 'margin' : '-20px auto', 'opacity': 0 }, 500, function(){ container.remove(); }); } 
 .container { margin: 20px auto; height: 20px; width: 100px; background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btn">Remove the second container</button> <div class="container" id="c1"> </div> <div class="container" id="c2"> </div> <div class="container" id="c3"> </div> 

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

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