Html+CSS+JS輪播圖實(shí)現(xiàn)源碼(手動(dòng)輪播,自動(dòng)輪播)
演示效果

輪播圖代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.banner {
width: 500px;
height: 300px;
position: relative;
/* border: 1px solid red; */
margin: 100px auto;
}
.banner .wrap {
width: 100%;
}
.banner .wrap .item {
width: 100%;
}
.item img {
width: 500px;
height: 300px;
vertical-align: top;
position: absolute;
}
.div1 {
position: absolute;
left: 0;
top: 50%;
transform: translatey(-50%);
cursor: pointer;
width: 41px;
height: 69px;
font-size: 30px;
line-height: 70px;
text-align: center;
color: #D6D8D4;
background-color: rgba(0, 0, 0, 0.3);
}
.div2 {
position: absolute;
right: 0;
top: 50%;
transform: translatey(-50%);
cursor: pointer;
width: 41px;
height: 69px;
font-size: 30px;
line-height: 70px;
text-align: center;
color: #D6D8D4;
background-color: rgba(0, 0, 0, 0.3);
}
.pagenation {
position: absolute;
/* border: 1px solid red; */
left: 50%;
transform: translateX(-50%);
display: flex;
bottom: 40px;
}
.pagenation>div {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: white;
margin-right: 10px;
cursor: pointer;
}
.pagenation>div:last-child {
margin-right: 0;
}
</style>
</head>
<body>
<div class="banner">
<div class="warp">
<div class="item"><img src="./img/1.jpg" alt=""></div>
<div class="item"><img src="./img/2.jpg" alt=""></div>
<div class="item"><img src="./img/3.jpg" alt=""></div>
<div class="item"><img src="./img/4.jpg" alt=""></div>
</div>
<div class="div1">
<</div> <div class="div2 ">>
</div>
<!-- 小圓點(diǎn) -->
<div class="pagenation">
<div id="pagenation-item0"></div>
<div id="pagenation-item1"></div>
<div id="pagenation-item2"></div>
<div id="pagenation-item3"></div>
</div>
</div>
<script>
var index = 0; //定義初始下標(biāo)
var banner = document.getElementsByClassName("banner")[0];
var itemList = document.getElementsByClassName("item");
var pagenationList = document.querySelectorAll(".pagenation>div");
var pagenation = document.querySelector(".pagenation");
itemList[0].style.opacity = 1;
pagenationList[0].style.background = "blue" //初始第一張圖對(duì)應(yīng)的小圓點(diǎn)的顏色為藍(lán)色
var up = document.getElementsByClassName("div1")[0];
var next = document.getElementsByClassName("div2")[0];
//下一張圖片(封裝方便下面自動(dòng)播放定時(shí)器調(diào)用)
function nextFn() {
index = index >= itemList.length - 1 ? 0 : ++index; //判斷點(diǎn)擊到了最后一張圖片再次點(diǎn)擊返回到第一張圖
for (var i = 0; i < itemList.length; i++) {
itemList[i].style.opacity = 0; //圖片透明度為0圖片隱藏
pagenationList[i].style.background = "white " //圖片沒(méi)顯現(xiàn)小圓點(diǎn)的顏色為白色
}
itemList[index].style.transition = "opacity 1s ease .2s"
itemList[index].style.opacity = 1; //圖片透明度為1圖片顯現(xiàn)
pagenationList[index].style.background = "blue" //圖片顯現(xiàn)小圓點(diǎn)的顏色為藍(lán)色
}
next.onclick = nextFn; //下一張(點(diǎn)擊事件)點(diǎn)擊切換下一張圖片
up.onclick = function () { //上一張(點(diǎn)擊事件)點(diǎn)擊切換上一張圖片
index = index <= 0 ? itemList.length - 1 : --index; //判斷點(diǎn)擊到了第一張圖片再次點(diǎn)擊返回到最后一張圖
for (var i = 0; i < itemList.length; i++) {
itemList[i].style.opacity = 0;
pagenationList[i].style.background = "white"
}
itemList[index].style.transition = "opacity 1s ease .2s" //增加過(guò)渡效果
itemList[index].style.opacity = 1;
pagenationList[index].style.background = "blue"
}
//設(shè)置定時(shí)器,自動(dòng)向下播放圖片
var t1 = setInterval(nextFn, 2000)
banner.onmouseover = function () {
clearInterval(t1);
}
banner.onmouseout = function () {
t1 = setInterval(nextFn, 2000)
}
// 事件委托
pagenation.onclick = function (event) {
event = window.event || event
console.log(event);
if (event.target.className == "pagenation") {
console.log("點(diǎn)擊的是父元素");
} else {
var id = event.target.id;
var photoIndex = null;
for (var i = 0; i < pagenationList.length; i++) {
pagenationList[i].style.background = "white"
if (id.indexOf(i) >= 0) {
photoIndex = i;
}
}
event.target.style.background = "blue"
index = photoIndex; // 將小圓點(diǎn)對(duì)應(yīng)的下標(biāo)與圖片下標(biāo)對(duì)應(yīng)
for (var i = 0; i < itemList.length; i++) {
itemList[i].style.opacity = 0;
}
itemList[index].style.transition = "opacity 1s ease .2s"
itemList[photoIndex].style.opacity = 1;
}
}
</script>
</body>
</html>總結(jié)
到此這篇關(guān)于Html+CSS+JS輪播圖實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Html+CSS+JS輪播圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jquery插件jquery.confirm彈出確認(rèn)消息
這篇文章介紹了插件jquery.confirm彈出確認(rèn)消息的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2015-12-12
JavaScript實(shí)現(xiàn)倒計(jì)時(shí)代碼段Item1(非常實(shí)用)
現(xiàn)今團(tuán)購(gòu)網(wǎng)、電商網(wǎng)、門(mén)戶網(wǎng)等,常使用時(shí)間記錄重要的時(shí)刻,如時(shí)間顯示、倒計(jì)時(shí)差、限時(shí)搶購(gòu)等,本文分析不同倒計(jì)時(shí)效果的計(jì)算思路及方法,掌握日期對(duì)象Date,獲取時(shí)間的方法,計(jì)算時(shí)差的方法,實(shí)現(xiàn)不同的倒時(shí)計(jì)效果2015-11-11
inquirer.js一個(gè)用戶與命令行交互的工具詳解
這篇文章主要介紹了inquirer.js一個(gè)用戶與命令行交互的工具詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
JavaScript數(shù)據(jù)類型區(qū)別及檢測(cè)方法
在JavaScript中,數(shù)據(jù)類型是編程中非常重要的概念,它決定了數(shù)據(jù)的性質(zhì)、如何存儲(chǔ)以及如何操作這些數(shù)據(jù),本文介紹JavaScript數(shù)據(jù)類型區(qū)別及檢測(cè)方法,感興趣的朋友一起看看吧2024-04-04
javascript中關(guān)于&& 和 || 表達(dá)式的小技巧分享
我將會(huì)介紹和解析12個(gè)簡(jiǎn)單但是強(qiáng)大的JavaScript技巧. 這些技巧所有的JavaScript程序員都可以馬上使用, 你不需要成為JavaScript高手才能理解這些.下面我們開(kāi)始本系列的第一篇文章,介紹下強(qiáng)大的&& 和 || 表達(dá)式2015-04-04
JavaScript代碼判斷輸入的字符串是否含有特殊字符和表情代碼實(shí)例
這篇文章主要介紹了JavaScript代碼判斷輸入的字符串是否含有特殊字符和表情,通過(guò)js代碼if語(yǔ)句進(jìn)行判斷,并結(jié)合自己開(kāi)發(fā)的情景,具體操作步驟大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一下。2017-08-08

