HTMLCSS实现动态背景图

[复制链接]
发表于 2025-9-22 22:22:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在网页开辟中,一个好的加载页面不但能提拔用户体验,还能为网站增长独特的风格。下面将对一段实现加载页面效果的 HTML 与 CSS 代码举行详细先容。
团体结构概述

该代码创建了一个包罗背景、内容容器以及加载动画元素的网页页面。团体结构通过多个div元素和 CSS 样式来实现,告急包括背景层、内容层以及加载动画层。
HTML 部门
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7.     <style>
  8.         /* 样式代码部分,后续会详细介绍 */
  9.     </style>
  10. </head>
  11. <body>
  12.     <div class="xiao">
  13.         <div class="xiao1">
  14.             <div class="xiao2"></div>
  15.         </div>
  16.     </div>
  17.     <div class="xiao3"></div>
  18. </body>
  19. </html>
复制代码

  • <!DOCTYPE html>:声明文档范例为 HTML5。
  • <html lang="en">:界说 HTML 文档的根元素,并指定语言为英语。
  • <head> 部门:包罗了文档的元数据,如字符编码、视口设置以及页面标题。

    • <meta charset="UTF-8">:设置字符编码为 UTF-8,确保页面可以或许准确体现各种字符。
    • <meta name="viewport" content="width=device-width, initial-scale=1.0">:设置视口,使页面在差别装备上可以或许准确缩放和体现。
    • <title>Document</title>:界说页面的标题,这里体现为 “Document”。
           
  • <body> 部门:包罗了页面的实际内容。

    • <div class="xiao">:这是页面的最外层容器,用于设置背景图像和团体结构。
    • <div class="xiao1">:嵌套在.xiao容器内,用于放置告急内容区域,设置了白色背景。
    • <div class="xiao2">:嵌套在.xiao1内,是一个体现特定图像的容器。
    • <div class="xiao3">:独立于.xiao容器,用于体现加载动画的图像。
           
CSS 部门
  1. /* 去除默认的外边距和内边距 */
  2. body,
  3. html {
  4.     margin: 0;
  5.     padding: 0;
  6.     height: 100%;
  7. }
  8. div.xiao {
  9.     position: relative;
  10.     z-index: 1;
  11.     /* 设置宽度和高度为100% */
  12.     width: 100%;
  13.     height: 100%;
  14.     background-image: url(./png/bg.webp);
  15.     /* 使背景图像覆盖整个元素 */
  16.     background-size: cover;
  17.     /* 使背景图像居中 */
  18.     background-position: center;
  19. }
  20. .xiao1 {
  21.     position: fixed;
  22.     z-index: 2;
  23.     left: 50%;
  24.     top: 50%;
  25.     transform: translate(-50%, -50%);
  26.     width: 90%;
  27.     height: 84%;
  28.     background-color: #fffcf2;
  29.     background-position: center;
  30. }
  31. .xiao2 {
  32.     position: fixed;
  33.     top: 3%;
  34.     left: 50%;
  35.     transform: translate(-50%, 0);
  36.     width: 400px;
  37.     height: 280px;
  38.     background-image: url(./png/logo_portal.svg);
  39.     z-index: 3;
  40.     background-size: cover;
  41.     background-position: center;
  42. }
  43. .xiao3 {
  44.     position: fixed;
  45.     top: 53%;
  46.     left: 50%;
  47.     transform: translate(-50%, 0);
  48.     width: 190px;
  49.     height: 190px;
  50.     background-image: url(./png/loading.svg);
  51.     z-index: 3;
  52.     background-size: cover;
  53.     background-position: center;
  54.     /* 修改旋转速度为1秒一圈 */
  55.     animation: spin 5s linear infinite;
  56. }
  57. @keyframes spin {
  58.     0% {
  59.         transform: translate(-50%, 0) rotate(0deg);
  60.     }
  61.     100% {
  62.         transform: translate(-50%, 0) rotate(360deg);
  63.     }
  64. }
复制代码

  • 完备代码展示
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.     <meta charset="UTF-8">
    5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.     <title>Document</title>
    7.     <style>
    8.         /* 去除默认的外边距和内边距 */
    9.         body,
    10.         html {
    11.             margin: 0;
    12.             padding: 0;
    13.             height: 100%;
    14.         }
    15.         div.xiao {
    16.             position: relative;
    17.             z-index: 1;
    18.             /* 设置宽度和高度为100% */
    19.             width: 100%;
    20.             height: 100%;
    21.             background-image: url(./png/bg.webp);
    22.             /* 使背景图像覆盖整个元素 */
    23.             background-size: cover;
    24.             /* 使背景图像居中 */
    25.             background-position: center;
    26.         }
    27.         .xiao1 {
    28.             position: fixed;
    29.             z-index: 2;
    30.             left: 50%;
    31.             top: 50%;
    32.             transform: translate(-50%, -50%);
    33.             width: 90%;
    34.             height: 84%;
    35.             background-color: #fffcf2;
    36.             background-position: center;
    37.         }
    38.         .xiao2 {
    39.             position: fixed;
    40.             top: 3%;
    41.             left: 50%;
    42.             transform: translate(-50%, 0);
    43.             width: 400px;
    44.             height: 280px;
    45.             background-image: url(./png/logo_portal.svg);
    46.             z-index: 3;
    47.             background-size: cover;
    48.             background-position: center;
    49.         }
    50.         .xiao3 {
    51.             position: fixed;
    52.             top: 53%;
    53.             left: 50%;
    54.             transform: translate(-50%, 0);
    55.             width: 190px;
    56.             height: 190px;
    57.             background-image: url(./png/loading.svg);
    58.             z-index: 3;
    59.             background-size: cover;
    60.             background-position: center;
    61.             /* 修改旋转速度为1秒一圈 */
    62.             animation: spin 5s linear infinite;
    63.         }
    64.         @keyframes spin {
    65.             0% {
    66.                 transform: translate(-50%, 0) rotate(0deg);
    67.             }
    68.             100% {
    69.                 transform: translate(-50%, 0) rotate(360deg);
    70.             }
    71.         }
    72.     </style>
    73. </head>
    74. <body>
    75.     <div class="xiao">
    76.         <div class="xiao1">
    77.             <div class="xiao2"></div>
    78.         </div>
    79.     </div>
    80.     <div class="xiao3"></div>
    81. </body>
    82. </html>
    复制代码
    效果如图
  •        

 
 
 
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表