
批改狀態(tài):合格
老師批語:
<html lang="zh-CN">
<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>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="slideshow">
<div class="imgs">
</div>
<div class="btns">
</div>
</div>
<script type="module">
import slide from './js/slideshow.js'
const imgs = document.querySelector('.imgs')
const btns = document.querySelector('.btns')
window.onload= function(){
//創(chuàng)建圖片
slide.createImgs(imgs)
//創(chuàng)建按鈕
slide.createButts(imgs,btns)
//為每個按鈕添加點擊事件
;[...btns.children].forEach(btn=>{
btn.onclick = function(){
slide.switchImg(btn,imgs)
}
}
)
// 間歇式定時器,第2秒換一張圖片
setInterval(
function (btnsArr,btnKeys){
slide.timePlay(btnsArr,btnKeys)
},
3000
,
//獲取按鈕組
[...btns.children]
,
//獲取按鈕組鍵值
Object.keys([...btns.children])
)
}
</script>
</body>
</html>
export default [
{
key: 1,
src: 'images/item1.jpeg',
url: 'http://ipnx.cn',
},
{
key: 2,
src: 'images/item2.jpeg',
url: 'http://ipnx.cn',
},
{
key: 3,
src: 'images/item3.jpeg',
url: 'http://ipnx.cn',
},
]
------------------------------------------------------
import imgArr from './data.js'
/**
* 創(chuàng)建圖片元素
* @param {DOMElement} imgs --圖片容器
*/
function createImgs(imgs){
const frag = new DocumentFragment()
for(let i=0 ; i<imgArr.length ; i++){
const img =document.createElement('img')
img.src=imgArr[i].src
img.dataset.key = imgArr[i].key
if(i===0)img.classList.add('active')
img.onclick = () => (location.href = imgArr[i].url)
frag.append(img)
}
imgs.append(frag)
}
/**
* @desc 創(chuàng)建按鈕組
* @param {DOMElement} imgs - 圖片容器
* @param {DOMElement} btns - 按鈕容器
*/
function createButts(imgs,btns){
//獲取圖片數(shù)量
let length = imgs.childElementCount
console.log(length);
for(let i=0 ; i<length; i++){
const btn = document.createElement('span')
btn.dataset.key = imgs.children[i].dataset.key
if(i===0)btn.classList.add('active')
btns.append(btn)
}
}
/**
* @desc 創(chuàng)建按鈕事件
* @param {DOMElement} btn - 當(dāng)前按鈕
* @param {DOMElement} imgs - 圖片容器
*/
function switchImg(btn,imgs){
// 去掉所有按鈕圖片的激活狀態(tài)
;[...btn.parentNode.children].forEach(btn=>btn.classList.remove('active'))
;[...imgs.children].forEach(img=>img.classList.remove('active'))
//將當(dāng)前用戶正在點擊的按鈕設(shè)置激活狀態(tài)
btn.classList.add('active')
//獲取當(dāng)前用戶點擊的圖片
const curImg = [...imgs.children].find(img=>img.dataset.key===btn.dataset.key)
//把當(dāng)前圖片設(shè)置激活狀態(tài)
curImg.classList.add('active')
}
/**
* @desc 定時輪播器: 間歇式的定時器
* @param {DOMElement} btnsArr - 按鈕數(shù)組(用來綁定事件)
* @param {DOMElement} btnKeys - 按鈕的鍵構(gòu)成的數(shù)組
*/
function timePlay(btnsArr,btnKeys){
let k = btnKeys.shift()
// 根據(jù)索引,從按鈕組中找到與該索引對應(yīng)的按鈕,給它自動派發(fā)一個點擊事件
btnsArr[k].dispatchEvent(new Event('click'))
btnKeys.push(k)
}
export default {createImgs,createButts,switchImg,timePlay}
body {
background-color: #eee;
}
/* 輪播圖容器 */
.slideshow {
width: 240px;
height: 360px;
/* em / rem */
}
/* 圖片容器 */
.slideshow .imgs {
width: inherit;
height: inherit;
/* width: 100%;
height: 100%; */
}
/* 圖片適應(yīng) */
.slideshow img {
width: 100%;
height: 100%;
border-radius: 10px;
/* ? 默認(rèn)全隱藏 */
display: none;
}
/* 設(shè)置圖片的激活狀態(tài) */
.slideshow img.active {
display: block;
}
.slideshow img:hover {
cursor: pointer;
}
/* ------ 按鈕容器 ------- */
/* 按鈕容器 */
.slideshow .btns {
display: flex;
place-content: center;
position: relative;
top: -40px;
/* transform: translateY(-40px); */
}
.slideshow .btns > span {
background-color: rgba(48, 148, 48, 0.8);
height: 16px;
width: 16px;
border-radius: 50%;
margin: 5px;
}
.slideshow .btns > span.active {
background-color:crimson;
}
.slideshow .btns > span:hover {
cursor: pointer;
}
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號