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>
相关推荐
雪芽蓝域zzs1 小时前
(六)打包优化 + Nginx 部署完整配置 + 项目收尾
前端·vue.js
研☆香1 小时前
聊一聊前端的常见字 关键字
开发语言·前端·javascript
东风破_1 小时前
JWT 1:从一个登录请求开始,理解 React 项目里的 API 层与 Mock
前端·后端
东风破_1 小时前
JWT 3:为什么 Token 要放进 Authorization?Axios 拦截器到底解决了什么?
前端·后端
东风破_1 小时前
JWT 5:路由守卫是什么?把整个 JWT 登录鉴权流程串起来
前端·后端
东风破_1 小时前
JWT 2:HTTP 是无状态的,为什么登录成功后还要给 Token?
前端·后端
东风破_1 小时前
JWT 4:Zustand 到底解决了什么?为什么登录状态要放进 Store?
前端·后端
IT_陈寒2 小时前
Vite动态导入差点让我秃头,原来问题出在这
前端·人工智能·后端
白泽_hunter2 小时前
项目里最常见的 5 个 this 指向坑:从规则到实战彻底讲透
前端
宿6742 小时前
vue3-pinia
前端·vue.js