我可以将动画应用于边距吗?

Can I apply animations to margins?

我正在尝试在 CSS3 页边距中制作动画,this site 似乎说你可以,但我无法开始工作。

我实际上有 3 个动画。 1 用于初始加载时的简单初始 fadeIn,然后 2 用于单击时的 margin 动画。我也刚刚尝试 margin 而不是顶部和底部,但仍然没有任何迹象表明它在工作。

单击一个部分以查看动画切换。

$(".section").click(function() {
    $(this).toggleClass("open");
});
body{
    background: #f1f1f1;
}

.section{
    display: block;
    background: #fff;
    border-bottom: 1px solid #f1f1f1;
    animation: fadeIn .5s ease, margin-top .5s ease, margin-bottom .5s ease;
}
.section.open {
    margin: 20px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div class="wrapper">
    <div class="section">Some content</div>
    <div class="section">Some content</div>
    <div class="section">Some content</div>
    <div class="section">Some content</div>
    <div class="section">Some content</div>
    <div class="section">Some content</div>
    <div class="section">Some content</div>
</div>

这是一个 JSFiddle:http://jsfiddle.net/ybh0thp9/3/

你不需要关键帧:http://jsfiddle.net/BramVanroy/ybh0thp9/7/

transition: margin 700ms;

您需要将过渡 属性 添加到您希望设置动画的基本元素。

您还提到您想要更改不透明度,但考虑到您只有一个没有子元素的元素,我不明白这怎么可能。我的意思是:如果元素是隐藏的,你就不能点击它。

不过,您可以为整个事物添加不透明度:http://jsfiddle.net/BramVanroy/ybh0thp9/9/

甚至更漂亮,进行转换:

http://jsfiddle.net/BramVanroy/ybh0thp9/10/

.section {
    margin: 0;
    opacity: 0.7;
    transform: scale(0.85);
    transition: all 700ms;
}
.section.open {
    margin: 20px 0;
    opacity: 1;
    transform: scale(1);
}

根据评论,您希望在页面加载时淡入元素。我们可以通过添加 class init.

http://jsfiddle.net/BramVanroy/ybh0thp9/12/

$(".section").addClass("init"); // JS
.section.init {opacity: 1;} // CSS

有关键帧:http://jsfiddle.net/BramVanroy/ybh0thp9/14/

@-webkit-keyframes fadeIn { from {opacity: 0; } to { opacity: 1; } }
@-moz-keyframes fadeIn { from {opacity: 0; } to { opacity: 1; } }
@keyframes fadeIn { from {opacity: 0; } to { opacity: 1; } }

-webkit-animation: fadeIn 1.5s ease;    
-moz-animation: fadeIn 1.5s ease;
animation: fadeIn 1.5s ease;

要创建动画 CSS3 您需要:

  1. 创建具有动画属性的class;要在某些浏览器中工作,您需要添加前缀:-webkit--o--moz-.
  2. 创建动画关键帧

看例子:

.animate{
    animation: myAnimation 10s; 
    animation-direction: alternate;
    animation-play-state: running;
    animation-iteration-count: infinite;
    animation-delay: 0;
    animation-timing-function: 1;
    animation-direction: alternate;

    -webkit-animation: myAnimation 10s;
    -webkit-animation-direction: alternate;
    -webkit-animation-play-state: running;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-delay: 0;
    -webkit-animation-timing-function: 1;
    -webkit-animation-direction: alternate;

    -moz-animation: myAnimation 10s;
    -moz-animation-direction: alternate;
    -moz-animation-play-state: running;
    -moz-animation-iteration-count: infinite;
    -moz-animation-delay: 0;
    -moz-animation-timing-function: 1;
    -moz-animation-direction: alternate;

    -o-animation: myAnimation 10s;
    -o-animation-direction: alternate;
    -o-animation-play-state: running;
    -o-animation-iteration-count: infinite;
    -o-animation-delay: 0;
    -o-animation-timing-function: 1;
    -o-animation-direction: alternate;
}

    @keyframes myAnimation {
        0%      { margin-top: 0; margin-left: 50px}
        25%     { margin-top: 100px; margin-left: 50px }
        50%     { margin-top: 0; margin-left: 50px }
        75%     { margin-top: 100px; margin-left: 50px }
        100%    { margin-top: 0; margin-left: 50px }
    }
    @-webkit-keyframes myAnimation {
        0%      { margin-top: 0; margin-left: 100px}
        25%     { margin-top: 100px; margin-left: 100px }
        50%     { margin-top: 0; margin-left: 100px }
        75%     { margin-top: 100px; margin-left: 100px }
        100%    { margin-top: 0; margin-left: 100px }
    }
    @-moz-keyframes myAnimation {
        0%      { margin-top: 0; margin-left: 100px}
        25%     { margin-top: 100px; margin-left: 100px }
        50%     { margin-top: 0; margin-left: 100px }
        75%     { margin-top: 100px; margin-left: 100px }
        100%    { margin-top: 0; margin-left: 100px }
    }
    @-o-keyframes myAnimation {
        0%      { margin-top: 0; margin-left: 100px}
        25%     { margin-top: 100px; margin-left: 100px }
        50%     { margin-top: 0; margin-left: 100px }
        75%     { margin-top: 100px; margin-left: 100px }
        100%    { margin-top: 0; margin-left: 100px }
    }

如果转换仍然无效,使用转换的提示...

确保您没有像这样为不同的属性设置两个单独的转换:

transition: margin 1000ms ease-in-out;
transition: box-shadow 1000ms ease-in-out;

查看浏览器的调试工具时很明显发生了什么:

box-shadow 将按预期设置动画,但由于正常的 css 规则处理,margin 未被考虑。

正确的做法是结合规则:

transition: margin 1000ms ease-in-out, box-shadow 1000ms ease-in-out;