Css微笔记

利用::first-line伪元素实现文字变加载动画
注册

利用::first-line伪元素实现文字变加载动画

利用-webkit-text-fill-color::first-line实现文字变加载动画
前者有兼容头(尽管兼容已经够好了),所以推荐使用后者::first-line

css代码

.button {
    font-size: 14px;
    color: #fff;
    border: 0;
    border-radius: 4px;
    padding: 5px 20px;
    height: 40px;
    background: #2a80eb;
    cursor: pointer;
    position: relative;
}
.button.loading::before {
    content: '';
    width: 20px; height: 20px;
    box-sizing: border-box;
    border: 2px solid;
    border-top-color: transparent;
    border-radius: 50%;
    position: absolute;
    inset: 0;
    margin: auto;
    animation: spin 1s linear infinite;
}
.fill-color.loading {
    -webkit-text-fill-color: transparent;
}
.first-line.loading::first-line {
    color: transparent;
}
@keyframes spin {
    from { rotate: 0deg; }    
    to { rotate: 360deg; }
}

HTML代码:

<h4 class="fill">-webkit-text-fill-color</h4>
<button 
    class="button fill-color" 
    onClick="this.classList.toggle('loading')"
>点击我</button>
<h4 class="fill">::first-line</h4>
<button 
    class="button first-line" 
    onClick="this.classList.toggle('loading')"
>点击我</button>

教程来自:https://www.zhangxinxu.com/wordpress/2025/03/css-first-line-hidden-text/

泽泽社长 更新于2025-06-24 发布于2025-06-24 8

大纲
加载中