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>
相关推荐
excel7 小时前
ES6 中函数的双重调用方式:fn() 与 fn\...``
前端
可乐爱宅着7 小时前
全栈框架next.js入手指南
前端·next.js
你的人类朋友8 小时前
什么是API签名?
前端·后端·安全
会豪10 小时前
Electron-Vite (一)快速构建桌面应用
前端
中微子10 小时前
React 执行阶段与渲染机制详解(基于 React 18+ 官方文档)
前端
唐某人丶10 小时前
教你如何用 JS 实现 Agent 系统(2)—— 开发 ReAct 版本的“深度搜索”
前端·人工智能·aigc
中微子10 小时前
深入剖析 useState产生的 setState的完整执行流程
前端
遂心_11 小时前
JavaScript 函数参数传递机制:一道经典面试题解析
前端·javascript
小徐_233311 小时前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts
RoyLin11 小时前
TypeScript设计模式:适配器模式
前端·后端·node.js