1.使用css樣式來進(jìn)行激活導(dǎo)航欄、顯示列表樣式
2.使用html data-*屬性建立每一個導(dǎo)航欄與每一個顯示列表相同的索引,每當(dāng)點(diǎn)擊導(dǎo)航欄時就會激活相同索引的顯示列表
3.對導(dǎo)航欄使用事件委托
<!DOCTYPE html>
<html lang="zh_hans">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.box {
width: 300px;
height: 340px;
}
.nav-box {
height: 40px;
background-color: lightsalmon;
}
.nav-box ul {
list-style: none;
display: flex;
justify-content: space-between;
}
.nav-box ul li {
width: 75px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
}
.list-box {
height: 300px;
background-color: lightcoral;
}
/*導(dǎo)航欄的激活樣式*/
.u01 .active {
background-color: yellow;
}
/*顯示區(qū)塊默認(rèn)都是隱藏的*/
.d {
display: none;
}
/*顯示區(qū)塊的激活樣式*/
.list-box .d.active {
display: block;
}
</style>
</head>
<body>
<div class="box">
<div class="nav-box">
<ul class="u01">
<!--第一個導(dǎo)航列表默認(rèn)是激活的-->
<li class="active" data-index="1">家具</li>
<li data-index="2">家電</li>
<li data-index="3">電腦</li>
<li data-index="4">手機(jī)</li>
</ul>
</div>
<div class="list-box">
<!--第一個顯示區(qū)塊默認(rèn)是激活的-->
<div class="d active" data-index="1">家具內(nèi)容</div>
<div class="d" data-index="2">家電內(nèi)容</div>
<div class="d" data-index="3">電腦內(nèi)容</div>
<div class="d" data-index="4">手機(jī)內(nèi)容</div>
</div>
</div>
</body>
<script>
// 1. 獲取導(dǎo)航欄:利用事件委托觸發(fā)子節(jié)點(diǎn)的onclick事件
var u01 = document.getElementsByClassName("u01")[0];
// 2. 獲取顯示區(qū)塊
var div = document.getElementsByClassName("d");
// 3. 給導(dǎo)航欄添加click事件:每當(dāng)點(diǎn)擊導(dǎo)航欄中的每一個列表項(xiàng)時顯示對應(yīng)的區(qū)塊
u01.addEventListener("click", show, false);
// 4. 給導(dǎo)航欄添加mouseover事件:每當(dāng)點(diǎn)擊導(dǎo)航欄中的每一個列表項(xiàng)時顯示對應(yīng)的區(qū)塊
u01.addEventListener("mouseover", show, false);
//事件回調(diào)函數(shù)
function show(ev) {
// 1. 每當(dāng)點(diǎn)擊時先清空導(dǎo)航欄原有激活樣式
Array.from(ev.target.parentNode.children).forEach(function (item) {
item.classList.remove("active");
});
// 2. 再對點(diǎn)擊的節(jié)點(diǎn)激活樣式
ev.target.classList.toggle("active");
// 3. 導(dǎo)航欄之后再清空顯示區(qū)塊的原有激活樣式
Array.from(div).forEach(function (item) {
item.classList.remove("active");
});
// 4. 在顯示區(qū)塊中查詢data-index與導(dǎo)航欄的data-index相等的內(nèi)容,將它設(shè)置為激活
Array.from(div).forEach(function (item) {
if (item.dataset.index === ev.target.dataset.index) {
item.classList.add("active");
}
});
}
</script>
</html>
1.原點(diǎn)隨圖片數(shù)量動態(tài)生成
2.使用css樣式來進(jìn)行激活原點(diǎn)、顯示對應(yīng)圖片樣式
3.使用html data-*屬性建立每一個原點(diǎn)與每一個顯示圖片相同的索引,每當(dāng)點(diǎn)擊原點(diǎn)時就會激活相同索引的顯示圖片
4.使用事件委托機(jī)制為每一個小圓點(diǎn)添加點(diǎn)擊事件
5.翻頁按鈕利用觸發(fā)激活原點(diǎn)進(jìn)行翻頁的機(jī)制
6.使用定時器實(shí)現(xiàn)移入移出輪播圖區(qū)域暫停和繼續(xù)翻頁效果
<!DOCTYPE html>
<html lang="zh_hans">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>輪播圖</title>
<style>
/*父容器屬性*/
.car-box {
position: relative;
margin: 0 auto;
width: 1000px;
height: 350px;
}
.car-box .slider {
width: 1000px;
height: 350px;
display: none;
}
.car-box .slider.active {
display: block;
}
/*原點(diǎn)列表樣式*/
.car-box .point-list {
position: absolute;
left: 50%;
margin-left: -38px;
top: 310px;
}
.car-box .point-list .point {
display: inline-block;
width: 12px;
height: 12px;
margin: 0 5px;
background-color: white;
border-radius: 100%;
}
.car-box .point-list .point:hover {
cursor: pointer;
}
.car-box .point-list .point.active {
background-color: black;
}
/*跳轉(zhuǎn)按鈕樣式*/
.skip {
position: absolute;
top: 140px;
display: inline-block;
width: 40px;
height: 80px;
text-align: center;
line-height: 80px;
background-color: lightgray;
color: white;
opacity: 0.2;
font-size: 36px;
}
.car-box .prev {
left: 0;
}
.car-box .next {
right: 0;
}
.car-box .skip:hover {
cursor: pointer;
opacity: 0.5;
color: black;
}
</style>
</head>
<body>
<div class="car-box">
<!--圖片-->
<img
src="./images/banner1.jpg"
alt=""
class="slider active"
data-index="1"
/>
<img src="./images/banner2.jpg" alt="" class="slider" data-index="2" />
<img src="./images/banner3.jpg" alt="" class="slider" data-index="3" />
<img src="./images/banner4.jpg" alt="" class="slider" data-index="4" />
<!--原點(diǎn)列表-->
<div class="point-list">
<!--應(yīng)該隨圖片數(shù)量動態(tài)生成
<span class="point active" data-index="1"></span>
<span class="point" data-index="2"></span>
<span class="point" data-index="3"></span>
<span class="point" data-index="4"></span>-->
</div>
<!--跳轉(zhuǎn)按鈕-->
<span class="skip prev"><</span>
<span class="skip next">></span>
</div>
</body>
<script>
//獲取圖片
var imgs = document.querySelectorAll(".slider");
//獲取原點(diǎn)列表
var plist = document.querySelector(".point-list");
//動態(tài)生成原點(diǎn)
imgs.forEach(function (item, index) {
var span = document.createElement("span");
span.classList.add("point");
//第一個默認(rèn)是激活樣式
if (index === 0) {
span.classList.add("active");
}
//添加索引,和圖片索引進(jìn)行關(guān)聯(lián)
span.dataset.index = item.dataset.index;
plist.appendChild(span);
});
//獲取所有小圓點(diǎn)
var point = document.querySelectorAll(".point");
//使用事件委托為每一個小圓點(diǎn)添加點(diǎn)擊事件
plist.addEventListener(
"click",
function (ev) {
//如果點(diǎn)擊的小圓點(diǎn)的索引值和圖片的索引值一樣則激活樣式
imgs.forEach(function (item) {
if (item.dataset.index === ev.target.dataset.index) {
//清除圖片原有激活樣式
imgs.forEach(function (item) {
item.classList.remove("active");
});
//設(shè)置圖片激活
item.classList.add("active");
//清除小圓點(diǎn)原有激活樣式和設(shè)置小圓點(diǎn)激活
setPorintActive(item.dataset.index);
}
});
},
false
);
//公共函數(shù):設(shè)置小圓點(diǎn)激活
function setPorintActive(imgIndex) {
//清除小圓點(diǎn)原有激活樣式
point.forEach(function (item) {
item.classList.remove("active");
});
//設(shè)置小圓點(diǎn)激活
point.forEach(function (item) {
if (item.dataset.index === imgIndex) {
item.classList.add("active");
}
});
}
//獲取翻頁按鈕
var skips = document.querySelectorAll(".skip");
//給翻頁按鈕添加點(diǎn)擊事件
skips.item(0).addEventListener("click", skipImg, false);
skips.item(1).addEventListener("click", skipImg, false);
//翻頁函數(shù)
function skipImg(ev) {
// 1. 找到當(dāng)前正在顯示的圖片
var currentImg = null;
imgs.forEach(function (img) {
if (img.classList.contains("active")) {
currentImg = img;
}
});
// 2. 判斷是否點(diǎn)擊了前一頁
if (ev.target.classList.contains("prev")) {
// 1. 先清空當(dāng)前圖片的激活樣式
currentImg.classList.remove("active");
// 2. 再把當(dāng)前頁設(shè)置為前一頁
currentImg = currentImg.previousElementSibling;
// 3. 把當(dāng)前頁激活,為防止越界,先判斷前一頁存不存在
if (currentImg !== null && currentImg.nodeName === "IMG") {
//存在則激活
currentImg.classList.add("active");
} else {
//不存在則直接跳轉(zhuǎn)到最后一頁
currentImg = imgs[imgs.length - 1];
currentImg.classList.add("active");
}
}
// 3. 判斷是否點(diǎn)擊了后一頁
if (ev.target.classList.contains("next")) {
// 1. 先清空當(dāng)前圖片的激活樣式
currentImg.classList.remove("active");
// 2. 再把當(dāng)前頁設(shè)置為后一頁
currentImg = currentImg.nextElementSibling;
// 3. 把當(dāng)前頁激活,為防止越界,先判斷后一頁存不存在
if (currentImg !== null && currentImg.nodeName === "IMG") {
//存在則激活
currentImg.classList.add("active");
} else {
//不存在則直接跳轉(zhuǎn)到第一頁
currentImg = imgs[0];
currentImg.classList.add("active");
}
}
// 4. 關(guān)聯(lián)小圓點(diǎn)高亮
setPorintActive(currentImg.dataset.index);
}
//獲取輪播圖容器
var box = document.querySelector(".car-box");
//定義一個定時器,用來清空定時器
var timer = null;
// 1. 當(dāng)鼠標(biāo)移出輪播圖區(qū)域時,啟動定時器
box.addEventListener("mouseout", startTimer, false);
// 2. 當(dāng)鼠標(biāo)移入輪播圖區(qū)域時,清空定時器
box.addEventListener("mouseover", clearTimer, false);
//啟動定時器函數(shù)
function startTimer() {
//定義一個點(diǎn)擊事件
var click = new Event("click");
//間歇式執(zhí)行,間隔為2秒
setInterval(function () {
//使用事件派發(fā)向翻頁按鈕派發(fā)點(diǎn)擊事件,觸發(fā)翻頁函數(shù)
skips.item(1).dispatchEvent(click);
}, 2000);
}
//清除定時器函數(shù)
function clearTimer() {
clearInterval(timer);
}
</script>
</html>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號