简体   繁体   English

如何防止css动画在页面加载时运行

[英]how to prevent css animation from running on page load

I have a container div set with overflow:hidden that swaps between two cards, with only one visible at a time: 我有一个带overflow:hidden的容器div overflow:hidden在两张卡之间交换,一次只能看到一个:

[card 1 | card 2]

I have jquery set up so that, when a user clicks on a button, it toggles a class, .slid , on the div , with stylus styling that changes its margin-left parameter to -400 or so. 我设置了jquery,以便当用户点击一个按钮时,它会在div上切换一个类, .slid ,使用手写笔样式将其margin-left参数更改为-400左右。

     .container
            &.slid {
                margin-left: -400px;

This all works; 这一切都有效; whenever you press the button, the cards flip back and forth. 无论何时按下按钮,卡片都会来回翻转。

I wanted to write an animation so that the cards will slide back and forth instead, so I changed my stylus code to 我想写一个动画,以便卡片来回滑动,所以我将手写笔代码更改为

       .container{
            animation: slideout 1s;
            &.slid {
                margin-left: -400px;
                animation: slidein 1s;
            }

with corresponding animations: 相应的动画:

@keyframes slidein {
    from {
        margin-left: 0px;
    }

    to {
        margin-left: -400px
    }

}

@keyframes slideout {
    from {
        margin-left: -400px;
    }

    to {
        margin-left: 0px;
    }

}

This also works more or less as intended, but I have one problem: since the stylus code is set to run the animation on load, a page refresh will run the slideout animation on page load. 这也或多或少按预期工作,但我有一个问题:由于手写笔代码设置为在加载时运行动画,页面刷新将在页面加载时运行slideout动画。 Is there a way around this easily that still preserves the animations sliding back and forth? 有没有办法轻松地保持动画来回滑动?

In my opinion I think that instead of applying the slideout animation to the container and toggling the application of a single .slid class which defines the slidein animation, you should do as following: 在我看来,我认为不是将滑动动画应用于容器并切换定义幻灯片动画的单个.slid类的应用程序,您应该执行以下操作:

1- Do not apply the animation directly to the container class. 1-不要将动画直接应用于容器类。

2- Define two classes .slid-in (your current .slid) and .slid-out that define the slidein and slideout animations respectively. 2-定义两个类.slid-in(当前的.slid)和.slid-out,分别定义slidein和slideout动画。 Something like: 就像是:

.container{
        &.slid-in {
            margin-left: -400px;
            animation: slidein 1s;
        }
        &.slid-out {
            margin-left: 0px;
            animation: slideout 1s;
        }
}

3- Update your code in order to apply the .slid-in class the first time the button is pressed, and toggling between .slid-in and .slid-out afterwards. 3-更新代码,以便在第一次按下按钮时应用.slid-in类,然后在.slid-in和.slid-out之间切换。

By doing it this way, you would prevent the slideout animation to be applied on page load, since the .container class is applied right away as deduced from your explanation. 通过这种方式,您可以阻止在页面加载时应用滑出动画,因为.container类会立即应用,从您的解释中推断出来。

You can influence the loading order of the CSS by just placing it at the bottom of the page. 您只需将其放在页面底部即可影响CSS的加载顺序。 That will make sure the html is loaded before the CSS is applied. 这将确保在应用CSS之前加载html。 Ofcourse if you are bringing things in with javascript or AJAX this won't be a bulletproof solution. 当然,如果你用javascript或AJAX引入东西,这将不是一个防弹解决方案。 It should be ok if all the elements are in the html itself. 如果所有元素都在html本身中,那应该没问题。

I hope that helps! 我希望有所帮助!

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

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