一、日期对象
1.1 实例化
得到当前的系统时间
//得到当前时间
const date = new Date()
//获取指定时间
//倒计时使用
const date1 = new Date('2023-04-07 15:30:00')
1.2 日期对象方法
方法 | 作用 | 取值 |
getFullYear() | 四位年份 | |
getMonth() | 0-11 | |
getDate() | 月份中的每一天 | 每个月份不一样 |
getDay() | 获取星期 | 0-6 |
getHours() | 0-23 | |
getMinutes() | 0-59 | |
getSeconds() | 0-59 |
<!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: 300px;
height: 40px;
border: 1px solid pink;
text-align: center;
line-height: 40px;
}
</style>
</head>
<body>
<div></div>
<script>
const div = document.querySelector('div')
function getMyDate() {
const date = new Date()
let h = date.getHours()
let m = date.getMinutes()
let s = date.getSeconds()
h = h < 10 ? '0' + h : h
m = m < 10 ? '0' + m : m
s = s < 10 ? '0' + s : s
return `今天是: ${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}号 ${h}:${m}:${s}`
}
setInterval(function () {
div.innerHTML = getMyDate()
//简洁法
// let date = new Date()
// div.innerHTML = date.toLocaleString()
}, 1000)
</script>
</body>
</html>
1.3 时间戳
1970.01.01 00:00:00 距离现在的毫秒数
获取时间戳
//1 getTime()
const date = new Date()
console.log(date.getTime())
//2 +new Date()
console.log(+new Date())
//3 Date.now()
console.log(Date.now())
//前两种可以得到指定时间的时间戳
//第三种只能得到当前时间戳
案例-毕业倒计时(快点毕业
<!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>
.countdown {
width: 240px;
height: 305px;
text-align: center;
line-height: 1;
color: #fff;
border-radius: 10px;
box-shadow: 10px 10px 5px rebeccapurple;
background-color: blueviolet;
margin: 100px auto;
/* background-size: 240px; */
/* float: left; */
overflow: hidden;
}
.countdown .next {
font-size: 16px;
margin: 25px 0 14px;
}
.countdown .title {
font-size: 33px;
}
.countdown .tips {
margin-top: 80px;
font-size: 23px;
}
.countdown small {
font-size: 17px;
}
.countdown .clock {
width: 220px;
margin: 18px auto 0;
overflow: hidden;
}
.countdown .clock span,
.countdown .clock i {
display: block;
text-align: center;
line-height: 34px;
font-size: 23px;
float: left;
}
.countdown .clock span:first-child{
margin-left: 18px;
}
.countdown .clock span {
height: 34px;
border-radius: 2px;
background-color: #303430;
}
.countdown .clock i {
width: 20px;
font-style: normal;
}
</style>
</head>
<body>
<div class="countdown">
<p class="next">今天是<span id="year">0000</span>年<span id="month">0</span>月<span id="day">0</span>日</p>
<p class="title">毕业倒计时</p>
<p class="clock">
<span id="date">00</span>
<i> 天 </i>
<span id="hour">00</span>
<i>:</i>
<span id="minutes">25</span>
<i>:</i>
<span id="second">20</span>
</p>
<p class="tips">离毕业又近了1秒钟</p>
</div>
<script>
// // 随机颜色函数
// // 1. 自定义一个随机颜色函数
// function getRandomColor(flag = true) {
// if (flag) {
// // 3. 如果是true 则返回 #ffffff
// let str = '#'
// let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
// // 利用for循环随机抽6次 累加到 str里面
// for (let i = 1; i <= 6; i++) {
// // 每次要随机从数组里面抽取一个
// // random 是数组的索引号 是随机的
// let random = Math.floor(Math.random() * arr.length)
// // str = str + arr[random]
// str += arr[random]
// }
// return str
// } else {
// // 4. 否则是 false 则返回 rgb(255,255,255)
// let r = Math.floor(Math.random() * 256) // 55
// let g = Math.floor(Math.random() * 256) // 89
// let b = Math.floor(Math.random() * 256) // 255
// return `rgb(${r},${g},${b})`
// }
// }
// // 页面刷新随机得到颜色
// const countdown = document.querySelector('.countdown')
// countdown.style.backgroundColor = getRandomColor()
// // 函数封装 getCountTime
// function getCountTime() {
// // 1. 得到当前的时间戳
// const now = +new Date()
// // 2. 得到将来的时间戳
// const last = +new Date('2022-4-1 18:30:00')
// // console.log(now, last)
// // 3. 得到剩余的时间戳 count 记得转换为 秒数
// const count = (last - now) / 1000
// // console.log(count)
// // 4. 转换为时分秒
// // h = parseInt(总秒数 / 60 / 60 % 24) // 计算小时
// // m = parseInt(总秒数 / 60 % 60); // 计算分数
// // s = parseInt(总秒数 % 60);
// // let d = parseInt(count / 60 / 60 / 24) // 计算当前秒数
// let h = parseInt(count / 60 / 60 % 24)
// h = h < 10 ? '0' + h : h
// let m = parseInt(count / 60 % 60)
// m = m < 10 ? '0' + m : m
// let s = parseInt(count % 60)
// s = s < 10 ? '0' + s : s
// console.log(h, m, s)
// // 5. 把时分秒写到对应的盒子里面
// document.querySelector('#hour').innerHTML = h
// document.querySelector('#minutes').innerHTML = m
// document.querySelector('#scond').innerHTML = s
// }
// // 先调用一次
// getCountTime()
// // 开启定时器
// setInterval(getCountTime, 1000)
function getDatee(){
const noww = new Date()
let yy = noww.getFullYear()
let mm = noww.getMonth() + 1
let dd = noww.getDate()
document.querySelector('.next #year').innerHTML = yy
document.querySelector('.next #month').innerHTML = mm
document.querySelector('.next #day').innerHTML = dd
}
function getCountTime(){
const now = +new Date()
const last = +new Date('2025-6-1 00:00:00')
const count = (last - now) / 1000 //换成秒数
let d = parseInt(count / 60 / 60 / 24)
let h = parseInt(count / 60 / 60 % 24)
h = h < 10 ? '0' + h : h
let m = parseInt(count / 60 % 60)
m = m < 10 ? '0' + m : m
let s = parseInt(count % 60)
s = s < 10 ? '0' + s : s
const date = document.querySelector('#date')
const hour = document.querySelector('#hour')
const minutes = document.querySelector('#minutes')
const second = document.querySelector('#second')
date.innerHTML = d
hour.innerHTML = h
minutes.innerHTML = m
second.innerHTML = s
}
getDatee()
getCountTime()
setInterval(getCountTime,1000)
</script>
</body>
</html>
二、节点操作
2.1 DOM节点
节点类型
- 元素节点 所有的标签
- 属性节点 所有的属性
- 文本节点 所有的文本
2.2 查找结点
站在元素关系的角度(父子、兄弟)查找
//父节点查找
子元素.parentNode
//子元素查找
//获得所有子节点(包括文本节点 注释节点)
父元素.childNodes
//仅获得所有标签节点
//得到伪数组 选择的是亲儿子
父元素.children
//兄弟查找
//下一个兄弟
element.nextElementSibling
//上一个兄弟
element.previousElementSibling
2.3 增加节点
创建一个新的节点
把该节点加入指定元素内部
//创建一个新节点
document.createElement('标签名')
//将该节点插入指定父元素的最后
父元素.appendChild('要插入的元素')
//插入父元素中某个元素的前面
父元素.insertBefore(要插入的元素, 在哪个元素前面)
//克隆一个已有元素的节点 true表示连带后代一起克隆 false表示克隆不包含后代节点
元素.cloneNode(true/false)
2.4 删除节点
删除元素必须通过父元素删除
父元素.removeChild(要删除的元素)
三、移动端事件
3.1 touch事件
touchstart | 手指触摸到一个DOM元素时触发 |
touchmove | 手指在一个DOM元素上滑动触发 |
touchend | 手指从一个DOM元素上移开时触发 |
<div></div>
<script>
const div = document.querySelector('div')
div.addEventListener('touchstart',function(){
console.log('开始摸');
})
div.addEventListener('touchend',function(){
console.log('结束摸');
})
div.addEventListener('touchmove',function(){
console.log('一直摸');
})
</script>
四、插件
https://www.swiper.com.cn/