【问题标题】:Can I apply animations to margins?我可以将动画应用于边距吗?
【发布时间】:2021-04-08 02:00:55
【问题描述】:

我正在尝试在 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/

【问题讨论】:

  • 如果您希望我们调试它们,您需要显示所有关键帧动画,但您不能只通过过渡来做到这一点 - jsfiddle.net/ybh0thp9/5
  • 您似乎混淆了动画和过渡,并且似乎在有人点击他们看不到的东西后试图淡化某些东西以使其不可见。
  • 哦,确实如此。我确实混淆了动画和过渡!

标签: html css css-animations


【解决方案1】:

您不需要为此设置关键帧: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);
}

根据评论,您希望在页面加载时淡入元素。我们可以通过添加一个类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;

【讨论】:

  • 谢谢!我用关键帧为它添加了 CSS 中的动画(优于 jquery 添加类)。它有效! jsfiddle.net/ybh0thp9/13
  • @denislexic 你的方法不起作用——也不应该——起作用。我不知道你从哪里得到那个语法,但它在纯 CSS 中不起作用。我编辑了答案,以向您展示关键帧的外观以及示例。
【解决方案2】:

如果它仍然不起作用,请使用过渡提示...

确保您没有为不同的属性设置两个单独的转换,如下所示:

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;

【讨论】:

  • 这并没有真正解决 OP,他们没有在他们的代码中使用任何 transition 属性。
  • 公平点但有点讽刺的是,接受的答案告诉 OP 切换转换并成为接受的答案;-)
  • 好吧,公平地说,公认的答案是“你不需要动画,只需使用过渡”,然后展示如何使用过渡来实现效果。 (然后它会更详细地实现来自 OP 的附加请求)这个答案假设从一开始就使用转换并且不引用 OP 的代码。作为对已接受答案的评论,或者重写以显示如何通过转换with 执行 OP 想要的操作,并附加警告不要用另一个声明覆盖一个声明,这可能更值得。
  • 我的眼睛直奔答案 - 仅从标题 - 直到你说我才注意到,所以实际的原始问题没有提到过渡。有时我的答案只是有用的东西,我认为可能会帮助将来犯同样愚蠢错误的人。有时我会被否决。有时我没有。有时我会被冒犯,有时我不会。
【解决方案3】:

要使用 CSS3 创建动画,您需要:

  1. 创建一个具有动画属性的类;要在某些浏览器中工作,您需要添加前缀:-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 }
    }

【讨论】:

  • 不带前缀的规则应始终排在最后。 W3C 规范应覆盖特定于浏览器的规则。
  • @BramVanroy 真的吗?!,我不知道:O
猜你喜欢
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
相关资源
最近更新 更多