简体   繁体   English

当嵌套的div滑出时,使div容器的高度变小

[英]Make height of div container smaller when nested divs slide out

I'm trying to make the height div.container smaller whenever the divs inside of the div.container slide out on click, but the height of the div continues to stay the same height. 每当尝试单击时div.container内部的div滑出时,我都试图使div.container的高度变小,但div的高度仍保持相同的高度。

I also want the div that hasn't been clicked yet to slide into place of the div that has been clicked. 我还希望尚未单击的div滑入已单击的div的位置。

Here's my JSFiddle: https://jsfiddle.net/ispykenny/soysd2zu/ Any help would be very helpful! 这是我的JSFiddle: https ://jsfiddle.net/ispykenny/soysd2zu/任何帮助都将非常有帮助! Thanks! 谢谢! and here's the code. 这是代码。

<div class="container">

<div class="two"><p>slide over on click</p></div>
<div class="three"><p>slide over on click</p></div>
<div class="two"><p>slide over on click</p></div>
<div class="three"><p>slide over on click</p></div>
<div class="two"><p>slide over on click</p></div>
<div class="three"><p>slide over on click</p></div>
<div class="two"><p>slide over on click</p></div>
<div class="three"><p>slide over on click</p></div>
<div class="two"><p>slide over on click</p></div>
<div class="three"><p>slide over on click</p></div>

.container{
    background-color:green;
}
.two{
    width:300px;
    margin-left:auto;
    margin-right:auto;
    cursor:pointer;
    background:#666;
    text-align:center;
    padding:10px;
    font-size:10px;
    border:1px solid black;
    position:relative;

}
.three{
    width:300px;
    margin-left:auto;
    margin-right:auto;
    cursor:pointer;
    background:#666;
    text-align:center;
    padding:10px;
    font-size:10px;
    border:1px solid black;
    position:relative;


}

$(document).ready(function(){
    $('.two').click(function(){
        $(this).animate({right:"1500px"},'5000');
    });
       $('.three').click(function(){
        $(this).animate({left:"1500px"},'5000');
    });
});  

Seemed like you need to make a style 'display' of your clicked div to set 'none' after animation is completed. 好像您需要在动画完成后对单击的div进行样式“显示”以将其设置为“无”。 But in this way there will not be any animation of vanishing in that empty field. 但是通过这种方式,在那个空旷的领域不会有任何消失的动画。

$(document).ready(function(){
$('.two').click(function(){
    $(this).animate({right:"1500px"},'5000', function() {
        $(this).css("display", "none")
    });
   $('.three').click(function(){
    $(this).animate({left:"1500px"},'5000', function() {
        $(this).css("display", "none")
    });
});  

Use slideUp to move the remaining blocks smoothly. 使用slideUp平滑移动其余块。

$(document).ready(function () {
    $('.two').click(function () {
        $(this).animate({
            right: "1500px"
        }, '5000', function() { $(this).slideUp(); });
    });
    $('.three').click(function () {
        $(this).animate({
            left: "1500px"
        }, '5000', function() { $(this).slideUp(); });
    });
});

Updated JSFiddle 更新了JSFiddle

https://jsfiddle.net/mblenton/soysd2zu/3/ https://jsfiddle.net/mblenton/soysd2zu/3/

$(document).ready(function () {
    $( '.two' ).click(function() {
      $( this).animate({
         right: "1500px"
      }, 1000, function() {
        $(this).remove();
      });
    });
    $( '.three' ).click(function() {
      $( this).animate({
         left: "1500px"
      }, 1000, function() {
        $(this).remove();
      });
    });    
});

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

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