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>
相关推荐
SleepyZone8 分钟前
Cline 源码浅析 - 从输入到输出
前端·ai编程·cline
Struggler28111 分钟前
pinia-基于monorepo的项目结构管理
前端
Struggler28116 分钟前
SSE的使用
前端
用户58061393930022 分钟前
前端文件下载实现深度解析:Blob与ObjectURL的完美协作
前端
Lin866625 分钟前
Vue 3 + TypeScript 组件类型推断失败问题完整解决方案
前端
coding随想25 分钟前
从零开始:前端开发者的SEO优化入门与实战
前端
前端工作日常28 分钟前
我理解的JSBridge
前端
Au_ust28 分钟前
前端模块化
前端
顺丰同城前端技术团队28 分钟前
还不会用 Charles?最后一遍了啊!
前端
BUG收容所所长30 分钟前
二分查找的「左右为难」:如何优雅地找到数组中元素的首尾位置
前端·javascript·算法