WebAPI阶段
DOM树、DOM对象
DOM: Document Object Model
- DOM树
整个HTML网页被视为一个树形结构,
- DOM对象
DOM’操作
- 获取DOM对象
const obj = document.querySelect('CSS 选择器')
const objs = document.querySelectAll('div') // 返回数组
- 修改DOM对象
obj.src = './imgs/1.jpg'
obj.style.color = 'red'
obj.disabled = false
3.为对象增加/删除类
obj.classList.add('className')
obj.classList.remove('className')
事件与事件监听器
- 事件类型
click、mouseenter、mouseleave、keydown、keyup - 事件监听器
obj.addEventListener('click',function(){})
obj.addEventListener('mouseenter',function(){})
典例
- 随机点名
<!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>
</head>
<style>
.box {
text-align: center;
}
</style>
<body>
<div class="box">
<div class="name">
<h3>张飞</h3>
</div>
<button class="start" >开始</button>
<button class="end" disabled>结束</button>
</div>
<script>
const names = ['关羽','张飞','赵云','刘备','孙权']
let start = document.querySelector('.start')
let end = document.querySelector('.end')
let h3 = document.querySelector('h3')
start.addEventListener('click',function(){
n = setInterval(function(){
let i = Math.floor(Math.random()*(names.length))
console.log(i)
h3.innerText = names[i]
},100)
start.disabled = true
end.disabled = false
})
end.addEventListener('click',function(){
clearInterval(n)
start.disabled = false
end.disabled = true
})
</script>
</body>
</html>
- 轮播图
<!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>
</head>
<style>
.slider {
margin: 0 auto;
padding: 0 auto;
width: 560px;
height: 380px;
}
.slider .wrapper img {
width: 100%;
height: 100%;
}
.slider .wrapper {
width: 560px;
height: 300px;
position: relative;
}
.chStytle {
background-color: #000;
height: 30px;
opacity: .5;
color: #fff;
border-radius: 5px;
font-weight: 700;
width: 30px;
cursor: pointer;
}
.slider .wrapper .prev {
position: absolute;
text-align: center;
right: 55px;
bottom: 20px;
}
.slider .wrapper .next {
position: absolute;
text-align: center;
right: 20px;
bottom: 20px;
}
.slider .footer {
width: 560px;
height: 80px;
background-color: rgb(100, 67, 68);
font-family: '微软雅黑';
font-size: 16px;
font-weight: 700;
margin-top: -5px;
position: relative;
}
ul {
/* float: left; */
position: absolute;
list-style: none;
padding: 0 0;
margin: 0 0;
bottom: 8px;
left: 5px;
}
.footer p {
position: absolute;
left: 10px;
top: 5px;
color: #fff;
/* margin:5px auto; */
}
.footer ul li {
float: left;
width: 8px;
height: 8px;
background-color: #fff;
margin: 10px 4px;
border-radius: 4px;
opacity: .4;
cursor: pointer;
}
.footer ul li.active {
width: 10px;
height: 10px;
opacity: 1;
}
</style>
<body>
<div class="slider">
<div class="wrapper">
<img src="./images/slider01.jpg" alt="">
<span class="prev chStytle"><</span>
<span class="next chStytle">></span>
</div>
<div class="footer">
<p>对人类来说会不会太超前了?</p>
<ul>
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
<script>
const sliderData = [
{ url: './images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
{ url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
{ url: './images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
{ url: './images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
{ url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
{ url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
{ url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },
{ url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },
]
i = 0;
const img = document.querySelector('.slider .wrapper img')
const p = document.querySelector('.footer p')
const footer = document.querySelector('.slider .footer')
const next = document.querySelector('.slider .wrapper .next')
const prev = document.querySelector('.slider .wrapper .prev')
const slider = document.querySelector('.slider')
n = setInterval(function () {
next.click()
}, 1000)
next.addEventListener('click',function(){
i++;
if (i == sliderData.length) i = 0;
img.src = sliderData[i].url
p.innerHTML = sliderData[i].title
footer.style.backgroundColor = sliderData[i].color
// 设置小圆点
document.querySelector(' .footer ul li.active ').classList.remove('active')
document.querySelector(`.footer ul li:nth-child(${i + 1})`).classList.add('active')
})
prev.addEventListener('click',function(){
i--;
if (i == -1) i = sliderData.length - 1;
img.src = sliderData[i].url
p.innerHTML = sliderData[i].title
footer.style.backgroundColor = sliderData[i].color
// 设置小圆点
document.querySelector(' .footer ul li.active ').classList.remove('active')
document.querySelector(`.footer ul li:nth-child(${i + 1})`).classList.add('active')
})
slider.addEventListener('mouseleave', function () {
n = setInterval(function () {
next.click()
}, 1000)
})
slider.addEventListener('mouseenter', function () {
if(n)
clearInterval(n)
})
</script>
</body>
</html>