【CSS练习】如何用纯 CSS 创作一个矩形旋转 loader 特效

image.png

【知识点】

  1. box-sizing
    定义了 user agent 应该如何计算一个元素的总宽度和总高度。可以更改用于计算元素宽度和高度的默认的 CSS 盒子模型。可以使用此属性来模拟不正确支持CSS盒子模型规范的浏览器的行为。
  • content-box
    默认值,标准盒子模型。 widthheight 只包括内容的宽和高, 不包括边框(border),内边距(padding),外边距(margin)。
  • border-box
    widthheight 属性包括内容,内边距和边框,但不包括外边距。这是当文档处于 Quirks模式 时Internet Explorer使用的盒模型
  • padding-box
  1. @keyframes
    产生名为rotating的动画效果,加入infinite关键字,可以让动画无限次播放
    animation: rotating linear infinite;
    定义动画效果
    animation-name: rotating;
    animation-timing-function: linear;

0%可以用from代表,100%可以用to代表

@keyframes rotating {
        from {
            transform: rotateY(0deg);
        }
        to {
            transform: rotateY(360deg);
        }
    }
}
  1. z-index
    sets the z-order of a positioned element and its descendants or flex items. Overlapping elements with a larger z-index cover those with a smaller one.
    大值覆盖小值
#loader span:nth-child(1) {
        width: 100%;
        height: 100%;
        z-index: 3;
        animation-duration:4s;
    }
    #loader span:nth-child(2) {
        width: 70%;
        height: 70%;
        margin: 15%;
        z-index: 2;
        animation-duration:2s;
    }
    #loader span:nth-child(3) {
        width: 40%;
        height: 40%;
        margin: 30%;
        z-index: 1;
        animation-duration:1s;
    }

代码

<style type="text/css">
    html, body{
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: black;
    }
    #loader {
        width: 150px;
        height: 150px;
        position: relative;
    }
    #loader span {
        position: absolute;
        border: 10px solid dimgray;
        border-radius: 2px;
        box-sizing: border-box;
        animation: rotating linear infinite;
    }
    #loader span:nth-child(1) {
        width: 100%;
        height: 100%;
        z-index: 3;
        animation-duration:4s;
    }
    #loader span:nth-child(2) {
        width: 70%;
        height: 70%;
        margin: 15%;
        z-index: 2;
        animation-duration:2s;
    }
    #loader span:nth-child(3) {
        width: 40%;
        height: 40%;
        margin: 30%;
        z-index: 1;
        animation-duration:1s;
    }
    #loader span::before, 
    #loader span::after{
        content: '';
        position: absolute;
        width: 10px;
        height: 50%;
        background-color: gold;
    }
    #loader span::before{
        top: -10px;
        left: -10px;
    }
    #loader span::after{
        bottom: -10px;
        right: -10px;
    }
    @keyframes rotating {
        from {
            transform: rotateY(0deg);
        }
        to {
            transform: rotateY(360deg);
        }
    }


    </style>

参考:https://segmentfault.com/a/1190000014675969

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 2,119评论 0 2
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,437评论 0 11
  • 一、CSS入门 1、css选择器 选择器的作用是“用于确定(选定)要进行样式设定的标签(元素)”。 有若干种形式的...
    宠辱不惊丶岁月静好阅读 1,727评论 0 6
  • 1、属性选择器:id选择器 # 通过id 来选择类名选择器 . 通过类名来选择属性选择器 ...
    Yuann阅读 1,752评论 0 7
  • 凡事彻底 今天看2018年华与华的新年总结展望与期待,四个大字映入眼帘,凡事彻底。想想自己在之前的工作中,对于工作...
    贾曼阅读 128评论 0 0

友情链接更多精彩内容