html - CSS3 : Width and margin transition simultaneously

标签 html css

我有两个盒子。单击按钮时,左侧框应该变小,右侧框应该变大。我的目标是实现平稳过渡。当右侧框变大时,我希望包含右侧边距。

我使用CSS3过渡效果。如何实现右框宽度和边距右过渡同时正确发生?

JS fiddle : http://jsfiddle.net/bmzw80py/4/

我的代码:

HTML:

<div class="container">   
    <div class="box-left"></div>
    <div class="box-right"></div>
</div>

<button id="animate">Animate</button>

CSS:

.container {
    width: 100%;
    height: 200px;
    padding: 40px 0 0 60px;
}

.box-left {
    float: left;
    width: 60%;
    height: 100px;
    background: blue;
}

.box-left-smaller {
     -webkit-transition: width 1s ease-in-out;
    -moz-transition: width 1s ease-in-out;
    -o-transition: width 1s ease-in-out;
    transition: width 1s ease-in-out;
    width: 355px;
}

.box-right {
    float: right;
    width: 30%;
    height: 100px;
    background: orange;
}

.box-right-bigger {
     -webkit-transition: width 1s ease-in-out;
    -moz-transition: width 1s ease-in-out;
    -o-transition: width 1s ease-in-out;
    transition: width 1s ease-in-out;

      -webkit-transition: margin 1s ease-in-out;
    -moz-transition: margin 1s ease-in-out;
    -o-transition: margin 1s ease-in-out;
    transition: margin 1s ease-in-out;

    width: 62%;
    margin-right: 80px;
}

JS:

$('#animate').click(function() {
   $('.box-left').addClass('box-left-smaller'); 
   $('.box-right').addClass('box-right-bigger'); 
});

最佳答案

无需触发两种不同的转换:您可以仅通过仅应用一个类来更改左框的宽度和左边距,例如

http://jsfiddle.net/4qwrLtuw/1/


CSS (全部)

.container {
    width: 100%;
    height: 200px;
    padding: 40px 0 0 0;
}


.box-right {
    overflow: hidden;
    height: 100px;
    background: orange;
}


.box-left {
    float: left;
    width: 60%;
    margin-right: 2%;
    height: 100px;
    background: blue;
}

.box-left-smaller {
     -webkit-transition: all 1s ease-in-out;
    -moz-transition: all 1s ease-in-out;
    -o-transition: all 1s ease-in-out;
    transition: all 1s ease-in-out;
    width: 30%;
    margin-right: 80px;  
}

结果

enter image description here

关于html - CSS3 : Width and margin transition simultaneously,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33096380/

相关文章:

css - 绝对定位 block 元素使其与其兄弟元素对齐

css - 文本字段 javaFX 中的提示文本

javascript - 使用 HTML/JavaScript/CSS 沿圆形路径移动 div

css - 页眉 Div 位置固定在页面顶部,但在加载时在其顶部显示一个 div

html - 浏览器窗口外的不透明度变为实心

javascript - 使用 HTML 捕获属性时的 MIME 类型是什么?

javascript - Javascript/Jquery 中的可滚动按钮组

css - 继承元素背景的反渐变

css - 如何在将鼠标悬停在图像上时在标签中显示图像名称

python - 将 HTML 插入 Flask 的正确方法