📌  相关文章
📜  如何使用 HTML 和 CSS 创建窗口加载效果?

📅  最后修改于: 2021-10-28 03:20:03             🧑  作者: Mango

在本文中,我们将使用 HTML 和 CSS 在锁屏出现之前创建一个窗口加载效果。

Windows 加载效果概览:

方法:

  • 创建一个包含 HTML div的 HTML 文件,我们在其中提供加载器效果。
  • 然后我们创建 5 个 span 元素,用于创建内联元素。
  • 然后我们必须使用@keyframe 来创建动画功能。
  • 然后我们必须使用 nth-child() 属性来选择不同的孩子。

HTML代码:

  • 首先,我们创建一个 HTML 文件 (index.html)。
  • 现在在创建我们的 HTML 文件之后,我们将使用 标签为我们的网页提供一个标题。它应该放在 <head> 标签之间。</li> <li>我们将提供所有动画效果的 CSS 文件链接到我们的 HTML。这也位于 <head> 标记之间。</li> <li>现在我们添加一个来自 Google Fonts 的链接,以便在我们的项目中使用不同类型的字体系列。</li> <li>来到我们 HTML 代码的正文部分。</li> <li>然后,我们必须创建一个 div,我们可以在其中存储所有标题部分和 span 标签。</li> </ul> <div class="noIdeBtnDiv"> <div> <h5 class="code">index.html</h5> <div class="hcb_wrap"> <pre class="prism line-numbers lang-kotlin" data-lang="kotlin"><code class="replace"><!DOCTYPE html> <html lang="en">    <head>     <link rel="stylesheet" href="style.css">     <link rel="preconnect" href="https://fonts.gstatic.com">     <link href= "https://fonts.googleapis.com/css2?family=Dosis:wght@300&family=Hanalei&display=swap"         rel="stylesheet"> </head>    <body>     <h1>Windows-Loading-Effect</h1>     <div class="container">         <span></span>         <span></span>         <span></span>         <span></span>         <span></span>     </div> </body>    </html></code></pre> </div> <p></p> <hr/> <h5 class="code">style.css</h5> <div class="hcb_wrap"> <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">*{     margin: 0;     padding: 0;     box-sizing: border-box; }    /* Common styles of project which    are applied to body */ body{     background-color: rgb(0, 21, 138);     overflow: hidden;     font-family: 'Dosis', sans-serif;     color: #fff; }    /* Style to our heading */ h1{     display: flex;     margin-top: 3em;     justify-content: center; }    .container{     position: absolute;     top: 50%;     left: 50%;     transform: translate(-50%,-50%); }    span{     display: inline-block;     width: 0.6em;     height: 0.6em;     border-radius: 50%;     margin: 0 0.125em;     background-color: rgb(235, 217, 217);     opacity: 0; }    /* Calling childs using nth-child() property */ span:last-child{     animation: move-right 3s infinite;     animation-delay: 100ms;     background-color: #000; } span:nth-child(5){     animation: move 3s infinite;     animation-delay: 200ms;     background-color: rgb(41, 133, 22); } span:nth-child(4){     animation:  move-right 3s infinite;     animation-delay: 300ms;     background-color: #000; } span:nth-child(3){     animation: move 3s infinite;     animation-delay: 400ms;     background-color: rgb(41, 133, 22); } span:nth-child(2){     animation:  move-right 3s infinite;     animation-delay: 500ms;     background-color: #000; } span:first-child{     animation: move 3s infinite;     animation-delay: 600ms;     background-color: rgb(41, 133, 22); }       /* Animations effect*/ @keyframes move{     0%{         transform: translateX(-31em);         opacity: 0;     }     30%,60%{         transform: translateX(0);         opacity: 1;     }     100%{         transform: translateX(31em);         opacity: 0;     } } @keyframes move-right{     0%{         transform: translateX(31em);         opacity: 0;     }     30%,60%{         transform: translateX(0);         opacity: 1;     }     100%{         transform: translateX(-31em);         opacity: 0;     } }</code></pre> </div> <p></p> <hr/> <h5 class="code">index.html</h5> <div class="hcb_wrap"> <pre class="prism line-numbers lang-perl" data-lang="Perl"><code class="replace"><!DOCTYPE html> <html lang="en">     <head>         <link rel="stylesheet" href="style.css" />         <link rel="preconnect" href="https://fonts.gstatic.com" />         <link href= "https://fonts.googleapis.com/css2?family=Dosis:wght@300&family=Hanalei&display=swap"               rel="stylesheet" />         <style>             * {                 margin: 0;                 padding: 0;                 box-sizing: border-box;             }                /* Common styles of project which                are applied to body */             body {                 background-color: rgb(0, 21, 138);                 overflow: hidden;                 font-family: "Dosis", sans-serif;                 color: #fff;             }                /* Style to our heading */             h1 {                 display: flex;                 margin-top: 3em;                 justify-content: center;             }                .container {                 position: absolute;                 top: 50%;                 left: 50%;                 transform: translate(-50%, -50%);             }                span {                 display: inline-block;                 width: 0.6em;                 height: 0.6em;                 border-radius: 50%;                 margin: 0 0.125em;                 background-color: rgb(235, 217, 217);                 opacity: 0;             }                /* Calling childs using nth-child() property */             span:last-child {                 animation: move-right 3s infinite;                 animation-delay: 100ms;                 background-color: #000;             }             span:nth-child(5) {                 animation: move 3s infinite;                 animation-delay: 200ms;                 background-color: rgb(41, 133, 22);             }             span:nth-child(4) {                 animation: move-right 3s infinite;                 animation-delay: 300ms;                 background-color: #000;             }             span:nth-child(3) {                 animation: move 3s infinite;                 animation-delay: 400ms;                 background-color: rgb(41, 133, 22);             }             span:nth-child(2) {                 animation: move-right 3s infinite;                 animation-delay: 500ms;                 background-color: #000;             }             span:first-child {                 animation: move 3s infinite;                 animation-delay: 600ms;                 background-color: rgb(41, 133, 22);             }                /* Animations effect */             @keyframes move {                 0% {                     transform: translateX(-31em);                     opacity: 0;                 }                 30%,                 60% {                     transform: translateX(0);                     opacity: 1;                 }                 100% {                     transform: translateX(31em);                     opacity: 0;                 }             }             @keyframes move-right {                 0% {                     transform: translateX(31em);                     opacity: 0;                 }                 30%,                 60% {                     transform: translateX(0);                     opacity: 1;                 }                 100% {                     transform: translateX(-31em);                     opacity: 0;                 }             }         </style>     </head>     <body>         <h1>Windows-Loading-Effect</h1>         <div class="container">             <span></span>             <span></span>             <span></span>             <span></span>             <span></span>         </div>     </body> </html></code></pre> </div> <p></p> <hr/> </div> </div> <p> <strong>CSS 代码:</strong> CSS 用于为我们的 HTML 页面提供不同类型的动画和效果,使其看起来对所有用户都是交互的。</p> <ul> <li>恢复所有浏览器效果。</li> <li>使用类和 id 为 HTML 元素赋予效果。</li> <li>使用<i>@keyframes</i>为浏览器提供动画/过渡效果。</li> <li>使用第 n 个 child() 属性来调用子元素。</li> </ul> <p> CSS 的所有功能都包含在以下代码中。</p> <div class="noIdeBtnDiv"> <div class="responsive-tabs"> <h2 class="tabtitle">样式文件</h2> <div class="tabcontent"> <div class="hcb_wrap"> <pre class="prism line-numbers lang-html" data-lang="HTML"><code class="replace">*{     margin: 0;     padding: 0;     box-sizing: border-box; }    /* Common styles of project which    are applied to body */ body{     background-color: rgb(0, 21, 138);     overflow: hidden;     font-family: 'Dosis', sans-serif;     color: #fff; }    /* Style to our heading */ h1{     display: flex;     margin-top: 3em;     justify-content: center; }    .container{     position: absolute;     top: 50%;     left: 50%;     transform: translate(-50%,-50%); }    span{     display: inline-block;     width: 0.6em;     height: 0.6em;     border-radius: 50%;     margin: 0 0.125em;     background-color: rgb(235, 217, 217);     opacity: 0; }    /* Calling childs using nth-child() property */ span:last-child{     animation: move-right 3s infinite;     animation-delay: 100ms;     background-color: #000; } span:nth-child(5){     animation: move 3s infinite;     animation-delay: 200ms;     background-color: rgb(41, 133, 22); } span:nth-child(4){     animation:  move-right 3s infinite;     animation-delay: 300ms;     background-color: #000; } span:nth-child(3){     animation: move 3s infinite;     animation-delay: 400ms;     background-color: rgb(41, 133, 22); } span:nth-child(2){     animation:  move-right 3s infinite;     animation-delay: 500ms;     background-color: #000; } span:first-child{     animation: move 3s infinite;     animation-delay: 600ms;     background-color: rgb(41, 133, 22); }       /* Animations effect*/ @keyframes move{     0%{         transform: translateX(-31em);         opacity: 0;     }     30%,60%{         transform: translateX(0);         opacity: 1;     }     100%{         transform: translateX(31em);         opacity: 0;     } } @keyframes move-right{     0%{         transform: translateX(31em);         opacity: 0;     }     30%,60%{         transform: translateX(0);         opacity: 1;     }     100%{         transform: translateX(-31em);         opacity: 0;     } } </code></pre> </div> </div> </div> </div> <p><strong>完整代码:</strong>这里我们将以上两段代码合二为一。</p> <div class="responsive-tabs"> <h2 class="tabtitle">索引.html </h2> <div class="tabcontent"> <div class="hcb_wrap"> <pre class="prism line-numbers lang-html" data-lang="HTML"><code class="replace"><!DOCTYPE html> <html lang="en">     <head>         <link rel="stylesheet" href="style.css" />         <link rel="preconnect" href="https://fonts.gstatic.com" />         <link href= "https://fonts.googleapis.com/css2?family=Dosis:wght@300&family=Hanalei&display=swap"               rel="stylesheet" />         <style>             * {                 margin: 0;                 padding: 0;                 box-sizing: border-box;             }                /* Common styles of project which                are applied to body */             body {                 background-color: rgb(0, 21, 138);                 overflow: hidden;                 font-family: "Dosis", sans-serif;                 color: #fff;             }                /* Style to our heading */             h1 {                 display: flex;                 margin-top: 3em;                 justify-content: center;             }                .container {                 position: absolute;                 top: 50%;                 left: 50%;                 transform: translate(-50%, -50%);             }                span {                 display: inline-block;                 width: 0.6em;                 height: 0.6em;                 border-radius: 50%;                 margin: 0 0.125em;                 background-color: rgb(235, 217, 217);                 opacity: 0;             }                /* Calling childs using nth-child() property */             span:last-child {                 animation: move-right 3s infinite;                 animation-delay: 100ms;                 background-color: #000;             }             span:nth-child(5) {                 animation: move 3s infinite;                 animation-delay: 200ms;                 background-color: rgb(41, 133, 22);             }             span:nth-child(4) {                 animation: move-right 3s infinite;                 animation-delay: 300ms;                 background-color: #000;             }             span:nth-child(3) {                 animation: move 3s infinite;                 animation-delay: 400ms;                 background-color: rgb(41, 133, 22);             }             span:nth-child(2) {                 animation: move-right 3s infinite;                 animation-delay: 500ms;                 background-color: #000;             }             span:first-child {                 animation: move 3s infinite;                 animation-delay: 600ms;                 background-color: rgb(41, 133, 22);             }                /* Animations effect */             @keyframes move {                 0% {                     transform: translateX(-31em);                     opacity: 0;                 }                 30%,                 60% {                     transform: translateX(0);                     opacity: 1;                 }                 100% {                     transform: translateX(31em);                     opacity: 0;                 }             }             @keyframes move-right {                 0% {                     transform: translateX(31em);                     opacity: 0;                 }                 30%,                 60% {                     transform: translateX(0);                     opacity: 1;                 }                 100% {                     transform: translateX(-31em);                     opacity: 0;                 }             }         </style>     </head>     <body>         <h1>Windows-Loading-Effect</h1>         <div class="container">             <span></span>             <span></span>             <span></span>             <span></span>             <span></span>         </div>     </body> </html> </code></pre> </div> </div> </div> <p><strong>输出:</strong> </p> <div class="wp-caption alignnone" style="width:810px"><img class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/How_to_create_windows_loading_effect_using_HTML_and_CSS_?_1.jpg"/> <p class="wp-caption-text"> Windows加载效果</p> </div> <div class="_ap_apex_ad" id="82d2079a-8120-480f-9fc7-5cda825d56e7" style="min-height:280px"></div> <p></p></div> </article> </div> </div> </div> </div> </div> <footer> <div class="bg-white text-center pb-1"> <p class="text-body-tertiary pt-3 lh-lg text-opacity-50" id="footer-text">Copyright © 2020 - 2024 版权所有 <br> <a href="https://beian.miit.gov.cn/" target="_blank" class="text-opacity-50 text-body-tertiary mt-1 mb-1">蜀ICP备20006366号-1</a> <br> Made with ❤️ in Chengdu </p> </div> </footer> <!-- 引入Bootstrap JavaScript库 --> <script src="https://unpkg.com/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script> <!-- 引入Meilisearch搜索相关的JavaScript库 --> <script src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script> <script src="https://imangodoc.com/static/javascript/meili_custom.js"></script> <!-- 引入prismjs代码高亮相关的JavaScript库 --> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/toolbar/prism-toolbar.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script> </body> </html>