0 前言
记录前端学习~
1 随机点名案例
目标样式
分析:
- 点击开始按钮随机抽取数组里的一个数据,放在页面中
- 点击结束按钮删除数组当前抽取的数据
- 当抽取到最后一个数据时,两个按钮同时禁用
【核心】利用定时器快速展示,停止定时器结束展示
代码
<!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 = ['马超', '黄忠', '赵云', '关羽', '张飞']
//开始
const start = document.querySelector('.start')
const qs = document.querySelector('.qs')
let timeId = 0
let random = 0
start.addEventListener('click', function () {
timeId = setInterval(function () {
random = parseInt(Math.random() * arr.length)
qs.innerHTML = arr[random]
}, 35)
if (arr.length === 1) {
start.disabled = true
end.disabled = true
}
})
//结束
const end = document.querySelector('.end')
end.addEventListener('click', function () {
clearInterval(timeId)
//删除抽到的元素
arr.splice(random, 1)
})
</script>
</body>
</html>
2 轮播图终版
目标样式
需求:当点击左右的按钮,可以切换轮播图
分析:
- 右侧按钮点击,变量++,如果大于等于8复原为0
- 左侧按钮点击,变量–,如果小于0,则复原最后一张
- 鼠标经过暂停定时器
- 鼠标离开开启定时器
代码
<!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 data = [
{ 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)' },
]
//信号量 控制播放图片张数
let i = 0
const slider = document.querySelector('.slider')
const next = document.querySelector('.next')
const prev = document.querySelector('.prev')
const img = document.querySelector('.slider-wrapper img')
const p = document.querySelector('.slider-footer p')
const footer = document.querySelector('.slider-footer')
//右侧按钮
//注册点击事件
next.addEventListener('click', function () {
i++
i = i >= data.length ? 0 : i
// console.log(data[i])
toggle()
})
//左侧按钮
prev.addEventListener('click', function () {
i--
i = i < 0 ? data.length-1 : i
// console.log(data[i])
toggle()
})
//自动播放-定时器
let timeId = setInterval(function () {
//利用js自动调用点击事件
next.click()
}, 1000)
//鼠标经过盒子,停止定时器,离开打开定时器
slider.addEventListener('mouseenter', function () {
clearInterval(timeId)
})
slider.addEventListener('mouseleave', function () {
clearInterval(timeId)
timeId = setInterval(function () {
//利用js自动调用点击事件
next.click()
}, 1000)
})
// 声明渲染函数
function toggle() {
img.src = data[i].url
p.innerHTML = data[i].title
footer.style.backgroundColor = data[i].color
// 更换小圆点 先移除原有类名 再在当前li添加这个类名
document.querySelector('.slider-indicator .active').classList.remove('active')
document.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active')
}
</script>
</body>
</html>
3 发布评论案例
目标样式
需求:用户输入文字,可以计算用户输入的字数,并且按下回车和发布都可以发布信息
分析:
- 判断输入事件 input
- 不断取得文本框内的字符长度,文本域.value.length
- 把获得的数字返回给下面的文本框
- 用keyup键盘事件
- 如果用户按下回车则发布信息
- 让留言信息模块显示,并把数据渲染到对应标签内部
代码
<!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>
const tx = document.querySelector('#tx')
const total = document.querySelector('.total')
const item = document.querySelector('.item')
const text = document.querySelector('.text')
// 文本域获得焦点,让total显示
tx.addEventListener('focus', function () {
//opacity透明度
total.style.opacity = 1
})
tx.addEventListener('blur', function () {
total.style.opacity = 0
})
//检测用户输入
tx.addEventListener('input', function () {
total.innerHTML = `${tx.value.length}/200字`
})
//回车发布评论
tx.addEventListener('keyup', function (e) {
if (e.key === 'Enter') {
if (tx.value.trim()) {
item.style.display = 'block'
text.innerHTML = tx.value
}
//清空文本域
tx.value = ''
total.innerHTML = `${tx.value.length}/200字`
}
})
</script>
</body>
</html>
总结
- 如果想要使用某个函数内定义的变量,需要将变量设置为全局变量
- random = parseInt(Math.random() * arr.length) 可以利用parseInt生成随机整数
- focus伪类选择器:css光标移动到文本框上,文本框会变尺寸
- 利用opacity 属性指定了一个元素的不透明度,焦点事件发生切换时会产生渐变效果
- trim方法:去除字符串左右两侧空格
- next.click() 直接利用js自动调用点击事件,产生点击next的效果