知识点补充:
1.insetBefore(new,ref(参考元素))
:在某元素之前插入DOM原生操作没有insertAfter();
2.appendChild()
:如果是父元素就在父元素最后一个子元素之后插入,如果没有子元素就在元素本身之后插入
3.Element.contains("class")
判断是否包含没class属性,返回布尔值;
4.Element.getAttribute()获取指定元素的属性的值;
5.自定义属性,通过dataset获取和设置
6.获取元素的子元素集合和节点集合:
- children:获取子元素集合;
- childNodes:获取所有节点集合(包含文字和空格以及回车);
案例练习
1.选项卡
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>选项卡</title>
<style>
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
ul{
list-style: none;
}
.container{
height: 500px;
width: 400px;
margin: 0 auto;
display: grid;
grid-template-rows: 100px 400px;
}
.container > ul{
display: flex;
justify-content: flex-start;
width: 400px;
align-items: center;
}
.item{
flex:1;
height: 100px;
text-align: center;
line-height: 100px;
background-color: lightgray;
}
.ulactive{
background-color: lightcoral;
}
.img{
width: 400px;
display: none;
}
.item:hover{
cursor:pointer;
}
.active{
display: block;
}
</style>
</head>
<body>
<div class="container">
<ul>
<li class="item ulactive" data-index="1">选项卡1</li>
<li class="item" data-index="2">选项卡2</li>
<li class="item" data-index="3">选项卡3</li>
</ul>
<div>
<img src="images/products%20(1).jpg" alt="" class="img active" data-index="1">
<img src="images/products%20(2).jpg" alt="" class="img" data-index="2">
<img src="images/products%20(7).jpg" alt="" class="img"data-index="3">
</div>
</div>
</body>
<script type="text/javascript">
let ul= document.querySelector(".container>ul");
let imgs=document.querySelectorAll(".container > div > img");
function ulactive(ul,name){
// console.log(Array.from(ul));
Array.from(ul).forEach(item=>{
// console.log(item);
item.classList.remove(name);
});
}
ul.addEventListener("click",ev=>{
// console.log(ev.currentTarget.childNodes);
// console.log(ev.currentTarget.children);
ulactive(ev.currentTarget.children,"ulactive");
ev.target.classList.add("ulactive");
let index=ev.target.dataset.index;
// console.log(imgs);
imgs.forEach(item=>{
if(item.dataset.index===index){
ulactive(imgs,"active")
item.classList.add("active");
}
})
});
</script>
</html>
运行结果
2.换背景
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>切换背景图片</title>
<style>
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.container{
height: 500px;
width: 400px;
margin: 0 auto;
background-image: url("images/products%20(1).jpg");
}
.img{
width: 50px;
opacity:0.8;
border: 4px solid white;
}
</style>
</head>
<body>
<div class="container">
<div>
<img src="images/products%20(1).jpg" alt="" class="img active" data-index="1">
<img src="images/products%20(2).jpg" alt="" class="img" data-index="2">
<img src="images/products%20(7).jpg" alt="" class="img"data-index="3">
</div>
</div>
</body>
<script type="text/javascript">
let container=document.querySelector(".container");
let div=document.querySelector(".container > div");
div.addEventListener("click",ev=>{
let src=ev.target.getAttribute("src");
container.style.backgroundImage=`url("${src}")`;
});
</script>
</html>
运行结果
3.懒加载
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>我的相册</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-color: #FFB6C1;
}
body > div:first-child{
width:6rem ;
margin: 2rem auto;
text-align: center;
color: #fff;
}
.container{
display: grid;
grid-template-columns: repeat(auto-fit,minmax(12rem,1fr));
grid-gap: 2rem;
padding: 2rem;
}
.container > .item {
display: flex;
flex-flow: column nowrap;
width: 12rem;
height: 13rem;
align-items: center;
background-color: #fff;
border-radius: 0.5rem;
justify-content: space-evenly;
}
.container > .item > img{
width: 10rem;
height: 12rem,;
}
</style>
</head>
<body>
<div><h2>我的相册</h2></div>
<div class="container">
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片1</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片2</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片3</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片4</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片5</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片6</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片7</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片8</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片9</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片10</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片11</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片12</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片13</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片14</span></div>
<div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片15</span></div>
</div>
</body>
<script>
let items=document.querySelectorAll(".item");
// console.log(items);
let item=document.querySelector(".item");
function changeurl(items){
let scrollHeight=document.documentElement.scrollTop;
let viewHeight=document.documentElement.clientHeight;
items.forEach(item=>{
let itemHeight=item.offsetTop;
if (itemHeight<(scrollHeight+viewHeight)){
img=item.firstElementChild;
src=img.dataset.src;
img.setAttribute("src",src);
console.log("图片加载…………");
}
})
}
window.addEventListener("scroll",()=>{
setTimeout(changeurl(items),500);
});
window.addEventListener("load",()=>{
setTimeout(changeurl(items), 0);
});
</script>
</html>
运行结果
、
4.轮播图
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>轮播图</title>
</head>
<style>
*{
margin:0;
padding: 0;
box-sizing: border-box;
}
.container{
width: 800px;
height: 200px;
margin: 3px auto;
position: relative;
}
.img{
width: 800px;
height: 266px;
display: none;
}
.dp{
display: block;
}
.skip{
width: 800px;
display: flex;
justify-content: space-between;
position: absolute;
top:100px;
}
.skip > span{
width: 60px;
height: 80px;
background-color:lightslategray;
font-size: 40px;
color: white;
line-height: 80px;
text-align: center;
opacity: 0.6;
}
.skip > span:hover{
opacity: 1;
cursor: pointer;
}
.pointer{
width: 800px;
display: flex;
justify-content: center;
position: absolute;
top:240px
}
.dot{
/* width: 6px; */
border: 6px solid #000000;
border-radius: 6px;
margin: 0 4px;
}
.dotactive{
border-color: white;
}
.pointer > span:hover{
cursor: pointer;
border-color: white;
}
</style>
<body>
<div class="container">
<div class="banner">
<img src="banner/banner1.jpg" data-index="1" class="img dp" alt="">
<img src="banner/banner2.jpg" data-index="2" class="img" alt="">
<img src="banner/banner3.jpg" data-index="3" class="img" alt="">
<img src="banner/banner4.jpg" data-index="4" class="img" alt="">
</div>
<div class="skip">
<span id="prev"><</span>
<span id="next">></span>
</div>
<div class="pointer">
</div>
</div>
</body>
<script type="text/javascript">
// 获取图片
let imgs=document.querySelectorAll(".img");
//根据图片数量生成小圆点
function createdot(num,ref){
let farg=document.createDocumentFragment();
for(i=0;i<num;i++){
let span=document.createElement("span")
span.dataset.index=i+1;
span.classList.add("dot");
if(i===0){
span.classList.add("dotactive");
}
farg.appendChild(span);
}
ref.appendChild(farg);
}
// 获取生成小圆点的参照
let pointer=document.querySelector(".pointer");
// 生成小圆点
createdot(imgs.length,pointer);
// 创建激活函数
function active(items,target,cl){
Array.from(items).forEach(item=>{
item.classList.remove(cl);
})
target.classList.add(cl);
}
// 根据点击小圆点切换图片
pointer.addEventListener("click",ev=>{
let dots=ev.currentTarget.children;
if(ev.target.tagName==="SPAN"){
active(dots,ev.target,"dotactive");
let index=ev.target.dataset.index;
imgs.forEach(item=>{
item.classList.remove("dp")
if(item.dataset.index===index){
item.classList.add("dp");
}
})
}
});
// 获取前一张按钮
let prev=document.querySelector(".skip span:first-of-type");
prev.addEventListener("click",ev=>{
let currentimg=[...imgs].filter(item=>{
if(item.classList.contains("dp")){
return item
}
})[0];
let index=currentimg.dataset.index;
let nextindex = index==="1"?imgs.length:index-1;
[...imgs].forEach(item=>{
if(item.dataset.index==nextindex){
// console.log(item);
active(imgs,item,"dp");
}
})
let dots=document.querySelectorAll(".pointer *")
dots.forEach(item=>{
if(item.dataset.index==nextindex){
// console.log(item);
active(dots,item,"dotactive");
}
})
})
// 获取下一张
let next=document.querySelector(".skip span:last-of-type");
// let click=new Event("click");
next.addEventListener("click",ev=>{
let currentimg=[...imgs].filter(item=>{
if(item.classList.contains("dp")){
return item
}
})[0];
let index=currentimg.dataset.index;
// console.log(index);
let nextindex = index==="4"? 1 :parseInt(index)+1;
// console.log(nextindex);
[...imgs].forEach(item=>{
if(item.dataset.index==nextindex){
// console.log(item);
active(imgs,item,"dp");
}
})
let dots=document.querySelectorAll(".pointer *")
dots.forEach(item=>{
if(item.dataset.index==nextindex){
// console.log(item);
active(dots,item,"dotactive");
}
})
})
function timer(){
let cl=new Event("click");
next.dispatchEvent(cl);
}
let time=setInterval(timer,1000);
let container=document.querySelector(".container");
// 鼠标移入暂停自动播放
container.addEventListener("mouseover",ev=>{
clearInterval(time);
})
// 鼠标移出自动播放
container.addEventListener("mouseout",ev=>{
time=setInterval(timer,1000);
})
</script>
</html>
运行结果