Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:将课堂案例全部照抄一遍, 连素材都懒得换, 这不是一个学习者应该有的态度 , 希望有一点你自己的想法在里面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
评论:<input type="text" />
<!-- 创建有序列表 -->
<ol></ol>
<script>
// 实现功能:input框中的值 按回车显示到有序列表中 且永远现在在第一位
var cl = console.log;
// 获取输入框与有序列表
var input = document.querySelector("input");
var ol = document.querySelector("ol");
// keyDown 按下
// keyup 抬起
// keypress 获取单个字母,功能键无效
// 创建事件监听
input.addEventListener(
"keyup",
function (ev) {
// 先拿到按键
// cl(ev.key); 回车 Enter
// 是enter 才执行
if (ev.key === "Enter") {
// 判断input是否有值
if (input.value.length === 0) {
alert("输入框为空,提交失败");
}
// 创建标签
var li = document.createElement("li");
// 加个删除按钮
li.innerHTML =
input.value + "<button onclick='del(this)'>删除</button>";
// 判断是否有值
if (ol.childElementCount === 0) {
// 插入ol中
ol.appendChild(li);
} else {
// 放到最前面
ol.insertBefore(li, ol.firstElementChild);
}
// 清空input框
input.value = null;
}
},
false
);
// 删除函数
function del(ele) {
// 问一下要不要删除
return confirm("是否删除?")
? ele.parentNode.parentNode.removeChild(ele.parentNode)
: false;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>选项卡</title>
<style>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #555;
}
a:hover {
text-decoration: underline;
color: red;
}
li {
list-style: none;
}
li:hover {
cursor: default;
}
.tabs {
width: 300px;
height: 300px;
margin: 30px;
background-color: #ddd;
display: flex;
flex-direction: column;
}
.tab {
height: 36px;
display: flex;
}
.tab li {
flex: auto;
text-align: center;
line-height: 36px;
background-color: #fff;
}
.tab li.active {
background-color: #ddd;
}
/* 默认所有选项卡只有一个显示,其它隐藏 */
.item {
padding: 20px;
display: none;
}
.item.active {
display: block;
}
</style>
</head>
<body>
<div class="tabs">
<!-- 导航 -->
<ul class="tab">
<li class="active" data-index="1">水果</li>
<li data-index="2">手机</li>
<li data-index="3">汽车</li>
</ul>
<!-- 详情1 -->
<ul class="item active" data-index="1">
<li><a href="">西瓜</a></li>
<li><a href="">苹果</a></li>
<li><a href="">香蕉</a></li>
</ul>
<!-- 详情2 -->
<ul class="item" data-index="2">
<li><a href="">华为</a></li>
<li><a href="">小米</a></li>
<li><a href="">OPPO</a></li>
</ul>
<!-- 详情2 -->
<ul class="item" data-index="3">
<li><a href="">吉利</a></li>
<li><a href="">奇瑞</a></li>
<li><a href="">长城</a></li>
</ul>
</div>
<script>
var cl = console.log;
// 给导航添加一个点击事件
var tab = document.querySelector(".tab");
var items = document.querySelectorAll(".item");
tab.addEventListener("click", show, false);
tab.addEventListener("mouseover", show, false);
function show(ev) {
// 先删除所有的active
ev.target.parentNode.childNodes.forEach(function (item) {
if (item.nodeType === 1) {
item.classList.remove("active");
}
});
// 给当前的点击激活
ev.target.classList.toggle("active");
// 清空原有列表的激活
items.forEach(function (item) {
item.classList.remove("active");
// 找到遇到导航相对应的 激活
// if (item.dataset.index === ev.target.dataset.index) {
// item.classList.add("active");
// }
});
items.forEach(function (item) {
if (item.dataset.index === ev.target.dataset.index)
item.classList.add("active");
});
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>懒加载</title>
<style>
.container {
width: 500px;
display: grid;
gap: 10px;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.container img {
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<img src="images/temp.jpg" alt="" data-src="images/img-1.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-2.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-3.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-4.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-5.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-6.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-7.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-8.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-9.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-10.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-11.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-12.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-13.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-14.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-15.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-16.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-17.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-18.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-19.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-20.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-21.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-22.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-23.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-24.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-25.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-26.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-27.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-28.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-29.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-30.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-31.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-32.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-33.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-34.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-35.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-36.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-37.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-38.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-39.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-40.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-41.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-42.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-43.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-44.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-45.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-46.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-47.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-48.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-49.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-50.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-51.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-52.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-53.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-54.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-55.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-56.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-57.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-58.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-59.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-60.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-61.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-62.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-63.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-64.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-65.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-66.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-67.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-68.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-69.jpg" />
<img src="images/temp.jpg" alt="" data-src="images/img-70.jpg" />
</div>
</body>
<script>
var cl = console.log;
// 先拿到图片
var imgs = document.querySelectorAll("img");
// 拿到文档可视区高度
var clientHeight = document.documentElement.clientHeight;
// 监听窗口滚动
window.addEventListener(
"scroll",
function () {
lazyload(imgs, clientHeight);
},
false
);
// 懒加载函数
function lazyload(imgs, clientHeight) {
// 动态获取当前内容区距离顶部的滚动大小
var scrollTop = document.documentElement.scrollTop;
// 遍历图片
imgs.forEach(function (img) {
if (img.offsetTop <= clientHeight + scrollTop) {
img.src = img.dataset.src;
}
});
}
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>轮播图</title>
<style>
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
.box {
/*定位父级*/
position: relative;
width: 1000px;
height: 350px;
margin: 0 auto;
}
.box .slider {
width: 1000px;
height: 350px;
display: none;
}
.box .slider.active {
display: block;
}
.box .point-list {
position: absolute;
/*绝对定位的环境下的水平居中方式*/
left: 50%;
margin-left: -38px;
top: 310px;
}
.box .point-list .point {
display: inline-block;
width: 12px;
height: 12px;
margin: 0 5px;
background-color: white;
border-radius: 100%;
}
.box .point-list .point.active {
background-color: black;
}
.box .point-list .point:hover {
cursor: pointer;
}
.skip {
position: absolute;
top: 140px;
display: inline-block;
width: 40px;
height: 80px;
text-align: center;
line-height: 80px;
background-color: lightgray;
color: white;
opacity: 0.2;
font-size: 36px;
}
.box .prev {
left: 0;
}
.box .next {
right: 0;
}
.box .skip:hover {
cursor: pointer;
opacity: 0.5;
color: black;
}
</style>
</head>
<body>
<div class="box">
<img
src="banner/banner1.jpg"
alt=""
data-index="1"
class="slider active"
/>
<img src="banner/banner2.jpg" alt="" data-index="2" class="slider" />
<img src="banner/banner3.jpg" alt="" data-index="3" class="slider" />
<img src="banner/banner4.jpg" alt="" data-index="4" class="slider" />
<div class="point-list"></div>
<span class="skip prev"><</span>
<span class="skip next">></span>
</div>
</body>
<script>
var cl = console.log;
// 先拿到要轮播的图片组
var imgs = document.querySelectorAll(".box > img");
// cl(imgs);
var pointList = document.querySelector(".point-list");
// 动态生成小圆点 index 索引
imgs.forEach(function (img, index) {
var span = document.createElement("span");
if (index === 0) {
// 默认第一个高亮
span.classList.add("point", "active");
}
span.classList.add("point");
// 将小圆点与当前图片进行关联
span.dataset.index = img.dataset.index;
pointList.appendChild(span);
});
// 获取所有小圆点
var points = document.querySelectorAll(".point");
// 给小圆点添加点击事件
pointList.addEventListener(
"click",
function (ev) {
cl(ev.target);
imgs.forEach(function (img) {
// 判断
if (img.dataset.index === ev.target.dataset.index) {
// 先删除掉所有的active
imgs.forEach(function (img) {
img.classList.remove("active");
});
// 给当前图片添加active
img.classList.add("active");
// 公共函数u:设置小圆点高亮,与图片对应且同步
setPointActive(img.dataset.index);
}
});
},
false
);
// 公共函数
function setPointActive(imgIndex) {
// 删除掉所有active
points.forEach(function (point) {
point.classList.remove("active");
// 给当前与图片对应的小圆点设置高亮
if (point.dataset.index === imgIndex) {
point.classList.add("active");
}
});
}
// 获取到翻页按钮
var skip = document.querySelectorAll(".skip");
// 添加点击事件
skip[0].addEventListener("click", skipImg, false);
skip[1].addEventListener("click", skipImg, false);
// 创建函数
function skipImg(ev) {
// 先找到图片
var courrentImg = null;
// 遍历
imgs.forEach(function (img) {
if (img.classList.contains("active")) {
courrentImg = img;
}
});
// 删除掉图片中的active
courrentImg.classList.remove("active");
// 判断是否点击了前一张 当前元素中是否存在prev
if (ev.target.classList.contains("prev")) {
courrentImg = courrentImg.previousElementSibling;
// 判断是否存在前一张
if (courrentImg !== null && courrentImg.nodeName === "IMG") {
courrentImg.classList.add("active");
} else {
// 否则显示最后一张
courrentImg = imgs[imgs.length - 1];
imgs[imgs.length - 1].classList.add("active");
}
}
// 判断是否点击了下一张
if (ev.target.classList.contains("next")) {
courrentImg = courrentImg.nextElementSibling;
// 判断是否存在后一张
if (courrentImg !== null && courrentImg.nodeName === "IMG") {
courrentImg.classList.add("active");
} else {
// 否则显示第一张
courrentImg = imgs[0];
imgs[0].classList.add("active");
}
}
// 小圆点高亮
setPointActive(courrentImg.dataset.index);
}
// 设置定时器
var box = document.querySelector(".box");
var timer = null;
// 鼠标移出启动
box.addEventListener("mouseout", startTimer, false);
// 鼠标移入启动
box.addEventListener("mouseover", clearTimer, false);
// 启动定时器函数
function startTimer() {
var click = new Event("click");
timer = setInterval(function () {
skip[1].dispatchEvent(click);
}, 2000);
}
// 清除定时器
function clearTimer() {
clearInterval(timer);
}
</script>
</html>
想要独立完成太难了,只好跟着源码一步一步操作理解每一步的作用这样子,