简体   繁体   English

将完成的 css animation 保存在 class 中

[英]Save completed css animation in class

Hi I have a css animation for a link hover, and I'd like to be able to save the completed state of the animation in a css class and add it to the link on click Hi I have a css animation for a link hover, and I'd like to be able to save the completed state of the animation in a css class and add it to the link on click


.link {
    cursor: pointer;
    position: relative;
    white-space: nowrap;

}

.link::before,
.link::after {
    position: absolute;
    width: 100%;
    height: 1px;
    background: currentColor;
    top: 100%;
    left: 0;
    pointer-events: none;
}

.link::before {
    content: '';
}


.link--elara {
    font-family: aktiv-grotesk-extended, sans-serif;
    font-size: 1.375rem;
}

.link--elara::before {
    transform-origin: 50% 100%;
    transition: clip-path 0.3s, transform 0.3s cubic-bezier(0.2, 1, 0.8, 1);
    clip-path: polygon(0% 0%, 0% 100%, 0 100%, 0 0, 100% 0, 100% 100%, 0 100%, 0 100%, 100% 100%, 100% 0%);
}

.link--elara:hover::before {
    transform: translate3d(0, 2px, 0) scale3d(1.08, 3, 1);
    clip-path: polygon(0% 0%, 0% 100%, 50% 100%, 50% 0, 50% 0, 50% 100%, 50% 100%, 0 100%, 100% 100%, 100% 0%);
}

.link--elara span {
    display: inline-block;
    transition: transform 0.3s cubic-bezier(0.2, 1, 0.8, 1);
}

.link--elara:hover span {
    transform: translate3d(0, -2px, 0);
}


I've tried other solutions such as animation-fill-mode: forwards;我尝试过其他解决方案,例如animation-fill-mode: forwards; and other solutions here but I haven't had any luck..和其他解决方案但我没有任何运气..

I'd really appreciate any help.我真的很感激任何帮助。 Thanks!谢谢!

The code below works probably the way you want, but it is not "saving the completed animation in a class".下面的代码可能按照您想要的方式工作,但它不是“将完成的 animation 保存在一个类中”。

Rather, it adds a class clicked to the button upon clicking.相反,它会在clicked时将 class 添加到按钮上。 This class has the finished tranlation that also happens on hover and takes over when the cursor exits the element.此 class 具有完成的翻译,该翻译也发生在 hover 上,并在 cursor 退出元素时接管。

 $(document).ready(function(){ $(".link--elara").click(function(e){ $(".link--elara").addClass("clicked"); }); });
 .link { cursor: pointer; position: relative; white-space: nowrap; }.link::before, .link::after { position: absolute; width: 100%; height: 1px; background: currentColor; top: 100%; left: 0; pointer-events: none; }.link::before { content: ''; }.link--elara { font-family: aktiv-grotesk-extended, sans-serif; font-size: 1.375rem; }.link--elara::before { transform-origin: 50% 100%; transition: clip-path 0.3s, transform 0.3s cubic-bezier(0.2, 1, 0.8, 1); clip-path: polygon(0% 0%, 0% 100%, 0 100%, 0 0, 100% 0, 100% 100%, 0 100%, 0 100%, 100% 100%, 100% 0%); }.link--elara:hover::before, .clicked::before { transform: translate3d(0, 2px, 0) scale3d(1.08, 3, 1); clip-path: polygon(0% 0%, 0% 100%, 50% 100%, 50% 0, 50% 0, 50% 100%, 50% 100%, 0 100%, 100% 100%, 100% 0%); }.link--elara span { display: inline-block; transition: transform 0.3s cubic-bezier(0.2, 1, 0.8, 1); }.link--elara:hover span, .clicked span { transform: translate3d(0, -2px, 0); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="link"> <div class="link--elara"> <span>Click me!</span> </div> </div>

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

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