
今天來講一個很常用的js特效,這個不用說了,是個網頁應該都可以看到,雖然不難,但是對很多人來說,也是需要一定的基礎來掌握(html和css不難,js需要認真看,細細品),話不多說,上圖片(默認為第一張圖片)而且是回應式的(隨著螢屏大小的變化而變化!):



需要注意的點都給大家圈起來了!
需要注意的就是還有一些css樣式,不懂的可以看這里
主要用到就是點擊的時候css屬性的轉移,重點都在代碼中標注出來了,希望大家認真琢磨!
最后附上原始碼!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,,shrink-to-fit=no">
<title>回應式輪播圖</title>
<link rel="stylesheet" href="1.css">
</head>
<body>
<!-- 設定圖片組(三張圖片) -->
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="img/1.jpg" style="width:100%">
<div class="text">美麗的山川</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="img/2.jpg" style="width:100%">
<div class="text">絢麗的云彩</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="img/3.jpg" style="width:100%">
<div class="text">呆萌的兔子</div>
</div>
<!-- 設定按鈕,并添加一個js事件 -->
<a class="prev" onclick="plusSlides(-1)">?</a>
<a class="next" onclick="plusSlides(1)">?</a>
</div>
<br>
<!-- 設定底部原點,并添加一個js事件 -->
<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>
<script src="1.js"></script>
</body>
</html>
* {
/* 不清除盒子的margin,默認還有8px */
box-sizing: border-box
}
body {
font-family: Verdana, sans-serif;
}
.mySlides {
/* 先讓他們都隱藏起來 */
display: none
}
/* 幻燈片容器 */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* 下一張 & 上一張 按鈕 */
.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;
}
/* 定位 "下一張" 按鈕靠右 */
.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: 13px;
width: 13px;
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 {
-webkit-animation-name: fade;
-webkit-animation-duration: 1s;
animation-name: fade;
animation-duration: 1s;
}
@-webkit-keyframes fade {
from {
opacity: .6
}
to {
opacity: 1
}
}
@keyframes fade {
from {
opacity: .6
}
to {
opacity: 1
}
}
// 設定一個引數記錄當前是第幾張圖片
var slideIndex = 1;
showSlides(slideIndex);
// 設定按鈕的點擊事件
function plusSlides(n) {
showSlides(slideIndex += n);
}
// 設定最下邊小原點的點擊事件
function currentSlide(n) {
showSlides(slideIndex = n);
}
// 設定當前在第幾張的計算方式
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var 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";
}
彩蛋來了!將以上的js檔案換成下面的會實作自動定時輪播!
還等什么,趕緊去試試吧!喜歡的記得留下三連哦!手動比心!
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {slideIndex = 1}
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";
setTimeout(showSlides, 2000); // 切換時間為 2 秒
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/229476.html
標籤:其他
上一篇:CSS系列講解-總目錄
