纯CSS实现红绿灯效果(面试常见)

建站知识 2021-07-03 07:54www.168986.cn长沙网站建设

先看题,别看答案试下吧 ~_~

1、狼蚁网站SEO优化的代码输出的内容是什么?

function O(name){
    this.name=name||'world';
}
O.prototype.hello=function(){
    return function(){
        console.log('hello '+this.name)
    }
}
var o=new O;
var hello=o.hello();
hello();

分析:

    1、O类实例化的时候赋值了一个属性name,默认值为world,那么在实例化的时候并未给值,所以name属性为world

    2、O类有一个原型方法hello,该方法其实是一个高阶函数,返回一个低阶函数,精髓就在这个低阶函数中的this,

        注意这里的低阶函数其实是在window环境中运行,所以this应该指向的是window

所以我的答案是:'hello undefined'(但这个答案是错误的,哈哈)

圈套:殊不知原生window是有name属性的,默认值为空

所以正确答案应该是:hello

2、给你一个div,用纯css写出一个红绿灯效果,按照红黄绿顺序依次循环点亮(无限循环)

当时没写出来,现场手写这么多代码是有难度的,狼蚁网站SEO优化是我后面实现代码(省略了浏览器兼容性前缀)

<div id="lamp"></div>
/*
思路:
    总共三个灯,分别红黄绿,要一个一个按顺序点亮,我们可以这样暂定一个循环需要10秒中,每盏灯点亮3秒,
    那么在keyframes中对应写法就如下所示(红灯点亮时间为10%--40%,黄灯点亮时间为40%--70%,绿灯点亮时间为70%--100%)
*/
@keyframes redLamp{
    0%{background-color: #999;}
    9.9%{background-color: #999;}
    10%{background-color: red;}
    40%{background-color: red;}
    40.1%{background-color: #999;}
    100%{background-color: #999;}
}
@keyframes yellowLamp{
    0%{background-color: #999;}
    39.9%{background-color: #999;}
    40%{background-color: yellow;}
    70%{background-color: yellow;}
    70.1%{background-color: #999;}
    100%{background-color: #999;}
}
@keyframes greenLamp{
    0%{background-color: #999;}
    69.9%{background-color: #999;}
    70%{background-color: green;}
    100%{background-color: green;}
}
#lamp,#lamp:before,#lamp:after{
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background-color: #999;
    position: relative;
}
#lamp{
    left: 100px;
    animation: yellowLamp 10s ease infinite;
}
#lamp:before{
    display: block;
    content: '';
    left: -100px;
    animation: redLamp 10s ease infinite;
}
#lamp:after{
    display: block;
    content: '';
    left: 100px;
    top: -100px;
    animation: greenLamp 10s ease infinite;
}

总结

以上所述是长沙网络推广给大家介绍的纯CSS实现红绿灯效果(面试常见),希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by

长沙网络推广|微博营销|长沙seo优化|视频营销|长沙网络营销|微信营销|长沙网站建设|口碑营销|软文营销