<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv=" ...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>圖片切換</title>
<style>
.picture {
position: relative;
width: 500px;
height: 333px;
margin: 0 auto;
border: 2px solid rgb(231, 127, 217);
overflow: hidden;
}
.radius { /* 圓點所在的p (容器) */
width: 100%;
height: 10px;
position: absolute;
bottom: 30px;
text-align: center;
}
.p
g {
position: absolute;
margin: 0;
width: 100%;
height: 20px;
background-color: rgba(0, 0, 0, .4);
text-align: center;
font-size: 16px;
font-weight: 600;
color: #fff;
}
.title {
position: absolute;
width: 100%;
bottom: 0px;
text-align: center;
font-size: 16px;
font-weight: 600;
color: rgb(21, 223, 72);
}
span {
display: inline-block;
border: 10px solid #fdfdfd;
border-radius: 50%;
}
.active {
border: 10px solid #656466;
}
/* 左右箭頭 */
.arrowhead-left,
.arrowhead-right {
position: absolute;
width: 41px;
height: 69px;
font-size: 30px;
line-height: 70px;
text-align: center;
color: #D6D8D4;
background-color: rgba(0,0,0,.3);
}
.arrowhead-left {
left: 0;
top: 40%;
}
.arrowhead-right {
right: 0;
top: 40%;
}
</style>
</head>
<body>
<div class="picture">
<!-- 圖片頁碼 -->
<p class="pg">封面</p>
<img src="./image/d8.jpeg" alt="">
<!-- 小圓點點 -->
<p class="radius"></p>
<!-- 圖片的下麵標題 -->
<p class="title">標題</p>
<!-- 左右箭頭 -->
<div class="arrowhead-left" id="al"> < </div>
<div class="arrowhead-right" id="ar"> > </div>
</div>
<script>
var address = ["./image/d1.jpeg", "./image/d2.jpeg", "./image/d3.jpeg", "./image/d4.jpeg", "./image/d5.jpeg", "./image/d7.jpeg"];
// var imgs = document.getElementsByTagName("img");
var imgs = document.querySelector("img");
var len = address.length; //圖片地址的數量為len
var str = "";
var pp = document.getElementsByTagName("p");//獲取的是p標簽的集合
// var pp = document.querySelector("p"); //獲取的是一個元素
var al = document.getElementById("al");
var ar = document.getElementById("ar");
var n = 0 ;
//添加span標簽(小圓點),個數為len個
for (i = 0; i < len; i++) {
str += ' <span></span>'
}
pp[1].innerHTML = str;
var spans = pp[1].getElementsByTagName('span'); //獲取p[1]里所有span標簽
spans[0].className = 'active'; //給第一個span標簽添加樣式 active
for (i = 0; i < len; i++) {
spans[i].index = i; //自定義索引值
spans[i].onmouseover = function () { //滑鼠指向圓點時的事件
for (i = 0; i < len; i++) {
spans[i].className = ""; //通過迴圈,清除所有圓點的類名
}
n=this.index ;
this.className = 'active'; //給滑鼠移入的圓點添加類名
imgs.src = address[this.index];
pp[0].innerHTML = [this.index + 1] + "/6";
pp[2].innerHTML = "風光" + [this.index + 1];
}
}
ar.onclick = function () { //右側箭頭,點擊一次圖片向右換一張
n++;
if (n>5) {
n=0;
}
for (i = 0; i < len; i++) {
spans[i].className = "";
}
spans[n].className = "active";
imgs.src = address[n];
pp[0].innerHTML = (n+1) + "/6";
pp[2].innerHTML = "風光" +(n+1);
}
al.onclick = function () { // //左側箭頭,點擊一次圖片向左換一張
n--;
if (n<0) {
n=(len-1);
}
for (i = 0; i < len; i++) {
spans[i].className = "";
}
spans[n].className = "active";
imgs.src = address[n];
pp[0].innerHTML = (n+1) + "/6";
pp[2].innerHTML = "風光" +(n+1);
}
setInterval(ar.onclick,3000); //添加定時器 setInterval(函數,間隔時間單位為毫秒)
//此次添加的函數為點擊右側箭頭,間隔為3秒
</script>
</body>
</html>