案例
倒计时
<!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>
<body>
<textarea name="" id="" cols="30" rows="10">
用户注册协议
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quibusdam, voluptate. Dignissimos harum error magni beatae, itaque delectus! Tempora impedit dolor deleniti! Totam aliquid repellendus asperiores hic obcaecati? Doloremque, debitis autem?
</textarea>
<br>
<button class="btn" disabled>已阅读协议(6)
</button>
<script>
let btn = document.querySelector('.btn')
let i = 6
let timer = setInterval(function(){
i--
btn.innerHTML = `已阅读协议(${i})`
if (i === 0){
clearInterval(timer)
btn.disabled = false
btn.innerHTML = `已阅读协议`
}
},1000)
</script>
</body>
</html>
焦点图
Document
1111
随机抽人
<!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>
div{
width: 400px;
height: 200px;
border: 2px solid black;
margin: 0 auto;
}
h2{
text-align: center;
}
span{
text-align: center;
display: block;
line-height: 50px;
}
.start{
margin-left: 50px;
}
.end{
float: right;
margin-right: 50px;
}
</style>
</head>
<body>
<div>
<h2>随机抽人</h2>
<span>1</span>
<button class="start">开始</button>
<button class="end">结束</button>
</div>
<script>
let name = document.querySelector('span')
let start = document.querySelector('.start')
let end = document.querySelector('.end')
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let data = ['刻晴','香菱', '凝光','甘雨','钟离']
let timer = 0
let random = 0
start.addEventListener('click',function(){
timer = setInterval (function(){
random = getRandomIntInclusive(0,data.length - 1)
name.innerHTML = data[random]
},100)
start.disabled = true
end.disabled = false
if(data.length === 1){
clearInterval(timer)
start.disabled = true
end.disabled = true
}
})
end.addEventListener('click',function(){
clearInterval(timer)
data.splice(random,1)
end.disabled = true
start.disabled = false
if( data.length === 1){
start.disabled = true
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>
<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 rgb(192, 192, 192);
outline: none;
transition: all .2s;
}
.mi .search {
border: 2px solid #ff6700;
}
.list{
position: absolute;
left: 0;
top: 48px;
width: 223px;
border: 1px solid #ff6700;
border-top: 0;
background: white;
display: none;
}
.list a {
display: block;
padding: 6px 15px;
font-size: 12px;
color: #424242;
text-decoration: none;
}
</style>
</head>
<body>
<div class="mi">
<input type="search" placeholder="小米">
<ul class="list">
<li><a href="#">全部商品</a></li>
<li><a href="#">小米11</a></li>
<li><a href="#">小米10s</a></li>
<li><a href="#">小米笔记本</a></li>
<li><a href="#">小米笔记本</a></li>
<li><a href="#">黑鲨4</a></li>
<li><a href="#">空调</a></li>
</ul>
</div>
<script>
let search = document.querySelector('input[type="search"]')
let list = document.querySelector('.list')
// 获取焦点 focus mouseenter
search.addEventListener('mouseenter',function(){
list.style.display = 'block'
search.classList.add('search')
})
// 失去焦点 blur mouseleave
search.addEventListener('mouseleave',function(){
list.style.display = 'none'
search.classList.remove('search')
})
</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>
<style>
*{
margin: 0;
padding: 0;
}
.w{
width: 400px;
height: 200px;
margin: 0 auto;
}
textarea{
width: 400px;
}
</style>
</head>
<body>
<div class="w">
<div class="controls">
<textarea id="area" placeholder="说点什么把..." name="" id="" cols="30" rows="10" maxlength="200"></textarea>
<div>
<span class="useCount">0</span>
<span>/</span>
<span>200</span>
<button id="send">发布</button>
</div>
</div>
<div class="contentList">
<ul></ul>
</div>
</div>
<script>
let area = document.querySelector('#area')
let useCount = document.querySelector('.useCount')
area.addEventListener('input',function(){
useCount.innerHTML = area.value.length
})
</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>
<style>
*{
margin: 0;
padding: 0;
}
table{
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #c0c0c0;
width: 500px;
margin: 100px auto;
text-align: center
}
th{
border: 1px solid #d0d0d0;
color: #404060;
padding: 10px;
}
.allCheck{
width: 80px;
}
tr:nth-child(1){
background-color: skyblue;
}
</style>
</head>
<body>
<table>
<tr>
<th class="allCheck">
<input type="checkbox" id="checkAll"><span class="all">全选</span>
</th>
<th>商品</th>
<th>商家</th>
<th>价格</th>
</tr>
<tr>
<th>
<input type="checkbox" name="checl" class="ck">
</th>
<th>小米手机</th>
<th>小米</th>
<th>1999</th>
</tr>
<tr>
<th>
<input type="checkbox" name="checl" class="ck">
</th>
<th>小米净水器</th>
<th>小米</th>
<th>4999</th>
</tr>
<tr>
<th>
<input type="checkbox" name="checl" class="ck">
</th>
<th>小米电视</th>
<th>小米</th>
<th>5999</th>
</tr>
</table>
<script>
let all = document.querySelector('#checkAll')
let cks = document.querySelectorAll('.ck')
let span = document.querySelector('span')
all.addEventListener('click',function(){
for( let i = 0; i < cks.length; i++){
cks[i].checked = all.checked
}
if (all.checked === true){
span.innerHTML = '取消'
}else{
span.innerHTML = '全选'
}
})
for (let i = 0;i < cks.length; i++ ){
cks[i].addEventListener('click',function(){
for( let j = 0; j < cks.length; j++){
if(cks[j].checked === false) {
all.checked = false
return
}
}
all.checked = 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>
<style>
div{
width: 80px;
}
input[type=text]{
width: 50px;
height: 44px;
outline: none;
border: 1px solid #ccc;
text-align: center;
border-right: 0;
}
input[type=button]{
width: 22px;
height: 24px;
cursor: pointer;
}
input{
float: left;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div>
<input type="text" name="" id="total" value="1" readonly>
<input type="button" value="+" id="add">
<input type="button" value="-" id="reduce" disabled>
</div>
<script>
let total = document.querySelector('#total')
let add = document.querySelector('#add')
let reduce = document.querySelector('#reduce')
add.addEventListener('click',function(){
total.value++
reduce.disabled = false
})
reduce.addEventListener('click',function(){
total.value--
if (total.value <= 1){
reduce.disabled = true
}
})
</script>
</body>
</html>
重点
回调函数
function fn(){}
setInterval(fn,1000)
环境变量this
<body>
<button>点击</button>
<script>
function fn(){
console.log(this);
}
window.fn()
let btn = document.querySelector('button')
btn.addEventListener('click',function(){
console.log(typeof this);
})
</script>
</body>
排他思想
<!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>
<style>
.color{
background: skyblue;
}
</style>
</head>
<body>
<button class="color">第1个</button>
<button>第2个</button>
<button>第3个</button>
<button>第4个</button>
<button>第5个</button>
<script>
let btns = document.querySelectorAll('button')
for (let i = 0; i < btns.length; i++){
btns[i].addEventListener('click',function(){
document.querySelector('.color').classList.remove('color')
this.classList.add('color')
})
}
</script>
</body>
</html>
案例tab栏切换
<!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>
<style>
*{
margin: 0;
padding: 0;
}
.box{
margin: 0 auto;
width: 1600px;
height: 920px;
}
.top{
width: 1600px;
height: 20px;
}
.con{
width: 1600px;
height: 900px;
}
.con img{
width: 100%;
display: none;
}
ul{
list-style: none;
display: flex;
}
.box ul .item{
flex: 1;
border: 1px solid blue;
text-align: center;
}
.box ul .active{
background: blue;
color: aqua;
}
.con .active{
display: block;
}
</style>
</head>
<body>
<div class="box">
<div class="top">
<ul>
<li class="item active">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
</ul>
</div>
<div class="con">
<img src="../../resources/image/background/1.jpg" alt="" class="active">
<img src="../../resources/image/background/2.jpg" alt="">
<img src="../../resources/image/background/3.jpg" alt="">
<img src="../../resources/image/background/4.jpg" alt="">
</div>
</div>
<script>
let lis = document.querySelectorAll('.item')
let imgs = document.querySelectorAll('img')
for(let i = 0 ; i < lis.length; i++){
lis[i].addEventListener('click',function(){
document.querySelector('.box ul .active').classList.remove('active')
this.classList.add('active')
document.querySelector('.con .active').classList.remove('active')
imgs[i].classList.add('active')
})
}
</script>
</body>
</html>