简体   繁体   English

如何在悬停时为文本上的下划线设置动画并保持鼠标移出该行

[英]How to animate underline on text on hover AND keep the line on mouse out

I've seen a lot of question and tutorials on how to animate a line, but they don't seem to fix my issue.我看过很多关于如何为线条设置动画的问题和教程,但它们似乎并没有解决我的问题。 I have a text with an already underline, so I want the user to hover under on the div that contains the text and have the line animate left to right but keep the line afterwards.我有一个已经有下划线的文本,所以我希望用户将鼠标悬停在包含文本的 div 上,并使该行从左到右动画,但之后保留该行。

This is the code that I have at the moment but its very glitch and the border bottom and :after dont work well together because they are at different height这是我目前拥有的代码,但它的小故障和边框底部和 :after 不能很好地协同工作,因为它们处于不同的高度

.cta-text{
    color: #2980FF;
    border-bottom: 3px solid #2980FF;
    transition: width .2s;
    
    &:hover{
        border-bottom: none;
    }

    &::after {
        content: '';
        width: 0;
        height: 3px;
        display: block;
        background: #2980FF;
    }

    &:hover::after {
        width: 100%;
        margin-top: -2px;
        transition: width .3s;
    }
}

Here's a codepen example https://codepen.io/godhandkiller/pen/jOrVjdq这是一个代码笔示例https://codepen.io/godhandkiller/pen/jOrVjdq

Use @keyframes to change/pause animation.使用@keyframes更改/暂停动画。

 @keyframes hover { 0% { // normal styles } 100% { // hover styles } } .class { animation-name: hover; animation-duration: 1ms; animation-fill-mode: backwards; animation-play-state: paused; } .class:active, .class:focus, .class:hover { //hover style you define }

Use this link to see an example.使用此链接查看示例。

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

相关问题 如何在鼠标悬停和鼠标移开时保留和隐藏工具提示 - How to keep and hide tooltip on mouse hover and mouse out 如何在鼠标悬停时在文本的左侧和右侧滑出一行 - How can I slide out a line on the left and right side of text on mouse hover 鼠标悬停时,<a>标签下划线从左到右过渡,鼠标移开时,下划线过渡从右到左</a> - On mouse hover the <a> tag underline transition from left to right and on mouse out the underline transition is back right to left 鼠标悬停,只使文字加粗,下划线应正常 - mouse hover, make only the text bold , the underline should be normal 如何在悬停时更改文本并在鼠标移出时恢复默认设置? - How to change text on hover and revert to default on mouse out? 如何在悬停中删除背景颜色并将其更改为下划线,但是在Bootstrap中文本和行之间有空格? - How to remove background color in hover and change it to underline but with space between text and line in Bootstrap? 悬停时显示下划线的文本 - Text that shows an underline on hover 悬停时进行CSS过渡:也为鼠标移出动画 - CSS Transition on hover: animate mouse-out as well :hover 只有一行文字装饰:下划线; - :hover only one line with text-decoration: underline; Animate Link在点击时下划线而不是悬停? - Animate Link underline on click as opposed to on hover?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM