1. 事件监听
1.1 语法
1.2 关闭广告案列
<body>
<div class="box">
欢迎来到ddw的团队
<div class="close">关闭x</div>
</div>
<script>
const clo = document.querySelector('.close')
const box = document.querySelector('.box')
clo.addEventListener('click',function(){
box.style.display = 'none'
})
</script>
</body>
1.3 随机点名案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
h2 {
text-align: center;
}
.box {
width: 600px;
margin: 50px auto;
display: flex;
font-size: 25px;
line-height: 40px;
}
.qs {
width: 450px;
height: 40px;
color: red;
}
.btns {
text-align: center;
}
.btns button {
width: 120px;
height: 35px;
margin: 0 50px;
}
</style>
</head>
<body>
<h2>随机点名</h2>
<div class="box">
<span>名字是:</span>
<div class="qs">这里显示姓名</div>
</div>
<div class="btns">
<button class="start">开始</button>
<button class="end">结束</button>
</div>
<script>
// 数据数组
const arr = ['马超', '黄忠', '赵云', '关羽', '张飞']
// 由于局部变量下面会无法获取计时器id,所以要设置全局变量
let timerId = 0
let r = 0
// 1.获取两个按钮对象和要修改文字的对象
const start = document.querySelector('.start')
const end = document.querySelector('.end')
const qs = document.querySelector('.qs')
// 2.设置开始按钮事件
start.addEventListener('click',function(){
// 使用定时器让名字随机展示
timerId = setInterval(function(){
r = Math.floor(Math.random()*arr.length)
qs.innerHTML = arr[r]
},40)
})
// 点击end之后首先要关闭定时器,然后删除选中的这个元素
end.addEventListener('click',function(){
// 关闭定时器
clearInterval(timerId)
arr.splice(r,1)
console.log(arr);
// 如果只剩下一个用户了,就可以把两个按钮禁用了
if(arr.length === 0){
start.disabled = end.disabled = true
}
})
</script>
</body>
</html>
1.4 事件监听版本
2.事件类型
2.1 焦点事件--小米搜索框
<script>
// 1. 获取对象
const input = document.querySelector('input')
const rl = document.querySelector('.result-list')
// 2. 设置监听事件
input.addEventListener('focus',function(){
rl.style.display = 'block'
// 添加一个带有边框颜色的类名
input.classList.add('search')
})
// 3.失去焦点
input.addEventListener('blur',function(){
rl.style.display = 'none'
input.classList.remove('search')
})
</script>
<script>
const div = document.querySelector('div')
// 鼠标经过事件
div.addEventListener('mouseenter',function(){
console.log(`你才不是可爱莎莎`)
})
// 鼠标离开事件
div.addEventListener('mouseleave',function(){
console.log(`你走啦`)
})
</script>
2.2 键盘事件
<script>
const input = document.querySelector('input')
input.addEventListener('keydown',function(){
console.log('键盘按下了');
})
input.addEventListener('keyup',function(){
console.log('键盘弹起了');
})
</script>
2.3 文本事件
- 输入文本显示字数案例
<!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>评论回车发布</title>
<style>
.wrapper {
min-width: 400px;
max-width: 800px;
display: flex;
justify-content: flex-end;
}
.avatar {
width: 48px;
height: 48px;
border-radius: 50%;
overflow: hidden;
background: url(./images/avatar.jpg) no-repeat center / cover;
margin-right: 20px;
}
.wrapper textarea {
outline: none;
border-color: transparent;
resize: none;
background: #f5f5f5;
border-radius: 4px;
flex: 1;
padding: 10px;
transition: all 0.5s;
height: 30px;
}
.wrapper textarea:focus {
border-color: #e4e4e4;
background: #fff;
height: 50px;
}
.wrapper button {
background: #00aeec;
color: #fff;
border: none;
border-radius: 4px;
margin-left: 10px;
width: 70px;
cursor: pointer;
}
.wrapper .total {
margin-right: 80px;
color: #999;
margin-top: 5px;
opacity: 0;
transition: all 0.5s;
}
.list {
min-width: 400px;
max-width: 800px;
display: flex;
}
.list .item {
width: 100%;
display: flex;
}
.list .item .info {
flex: 1;
border-bottom: 1px dashed #e4e4e4;
padding-bottom: 10px;
}
.list .item p {
margin: 0;
}
.list .item .name {
color: #FB7299;
font-size: 14px;
font-weight: bold;
}
.list .item .text {
color: #333;
padding: 10px 0;
}
.list .item .time {
color: #999;
font-size: 12px;
}
</style>
</head>
<body>
<div class="wrapper">
<i class="avatar"></i>
<textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
<button>发布</button>
</div>
<div class="wrapper">
<span class="total">0/200字</span>
</div>
<div class="list">
<div class="item" style="display: none;">
<i class="avatar"></i>
<div class="info">
<p class="name">清风徐来</p>
<p class="text">大家都辛苦啦,感谢各位大大的努力,能圆满完成真是太好了[笑哭][支持]</p>
<p class="time">2022-10-10 20:29:21</p>
</div>
</div>
</div>
<script>
// 第一步:实现鼠标点击时显示字数框,失去焦点时不显示
// 1. 获取对象
const tx = document.querySelector('#tx')
const total = document.querySelector('.total')
// 2. 设置焦点事件,文本域获得焦点,让total显示出来
tx.addEventListener('focus',function(){
total.style.opacity = 1
})
tx.addEventListener('blur',function(){
total.style.opacity = 0
})
// 第二步:搞一个输入事件,来获取用户输入的字符的长度
tx.addEventListener('input',function(){
let len = tx.value.length
total.innerHTML = `${len}/200字`
})
</script>
</body>
</html>
3.综合案例-完整轮播图
<!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>轮播图点击切换</title>
<style>
* {
box-sizing: border-box;
}
.slider {
width: 560px;
height: 400px;
overflow: hidden;
}
.slider-wrapper {
width: 100%;
height: 320px;
}
.slider-wrapper img {
width: 100%;
height: 100%;
display: block;
}
.slider-footer {
height: 80px;
background-color: rgb(100, 67, 68);
padding: 12px 12px 0 12px;
position: relative;
}
.slider-footer .toggle {
position: absolute;
right: 0;
top: 12px;
display: flex;
}
.slider-footer .toggle button {
margin-right: 12px;
width: 28px;
height: 28px;
appearance: none;
border: none;
background: rgba(255, 255, 255, 0.1);
color: #fff;
border-radius: 4px;
cursor: pointer;
}
.slider-footer .toggle button:hover {
background: rgba(255, 255, 255, 0.2);
}
.slider-footer p {
margin: 0;
color: #fff;
font-size: 18px;
margin-bottom: 10px;
}
.slider-indicator {
margin: 0;
padding: 0;
list-style: none;
display: flex;
align-items: center;
}
.slider-indicator li {
width: 8px;
height: 8px;
margin: 4px;
border-radius: 50%;
background: #fff;
opacity: 0.4;
cursor: pointer;
}
.slider-indicator li.active {
width: 12px;
height: 12px;
opacity: 1;
}
</style>
</head>
<body>
<div class="slider">
<div class="slider-wrapper">
<img src="./images/slider01.jpg" alt="" />
</div>
<div class="slider-footer">
<p>对人类来说会不会太超前了?</p>
<ul class="slider-indicator">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="toggle">
<button class="prev"><</button>
<button class="next">></button>
</div>
</div>
</div>
<script>
// 1. 初始数据
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)' },
]
// 2.设置一个变量number代表是第几个图片,同理这个计时器也要搞一个全局变量
let number = 0
let n = 0
//3. 搞一个定时器让这些图片轮流播放
// 获取图片对象和文字标签
const img = document.querySelector('img')
const p = document.querySelector('p')
// 背景也要切换
const footer = document.querySelector('.slider-footer')
function change(){
number = number % 8
img.src = sliderData[number].url
p.innerHTML = sliderData[number].title
footer.style.backgroundColor = sliderData[number].color
// 添加小圆点,先移除原来的类名,li再添加
document.querySelector('.slider-indicator .active').classList.remove('active')
document.querySelector(`.slider-indicator li:nth-child(${number+1})`).classList.add('active')
}
// 设置一个计时器,如果用户啥都不干那就让它自己播放吧
function display(){
n= setInterval(function(){
change()
number++
},500)
}
display()
// 老板说用户鼠标在图片上的时候,不能来回切换,那就把计时器关了呗
// 4.获取这个图片所在的div
const div = document.querySelector('div')
// 设置一个事件,类型就是鼠标进入退出
div.addEventListener('mouseenter',function(){
clearInterval(n)
})
// 用户退出之后就开始计数器自动播放呗
div.addEventListener('mouseleave',function(){
display()
})
// 修改按钮,鼠标进来了,点击下一个就要下一个
// 先获取按钮
const prev = document.querySelector('.prev')
const next = document.querySelector('.next')
// 设计点击属性事件呗
prev.addEventListener('click',function(){
// 特殊情况,就是在最左边的时候,但是change函数会执行%8,很安全
// if(number === 0){
// number = 7
// }
change()
number--
})
next.addEventListener('click',function(){
change()
number++
})
</script>
</body>
</html>
4.事件对象
4.1 获取事件对象
- 事件对象里面的元素非常多,比如pageX,pageY等等
4.2 事件对象常见的属性
<script>
// 需求:按下回车键才代表发布
const input = document.querySelector('input')
input.addEventListener('keyup',function(e){
if(e.key === 'Enter'){
console.log('发布成功')
}
})
</script>
4.3 评论回车发布
输入空格后在输入文字可以用trim方法去除空格
点完回车发布完评论就应该立即清空文本框内容并且字数恢复成0
<script>
// 第一步:实现鼠标点击时显示字数框,失去焦点时不显示
// 1. 获取对象
const tx = document.querySelector('#tx')
const total = document.querySelector('.total')
const item = document.querySelector('.item')
const text = document.querySelector('.info .text')
// 2. 设置焦点事件,文本域获得焦点,让total显示出来
tx.addEventListener('focus',function(){
total.style.opacity = 1
})
tx.addEventListener('blur',function(){
total.style.opacity = 0
})
// 第二步:搞一个输入事件,来获取用户输入的字符的长度
tx.addEventListener('input',function(){
let len = tx.value.length
total.innerHTML = `${len}/200字`
})
// 第三步:用户点了回车之后,要把输入的文字内容显示出来
tx.addEventListener('keyup',function(e){
if(e.key === 'Enter'){
if(tx.value.trim() !== ''){ // 去除完空格不等于空的时候,执行语句
// 将item的display改成显示的
item.style.display = 'block'
// 将文字改成用户输入的文字
text.innerHTML = tx.value
}
// 用户按下回车之后,要清空,字数也要复原
tx.value = ''
total.innerHTML = `0/200字`
}
})
</script>