|
|
|
演示效果
HTML&CSS - <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>全屏轮播</title>
- <style>
- @import url("https://fonts.googleapis.com/css2?family=Cinzel:wght@400..900&display=swap");
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- --primary: #fff;
- --secondary: #2e2d2d;
- --frost: rgba(255, 255, 255, 0.4);
- }
- body {
- height: 100vh;
- margin: 0;
- display: flex;
- align-items: center;
- background-color: var(--primary);
- overflow: hidden;
- transition: all 1s ease-in-out;
- }
- body::before {
- content: "";
- position: fixed;
- inset: 0;
- background-color: rgba(255, 255, 255, 0.2);
- pointer-events: none;
- }
- .card {
- width: 500px;
- height: 200px;
- max-width: 80vw;
- margin: 0 auto;
- display: grid;
- grid-template-columns: 1fr 1fr;
- border-radius: 5px;
- overflow: hidden;
- background-color: var(--frost);
- backdrop-filter: blur(10px);
- -webkit-backdrop-filter: blur(10px);
- box-shadow: 08px32pxrgba(0, 0, 0, 0.1);
- }
- .content {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- z-index: 50;
- background-color: var(--primary);
- }
- #title {
- font-weight: 400;
- font-family: "Cinzel", serif;
- color: var(--secondary);
- font-size: 1.2rem;
- }
- .image {
- overflow: hidden;
- }
- #slide {
- height: 100%;
- width: 100%;
- object-fit: cover;
- }
- .dots {
- width: 100%;
- position: absolute;
- bottom: 10px;
- left: 0;
- display: flex;
- gap: 10px;
- justify-content: center;
- align-items: center;
- }
- .dot {
- height: 15px;
- width: 15px;
- display: inline-block;
- border-radius: 50%;
- background-color: var(--frost);
- cursor: pointer;
- transition: background-color 0.3s ease;
- }
- .active {
- background-color: var(--primary);
- }
- @keyframes slidein {
- from {
- transform: translateX(-3000px);
- }
- to {
- transform: translateX(0);
- }
- }
- @media (width >=48rem) {
- #title {
- font-size: 2rem;
- }
- }
- </style>
- </head>
- <body>
- <div class="card">
- <div class="content">
- <h1 id="title">Title</h1>
- </div>
- <div class="image">
- <img src="https://q6.itc.cn/q_70/images03/20250116/b5dba16a1f5749a6a375f20f901d6381.png"
- alt="Photo of Paul Cuoco from Unsplash" id="slide" />
- </div>
- </div>
- <div class="dots">
- <span class="dot"></span>
- <span class="dot"></span>
- <span class="dot"></span>
- </div>
- <script>
- const collection = [
- {
- title: "好景",
- imageUrl:
- "https://q6.itc.cn/q_70/images03/20250116/b5dba16a1f5749a6a375f20f901d6381.png",
- imageAlt: "桂林山水"
- },
- {
- title: "好山",
- imageUrl:
- "https://img0.baidu.com/it/u=3902947011,207703161&fm=253&app=138&f=JPEG?w=800&h=1455",
- imageAlt: "桂林山水"
- },
- {
- title: "好水",
- imageUrl:
- "https://img0.baidu.com/it/u=4091158592,2102354044&fm=253&app=138&f=JPEG?w=800&h=1455",
- imageAlt: "桂林山水"
- }
- ];
- const slideEl = document.getElementById("slide");
- const dots = document.getElementsByClassName("dot");
- const titleEl = document.getElementById("title");
- let current = 0;
- function getCurrentSlide(n) {
- current = n;
- slideEl.src = collection[n].imageUrl;
- slideEl.alt = collection[n].imageAlt;
- titleEl.textContent = collection[n].title;
- slideEl.style.animation = "none";
- void slideEl.offsetWidth;
- slideEl.style.animation = "slidein 0.5s ease-in-out forwards";
- document.body.style.background = `url(${collection[n].imageUrl})`;
- document.body.style.backgroundPosition = "center";
- document.body.style.backgroundSize = "cover";
- Array.from(dots).forEach((dot, index) => {
- if (index === n) {
- dot.classList.add("active");
- } else {
- dot.classList.remove("active");
- }
- });
- }
- Array.from(dots).forEach((dot, index) => {
- dot.addEventListener("click", () => {
- getCurrentSlide(index);
- });
- });
- document.addEventListener("DOMContentLoaded", () => {
- getCurrentSlide(current);
- setInterval(() => {
- current = (current + 1) % collection.length;
- getCurrentSlide(current);
- }, 2000);
- });
- </script>
- </body>
- </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
- const collection = [
- {
- title: "好景",
- imageUrl: "https://q6.itc.cn/q_70/images03/20250116/b5dba16a1f5749a6a375f20f901d6381.png",
- imageAlt: "桂林山水"
- },
- {
- title: "好山",
- imageUrl: "https://img0.baidu.com/it/u=3902947011,207703161&fm=253&app=138&f=JPEG?w=800&h=1455",
- imageAlt: "桂林山水"
- },
- {
- title: "好水",
- imageUrl: "https://img0.baidu.com/it/u=4091158592,2102354044&fm=253&app=138&f=JPEG?w=800&h=1455",
- imageAlt: "桂林山水"
- }
- ];
- const slideEl = document.getElementById("slide");
- const dots = document.getElementsByClassName("dot");
- const titleEl = document.getElementById("title");
- let current = 0;
- function getCurrentSlide(n) {
- current = n;
- slideEl.src = collection[n].imageUrl;
- slideEl.alt = collection[n].imageAlt;
- titleEl.textContent = collection[n].title;
- slideEl.style.animation = "none";
- void slideEl.offsetWidth;
- slideEl.style.animation = "slidein 0.5s ease-in-out forwards";
- document.body.style.background = `url(${collection[n].imageUrl})`;
- document.body.style.backgroundPosition = "center";
- document.body.style.backgroundSize = "cover";
- Array.from(dots).forEach((dot, index) => {
- if (index === n) {
- dot.classList.add("active");
- } else {
- dot.classList.remove("active");
- }
- });
- }
- Array.from(dots).forEach((dot, index) => {
- dot.addEventListener("click", () => {
- getCurrentSlide(index);
- });
- });
- document.addEventListener("DOMContentLoaded", () => {
- getCurrentSlide(current);
- setInterval(() => {
- current = (current + 1) % collection.length;
- getCurrentSlide(current);
- }, 2000);
- });
复制代码 collection:定义了一个包含图片信息的数组,每张图片有标题、图片 URL 和描述。slideEl、dots、titleEl:获取页面中的图片、指示点和标题元素。current:当前显示的图片索引。
|
|