如何创建:幻灯片
- หน้าก่อนหน้า หัวข้อเพจที่ตอบสนอง
- 下一页 幻灯片画廊
学习如何使用 CSS 和 JavaScript 创建响应式幻灯片。
幻灯片 / 轮播
幻灯片用于循环浏览元素:
创建幻灯片
第一步 - 添加 HTML:
<!-- 幻灯片容器 --> <div class="slideshow-container"> <!-- 带有数字和标题文本的全宽图像 --> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="img1.jpg" style="width:100%"> <div class="text">Caption Text</div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="img2.jpg" style="width:100%"> <div class="text">Caption Two</div> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="img3.jpg" style="width:100%"> <div class="text">Caption Three</div> </div> <!-- ปุ่มข้างหน้าและข้างหลัง --> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> <br> <!-- จุดประกาศ --> <div style="text-align:center"> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> </div>
ขั้นที่สอง - เพิ่ม CSS:
ตั้งรูปแบบสไตล์สำหรับปุ่มข้างหน้าและข้างหลัง ข้อความหัวข้อ และจุดประกาศ:
* {box-sizing:border-box} /* ตัวแทนต容器สไลด์ชัดแจ้ง */ .slideshow-container { max-width: 1000px; position: relative; margin: auto; } /* ซ่อนภาพด้วยความคิดเห็น */ .mySlides { display: none; } /* ปุ่มข้างหน้าและข้างหลัง */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; margin-top: -22px; padding: 16px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; } /* จัดลำดับปุ่มต่อไปที่ด้านขวา */ .next { right: 0; border-radius: 3px 0 0 3px; } /* ขณะเลื่อนเมาส์เหนือเพิ่มสีดำที่มีความโปร่งอากาศ */ .prev:hover, .next:hover { background-color: rgba(0,0,0,0.8); } /* ข้อความหัวข้อ */ .text { color: #f2f2f2; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } /* ข้อความเลข (1/3 ฯลฯ) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* จุด/ลูกปืน/ตัวแสดง */ .dot { cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: #717171; } /* อนิเมชั่นหลิก */ .fade { animation-name: fade; animation-duration: 1.5s; } @keyframes fade { from {opacity: .4} to {opacity: 1} }
ขั้นที่ 3 - เพิ่ม JavaScript:
let slideIndex = 1; showSlides(slideIndex); // ปุ่มถัดไป/ก่อนหน้า function plusSlides(n) { showSlides(slideIndex += n); } // ภาพย่อกลุ่ม function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { let i; let slides = document.getElementsByClassName("mySlides"); let dots = document.getElementsByClassName("dot"); if (n > slides.length) {slideIndex = 1} if (n < 1) {slideIndex = slides.length} for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex-1].style.display = "block"; dots[slideIndex-1].className += " active"; }
ภาพแผงโปรแกรมฯ อัตโนมัติ
เพื่อแสดงภาพแผงโปรแกรมฯ อัตโนมัติ ใช้โค้ดดังนี้:
let slideIndex = 0; showSlides(); function showSlides() { let i; let slides = document.getElementsByClassName("mySlides"); for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } slideIndex++; if (slideIndex > slides.length) {slideIndex = 1} slides[slideIndex-1].style.display = "block"; setTimeout(showSlides, 2000); // เปลี่ยนภาพทุกๆ 2 วินาที }
การแสดงภาพแผงหลายภาพ
let slideIndex = [1,1]; /* แบ่งสิ่งในกลุ่มที่แสดงภาพแผงหลายแบบด้วยคลาส CSS ต่างกัน */ let slideId = ["mySlides1", "mySlides2"] showSlides(1, 0); showSlides(1, 1); function plusSlides(n, no) { showSlides(slideIndex[no] += n, no); } function showSlides(n, no) { let i; let x = document.getElementsByClassName(slideId[no]); if (n > x.length) {slideIndex[no] = 1} if (n < 1) {slideIndex[no] = x.length} for (i = 0; i < x.length; i++) { x[i].style.display = "none"; } x[slideIndex[no]-1].style.display = "block"; }
หน้าที่เกี่ยวข้อง
ตัวอย่าง:如何创建幻灯片图库
ตัวอย่าง:如何创建模态图库
- หน้าก่อนหน้า หัวข้อเพจที่ตอบสนอง
- 下一页 幻灯片画廊