使用动画最小化多个Div

时间:2017-02-07 22:17:51

标签: javascript jquery css3 jquery-animate css-animations

我试图在我的div .panels的width属性上添加动画。我已经在css中使用 transition-property, .animate()进行了尝试,但它不起作用。 我注意到calc()不能用于jQuery动画...

第三个面板应该总是减去另外两个宽度,因此它可以浮动在右边。

ex. width: 'calc(100% - ' + (panelWidth1 + panelWidth2) + 'px)'

那么有什么建议让它起作用吗? 谢谢!

HMTL

<div class="actions">
  <button id="MinPanel1">Minimize Panel 1</button>
  <button id="MinPanel2">Minimize Panel 2</button>
</div>

<div class="panels">
  <div class="panel1"></div>
  <div class="panel2"></div>
  <div class="panel3"></div>
</div>

JS

$( document ).ready(function() {

    $('#MinPanel1').click(function() {
        var toggleWidth = $(".panel1").width() == 225 ? "57px" : "225px";
        $('.panel1').css({
            width: toggleWidth
        });
        adjustPanel();
    });

    $('#MinPanel2').click(function() {
        var toggleWidth = $(".panel2").width() == 330 ? "66px" : "330px";
        $('.panel2').css({
            width: toggleWidth
        });
        adjustPanel();
    });


    adjustPanel = function() {
        var panelWidth1 = $('.panel1').width();
        var panelWidth2 = $('.panel2').width();
        var panelWidthTotal = panelWidth1 + panelWidth2;

        $('.panel3').css({
            width: '-webkit-calc(100% - ' + panelWidthTotal + 'px)',
            width: '-moz-calc(100% - ' + panelWidthTotal + 'px)',
            width: 'calc(100% - ' + panelWidthTotal + 'px)'
        });
    }

});

CSS

[...]

.panel1, .panel2, .panel3{
    height: 100%;
    float: left;
    /*-moz-transition-property: width;
    -o-transition-property: width;
    -webkit-transition-property: width;
    transition-property: width;
    -moz-transition-duration: 300ms;
    -o-transition-duration: 300ms;
    -webkit-transition-duration: 300ms;
    transition-duration: 300ms;*/
}

[...]

DEMO:JS FIDDLE

已编辑的演示结果:JS FIDDLE

2 个答案:

答案 0 :(得分:1)

试试这个:

$( document ).ready(function() {

    $('#MinPanel1').click(function() {
        var toggleWidth = $(".panel1").width() == 225 ? "57px" : "225px";
        $('.panel1').animate({
            width: toggleWidth
        },function(){
            adjustPanel($('.panel1').width(), $('.panel2').width())
        });
    });

    $('#MinPanel2').click(function() {
        var toggleWidth = $(".panel2").width() == 330 ? "66px" : "330px";
        $('.panel2').animate({
            width: toggleWidth
        },function(){
            adjustPanel($('.panel1').width(), $('.panel2').width())
        });
    });


    adjustPanel = function(pan1, pan2) {
    console.log($('.panels').width(), pan1, pan2);
        var panelWidthTotal = $('.panels').width() - (pan1 + pan2) + 'px';

        $('.panel3').animate({
            width: panelWidthTotal
        });
    }

});

答案 1 :(得分:-1)

这是工作代码。它适用于css转换,并且只是重写了js因为代码需要div的最终宽度而不是动画持续时它会抓取的那个。

$( document ).ready(function() {
    
    $('#MinPanel1').click(function() {
        var toggleWidth = $(".panel1").width() == 225 ? "57px" : "225px";
        var toggleWidth1=toggleWidth;
        $('.panel1').css({
            width: toggleWidth
        });
        adjustPanel();
    });
    
    $('#MinPanel2').click(function() {
        var toggleWidth = $(".panel2").width() == 330 ? "66px" : "330px";
        var toggleWidth2=toggleWidth;
        $('.panel2').css({
            width: toggleWidth
        });
        adjustPanel();
    });
    
    
    adjustPanel = function() {
        var panelWidth1 = toggleWidth1;
        var panelWidth2 = toggleWidth2;
        var panelWidthTotal = panelWidth1 + panelWidth2;
        
        $('.panel3').css({
            width: '-webkit-calc(100% - ' + panelWidthTotal + 'px)',
            width: '-moz-calc(100% - ' + panelWidthTotal + 'px)',
            width: 'calc(100% - ' + panelWidthTotal + 'px)'
        });
    }
    
});
*{
    box-sizing: border-box;
}
html, body{
    width: 100%;
    height: 100%;
}
.actions{
    position: absolute;
    top: 0;
    left: 0;
    padding: 10px;
    width: 100%;
    background: white;
}

.actions button{
    margin-right: 10px;
}

.panels{
    height: 100%;
}
.panel1, .panel2, .panel3{
    height: 100%;
    float: left;
    /*-moz-transition-property: width;
    -o-transition-property: width;
    -webkit-transition-property: width;
    transition-property: width;
    -moz-transition-duration: 300ms;
    -o-transition-duration: 300ms;
    -webkit-transition-duration: 300ms;
    transition-duration: 300ms;*/
}

.panel1{
    width: 225px;
    background-color: blue;
    -webkit-transition: width 2s; /* Safari */
    transition: width 2s;

}

.panel2{
    width: 330px;
    background-color: red;
    -webkit-transition: width 2s; /* Safari */
    transition: width 2s;
}

.panel3{
    width: calc(100% - 555px);
    background-color: darkgreen;
    -webkit-transition: width 2s; /* Safari */
    transition: width 2s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="actions">
  <button id="MinPanel1">Minimize Panel 1</button>
  <button id="MinPanel2">Minimize Panel 2</button>
</div>

<div class="panels">
  <div class="panel1"></div>
  <div class="panel2"></div>
  <div class="panel3"></div>
</div>