案例-轮播图点击切换
需求:当点击左右的按钮,可以切换轮播图
分析:
①右侧按钮点击,变量++,如果大于等于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>
ul>
<div class="toggle">
<button class="prev"><button>
<button class="next">>button>
div>
div>
div>
<script>
// 1.初始数据
const data = [
{ url: 'imgs/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
{ url: 'imgs/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
{ url: 'imgs/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
{ url: 'imgs/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
]
//获取元素
const img = document.querySelector('.slider-wrapper img')
const p = document.querySelector('.slider-footer p')
const footer = document.querySelector('.slider-footer')
//2.右侧按钮
//2.1 获取右侧按钮
const next = document.querySelector('.next')
let i = 0
//2.2 注册点击事件
next.addEventListener('click', function () {
i++
if (i >= data.length) {
i = 0
}
commom()
})
//3.左侧按钮
//3.1 获取左侧按钮
const prev = document.querySelector('.prev')
//3.2 注册点击事件
prev.addEventListener('click', function () {
i--
if (i < 0) {
i = data.length - 1
}
commom()
})
//4.声明一个渲染函数作为复用
function commom() {
//2.3 渲染对应的数据
img.src = data[i].url
p.innerHTML = data[i].title
footer.style.backgroundColor = data[i].color
//2.4 更换小圆点 先移除原来的类名,再给当前的li添加active类
document.querySelector('.slider-indicator .active').classList.remove('active')
document.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active')
}
//5.自动播放模块
let timeId = setInterval(function () {
//利用js自动调用点击事件 click() !
next.click() //click()是一个方法
}, 1000)
// 6.鼠标经过 暂停定时器
const slider = document.querySelector('.slider')
slider.addEventListener('mouseenter', function () {
clearInterval(timeId)
})
// 7.鼠标离开 开启定时器
slider.addEventListener('mouseleave', function () {
//先关闭定时器
clearInterval(timeId)
//再打开
timeId = setInterval(function () {
//利用js自动调用点击事件 click() !
next.click()
}, 1000)
})
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>Documenttitle>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul {
list-style: none;
}
.mi {
position: relative;
width: 223px;
margin: 100px auto;
}
.mi input {
width: 223px;
height: 48px;
padding: 0 10px;
font-size: 14px;
line-height: 48px;
border: 1px solid #e0e0e0;
outline: none;
}
.mi .search {
border: 1px solid #ff6700;
}
.result-list {
display: none;
position: absolute;
left: 0;
top: 48px;
width: 223px;
border: 1px solid #ff6700;
border-top: 0;
background: #fff;
}
.result-list a {
display: block;
padding: 6px 15px;
font-size: 12px;
color: #424242;
text-decoration: none;
}
.result-list a:hover {
background-color: #eee;
}
style>
head>
<body>
<div class="mi">
<input type="search" placeholder="小米笔记本">
<ul class="result-list">
<li><a href="#">全部商品a>li>
<li><a href="#">小米11a>li>
<li><a href="#">小米10Sa>li>
<li><a href="#">小米笔记本a>li>
<li><a href="#">小米手机a>li>
<li><a href="#">黑鲨4a>li>
<li><a href="#">空调a>li>
ul>
div>
<script>
const input = document.querySelector('input')
// const input = document.querySelector('[type=search]')
const list = document.querySelector('.result-list')
//获得焦点
input.addEventListener('focus', function () {
list.style.display = 'block'
//添加一个带有颜色边框的类名
input.classList.add('search')
})
//失去焦点
input.addEventListener('blur', function () {
list.style.display = 'none'
input.classList.remove('search')
})
script>
body>
html>
【示例代码】
<body>
<input type="text">
<script>
const inp = document.querySelector('input')
inp.addEventListener('input', function () {
console.log(inp.value) //获取用户输入的内容
})
script>
body>
案例-评论字数统计
需求:用户输入文字,可以计算用户输入的字数.
分析:
①判断用输入事件input
②不断取得文本框里面的字符长度,文本域.value.length
③把获得数字给下面文本框
【示例代码】
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:21p>
div>
div>
div>
<script>
const tx = document.querySelector('#tx')
const total = document.querySelector('.total')
//1.当文本域获得了焦点,就让 total 显示出来
tx.addEventListener('focus', function () {
total.style.opacity = 1
})
//2.当文本域失去焦点,就让 total 隐藏起来
tx.addEventListener('blur', function () {
total.style.opacity = 0
})
// 3.用户输入
tx.addEventListener('input', function () {
total.innerHTML = `${tx.value.length}/200字`
})
script>
body>
html>