Blogger Information
Blog 21
fans 0
comment 0
visits 14744
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
(1106)js 选项卡和 todolist
Yuming
Original
557 people have browsed it

js 选项卡和 todolist

  1. 留言板
  1. <div class="container">
  2. <input type="text" placeholder="请输入留言内容" />
  3. <ul></ul>
  4. </div>
  1. const input = document.querySelector("input");
  2. input.addEventListener("keyup", handleAddBlod, false);
  3. function handleAddBlod(e) {
  4. if (e.key == "Enter") {
  5. const ul = document.querySelector(".container ul");
  6. const li = document.createElement("li");
  7. li.innerHTML = `${input.value} <button onClick='del(this)'>删除</button>`;
  8. if (ul.childElementCount == 0) {
  9. ul.appendChild(li);
  10. } else {
  11. ul.insertBefore(li, ul.firstElementChild);
  12. }
  13. }
  14. }
  15. function del(params) {
  16. params.parentNode.remove();
  17. }

2.tab 选项卡

  1. <div class="tabs">
  2. <ul class="tab">
  3. <li class="active" data-index="1">选项卡1</li>
  4. <li data-index="2">选项卡2</li>
  5. <li data-index="3">选项卡3</li>
  6. </ul>
  7. <ul class="item active" data-index="1">
  8. <li>第一</li>
  9. <li>第一</li>
  10. <li>第一</li>
  11. </ul>
  12. <ul class="item" data-index="2">
  13. <li>第二</li>
  14. <li>第二</li>
  15. <li>第二</li>
  16. </ul>
  17. <ul class="item" data-index="3">
  18. <li>第三</li>
  19. <li>第三</li>
  20. <li>第三</li>
  21. </ul>
  22. </div>
  23. ![](./02.png)
  1. const tab = document.querySelector("ul");
  2. // console.log(tab.children[1]); //用来湖从htmlcollection获取元素节点
  3. tab.addEventListener("click", handleTabChange, false);
  4. tab.addEventListener("mouseover", handleTabChange, false);
  5. function handleTabChange(ev) {
  6. const index = ev.target.dataset.index;
  7. Array.from(tab.children).forEach((element) => {
  8. element.classList.remove("active");
  9. });
  10. ev.target.classList.add("active");
  11. // 内容区的代码
  12. items.forEach((item) => {
  13. // 1删掉acitive
  14. // 2添加active
  15. item.dataset.index == index
  16. ? item.classList.add("active")
  17. : item.classList.remove("active");
  18. });
  19. }
  20. // 2.内容去的显示与隐藏
  21. const items = document.querySelectorAll(".tabs .item");

3.一键换肤

  1. <div class="container">
  2. <img src="./images/1.jpg" alt="" />
  3. <img src="./images//10.jpg" alt="" />
  4. <img src="./images//13.jpg" alt="" />
  5. </div>
  1. const imgs = document.querySelector(".container");
  2. imgs.addEventListener("click", function (ev) {
  3. console.log(ev.target.src);
  4. document.body.style.backgroundImage = `url(${ev.target.src})`;
  5. });

4.图片懒加载

  1. // 通过事件监听
  2. // 滚动高度scorryTop 视口高度clientHeight 元素Y轴位置offset 文档高度
  3. const clientHeight = document.documentElement.clientHeight;
  4. const imgs = document.querySelectorAll("img");
  5. window.addEventListener("scroll", scroll, false);
  6. window.addEventListener("load", scroll, false);
  7. function scroll() {
  8. const scrollTop = document.documentElement.scrollTop;
  9. // 元素Y洲位置 小于 视口高度加滚动高度
  10. imgs.forEach((img) => {
  11. if (img.offsetTop <= clientHeight + scrollTop) {
  12. img.src = img.dataset.src;
  13. }
  14. });
  15. }
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:不知你是否理解了, 没有作业总结 , 直接放个源码, 这样不好的, 老师不是要看这个
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post