爬行的蜗牛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: golang Linux PHP
查看: 58|回复: 0

HTML&CSS&JS:超级惊艳的全屏图片轮播效果

[复制链接]

168

主题

47

回帖

1474

积分

管理员

积分
1474
发表于 2025-9-26 11:21:43 | 显示全部楼层 |阅读模式

演示效果
640.gif
HTML&CSS

  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>全屏轮播</title>
  7.     <style>
  8.         @import url("https://fonts.googleapis.com/css2?family=Cinzel:wght@400..900&display=swap");

  9.         * {
  10.             margin: 0;
  11.             padding: 0;
  12.             box-sizing: border-box;

  13.             --primary: #fff;
  14.             --secondary: #2e2d2d;
  15.             --frost: rgba(255, 255, 255, 0.4);
  16.         }

  17.         body {
  18.             height: 100vh;
  19.             margin: 0;
  20.             display: flex;
  21.             align-items: center;
  22.             background-color: var(--primary);
  23.             overflow: hidden;
  24.             transition: all 1s ease-in-out;
  25.         }

  26.         body::before {
  27.             content: "";
  28.             position: fixed;
  29.             inset: 0;
  30.             background-color: rgba(255, 255, 255, 0.2);
  31.             pointer-events: none;
  32.         }

  33.         .card {
  34.             width: 500px;
  35.             height: 200px;
  36.             max-width: 80vw;
  37.             margin: 0 auto;
  38.             display: grid;
  39.             grid-template-columns: 1fr 1fr;
  40.             border-radius: 5px;
  41.             overflow: hidden;
  42.             background-color: var(--frost);
  43.             backdrop-filter: blur(10px);
  44.             -webkit-backdrop-filter: blur(10px);
  45.             box-shadow: 08px32pxrgba(0, 0, 0, 0.1);
  46.         }

  47.         .content {
  48.             display: flex;
  49.             flex-direction: column;
  50.             align-items: center;
  51.             justify-content: center;
  52.             z-index: 50;
  53.             background-color: var(--primary);
  54.         }

  55.         #title {
  56.             font-weight: 400;
  57.             font-family: "Cinzel", serif;
  58.             color: var(--secondary);
  59.             font-size: 1.2rem;
  60.         }

  61.         .image {
  62.             overflow: hidden;
  63.         }

  64.         #slide {
  65.             height: 100%;
  66.             width: 100%;
  67.             object-fit: cover;
  68.         }

  69.         .dots {
  70.             width: 100%;
  71.             position: absolute;
  72.             bottom: 10px;
  73.             left: 0;
  74.             display: flex;
  75.             gap: 10px;
  76.             justify-content: center;
  77.             align-items: center;
  78.         }

  79.         .dot {
  80.             height: 15px;
  81.             width: 15px;
  82.             display: inline-block;
  83.             border-radius: 50%;
  84.             background-color: var(--frost);
  85.             cursor: pointer;
  86.             transition: background-color 0.3s ease;
  87.         }

  88.         .active {
  89.             background-color: var(--primary);
  90.         }

  91.         @keyframes slidein {
  92.             from {
  93.                 transform: translateX(-3000px);
  94.             }

  95.             to {
  96.                 transform: translateX(0);
  97.             }
  98.         }

  99.         @media (width >=48rem) {
  100.             #title {
  101.                 font-size: 2rem;
  102.             }
  103.         }
  104.     </style>
  105. </head>

  106. <body>
  107.     <div class="card">
  108.         <div class="content">
  109.             <h1 id="title">Title</h1>
  110.         </div>
  111.         <div class="image">
  112.             <img src="https://q6.itc.cn/q_70/images03/20250116/b5dba16a1f5749a6a375f20f901d6381.png"
  113.                 alt="Photo of Paul Cuoco from Unsplash" id="slide" />
  114.         </div>
  115.     </div>
  116.     <div class="dots">
  117.         <span class="dot"></span>
  118.         <span class="dot"></span>
  119.         <span class="dot"></span>
  120.     </div>
  121.     <script>
  122.         const collection = [
  123.             {
  124.                 title: "好景",
  125.                 imageUrl:
  126.                     "https://q6.itc.cn/q_70/images03/20250116/b5dba16a1f5749a6a375f20f901d6381.png",
  127.                 imageAlt: "桂林山水"
  128.             },
  129.             {
  130.                 title: "好山",
  131.                 imageUrl:
  132.                     "https://img0.baidu.com/it/u=3902947011,207703161&fm=253&app=138&f=JPEG?w=800&h=1455",
  133.                 imageAlt: "桂林山水"
  134.             },
  135.             {
  136.                 title: "好水",
  137.                 imageUrl:
  138.                     "https://img0.baidu.com/it/u=4091158592,2102354044&fm=253&app=138&f=JPEG?w=800&h=1455",
  139.                 imageAlt: "桂林山水"
  140.             }
  141.         ];

  142.         const slideEl = document.getElementById("slide");
  143.         const dots = document.getElementsByClassName("dot");
  144.         const titleEl = document.getElementById("title");
  145.         let current = 0;

  146.         function getCurrentSlide(n) {
  147.             current = n;
  148.             slideEl.src = collection[n].imageUrl;
  149.             slideEl.alt = collection[n].imageAlt;
  150.             titleEl.textContent = collection[n].title;

  151.             slideEl.style.animation = "none";
  152.             void slideEl.offsetWidth;
  153.             slideEl.style.animation = "slidein 0.5s ease-in-out forwards";

  154.             document.body.style.background = `url(${collection[n].imageUrl})`;
  155.             document.body.style.backgroundPosition = "center";
  156.             document.body.style.backgroundSize = "cover";

  157.             Array.from(dots).forEach((dot, index) => {
  158.                 if (index === n) {
  159.                     dot.classList.add("active");
  160.                 } else {
  161.                     dot.classList.remove("active");
  162.                 }
  163.             });
  164.         }

  165.         Array.from(dots).forEach((dot, index) => {
  166.             dot.addEventListener("click", () => {
  167.                 getCurrentSlide(index);
  168.             });
  169.         });

  170.         document.addEventListener("DOMContentLoaded", () => {
  171.             getCurrentSlide(current);

  172.             setInterval(() => {
  173.                 current = (current + 1) % collection.length;
  174.                 getCurrentSlide(current);
  175.             }, 2000);
  176.         });

  177.     </script>
  178. </body>

  179. </html>
复制代码
HTML
  • card:卡片容器,包含内容和图片。
  • content:内容区域,包含标题。
  • image:图片区域,包含一个 img 标签。
  • dots:指示点容器,包含三个指示点。
  • script:内联 JavaScript 逻辑。
CSS
  • @import:引入 Google 字体。 *:全局样式,设置默- 认的边距、填充和盒模型。
  • body:设置页面背景颜色、高度和过渡效果。
  • body::before:创建一个半透明的覆盖层。
  • .card:定义卡片的大小、布局和样式。
  • .content:定义内容区域的布局。
  • #title:设置标题的字体、颜色和大小。
  • .image:定义图片区域的样式。
  • #slide:设置图片的样式,确保图片覆盖整个区域。
  • .dots:定义指示点的布局。
  • .dot:定义单个指示点的样式。
  • .active:定义活动指示点的样式。
  • @keyframes slidein:定义图片滑入的动画效果。
  • @media:响应式设计,调整标题大小。
JavaScript

  1. const collection = [
  2.     {
  3.         title: "好景",
  4.         imageUrl: "https://q6.itc.cn/q_70/images03/20250116/b5dba16a1f5749a6a375f20f901d6381.png",
  5.         imageAlt: "桂林山水"
  6.     },
  7.     {
  8.         title: "好山",
  9.         imageUrl: "https://img0.baidu.com/it/u=3902947011,207703161&fm=253&app=138&f=JPEG?w=800&h=1455",
  10.         imageAlt: "桂林山水"
  11.     },
  12.     {
  13.         title: "好水",
  14.         imageUrl: "https://img0.baidu.com/it/u=4091158592,2102354044&fm=253&app=138&f=JPEG?w=800&h=1455",
  15.         imageAlt: "桂林山水"
  16.     }
  17. ];

  18. const slideEl = document.getElementById("slide");
  19. const dots = document.getElementsByClassName("dot");
  20. const titleEl = document.getElementById("title");
  21. let current = 0;

  22. function getCurrentSlide(n) {
  23.     current = n;
  24.     slideEl.src = collection[n].imageUrl;
  25.     slideEl.alt = collection[n].imageAlt;
  26.     titleEl.textContent = collection[n].title;

  27.     slideEl.style.animation = "none";
  28.     void slideEl.offsetWidth;
  29.     slideEl.style.animation = "slidein 0.5s ease-in-out forwards";

  30.     document.body.style.background = `url(${collection[n].imageUrl})`;
  31.     document.body.style.backgroundPosition = "center";
  32.     document.body.style.backgroundSize = "cover";

  33.     Array.from(dots).forEach((dot, index) => {
  34.         if (index === n) {
  35.             dot.classList.add("active");
  36.         } else {
  37.             dot.classList.remove("active");
  38.         }
  39.     });
  40. }

  41. Array.from(dots).forEach((dot, index) => {
  42.     dot.addEventListener("click", () => {
  43.         getCurrentSlide(index);
  44.     });
  45. });

  46. document.addEventListener("DOMContentLoaded", () => {
  47.     getCurrentSlide(current);

  48.     setInterval(() => {
  49.         current = (current + 1) % collection.length;
  50.         getCurrentSlide(current);
  51.     }, 2000);
  52. });
复制代码
collection:定义了一个包含图片信息的数组,每张图片有标题、图片 URL 和描述。slideEl、dots、titleEl:获取页面中的图片、指示点和标题元素。current:当前显示的图片索引。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表