CSS实现前端小组件随笔

一.CSS+JS实现打字机效果

1.1实现效果

1.2HTML部分

html 复制代码
<span class="bottom-text"></span>

1.3CSS部分

css 复制代码
.bottom-text {
    font-fanmily: "fangsong";
    display:inline-block;
    position:relative;
    font-size:20px;
    height:20px;
    inline-height:20px;
    color:white;
}

.bottom-text::after {
    content:"";
    position:absolute;
    right:-10px;
    top:5px;
    height:20px;
    width:2px;
    background-color:#fff;
    animation: san 0.5s steps(1) infinite;
}
@keyframes san{
    0%,100% {
        background-color:#fff;
       }
       50% {
            background-color:transparent;
       }
}

1.4JS部分

javascript 复制代码
    <script>
        //页面底部打字机效果
        const text = document.querySelector(".bottom-text");
        const txt = ["你不比任何人差。","答案在风中飘荡。"];
        var crIndex = 0;
        var txtIndex = 0;
        var switchMode = true;
        setInterval(function(){
            if(switchMode) {
                text.innerHTML = txt[txtIndex].slice(0,++crIndex);
            }
            else {
                text.innerHTML = txt[txtIndex].slice(0,crIndex);
                crIndex--;
            }
            if(crIndex == txt[txtIndex].length+3) {
                switchMode = false;
            }
            else if (crIndex < 0) {
                crIndex = 0;
                switchMode = true;
                txtIndex++;
                if(txtIndex >= txt.length){
                    txtIndex = 0;
                }
            }
        },200);
    </script>
相关推荐
码海扬帆:前端探索之旅6 小时前
深度定制 uni-combox:新增功能详解与实战指南
前端·vue.js·uni-app
谷雨不太卷6 小时前
进程的状态码
java·前端·算法
打小就很皮...6 小时前
基于 Python + LangChain + RAG 的知识检索系统实战
前端·langchain·embedding·rag
BJ-Giser6 小时前
Cesium 烟雾粒子特效
前端·可视化·cesium
空中海6 小时前
02 ArkTS 语言与工程规范
java·前端·spring
YJlio6 小时前
7.4.5 Windows 11 企业网络连接与网络重置实战:远程访问、本地策略与故障恢复
前端·chrome·windows·python·edge·机器人·django
Slow菜鸟6 小时前
Codex CLI 教程(五)| Skills 安装指南:面向 Java 全栈工程师打造个人 ECC(V1版)
大数据·前端·人工智能
Lee川7 小时前
打字机是怎么炼成的:Chat 流式输出深度解析
前端·后端·面试
前端若水7 小时前
过渡(transition)高级:贝塞尔曲线、硬件加速
前端·css·css3
Lee川7 小时前
Token 无感刷新与 Logout:前端安全会话管理实战
前端·后端·react.js