简体   繁体   English

使用动画最小化多个Div

[英]Minimize multiple Div with animation

I'm trying to add an animation on the width property for my div .panels. 我正在尝试在我的div .panels的width属性上添加动画。 Ive tried it in css with transition-property, and with .animate() , but it doesnt work. 我已经在css中使用transition-property.animate()进行了尝试 ,但它不起作用。 and I noticed that calc() is not working with jQuery animate... 我注意到calc()不能用于jQuery动画...

The third panel should always have the subtraction of the other two width, so it can float on the right. 第三个面板应该总是减去另外两个宽度,因此它可以浮动在右边。

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

So any suggestion to make it work ? 那么有什么建议让它起作用吗? Thanks! 谢谢!

HMTL 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 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 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

EDITED DEMO RESULT: JS FIDDLE 编辑演示结果: JS FIDDLE

try this : 尝试这个 :

$( 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
        });
    }

});

Here is the working code. 这是工作代码。 It works with css transition, and just has the js a bit rewritten because the code needs the final width of the divs and not the one it would grab while the animation lasts. 它适用于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> 

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

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