Blogger Information
Blog 18
fans 1
comment 0
visits 17372
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Javascript实战案例
α清尘
Original
753 people have browsed it

经典的toDoList

实现按住回车键发表留言,并且可以删除,新发表的留言在上方显示;

代码展示:

  1. <form action="" onsubmit="return false">
  2. <label for="liuyan">留言:</label>
  3. <input type="text" name="liuyan" id="liuyan" placeholder="请按回车键添加">
  4. </form>
  5. <ul id="list"></ul>
  6. <script>
  7. const ly=document.querySelector("#liuyan");
  8. const list=document.querySelector("#list");
  9. ly.addEventListener("keyup",show,false)
  10. function show(ev){
  11. console.log(ev.key);
  12. if(ev.key==="Enter"){
  13. console.log(ly.value);
  14. if(ly.value.trim().length===0){
  15. alert("不能为空!")
  16. ly.focus();
  17. return false;
  18. }
  19. const li=document.createElement("li")
  20. li.innerHTML=ly.value+ ' <button onclick="del(this)">删除</button>';
  21. if(list.childElementCount===0){
  22. list.appendChild(li);
  23. }else{
  24. list.insertBefore(li,list.firstElementChild)
  25. }
  26. ly.value=null;
  27. }
  28. }
  29. function del(ele){
  30. console.log(ele);
  31. if(confirm("是否确认删除?"))list.removeChild(ele.parentNode);
  32. }
  33. </script>

选项卡案例

实例演示:

  1. <div class="tabs">
  2. <ul class="tab">
  3. <li class="active" data-index="1">公司新闻</li>
  4. <li data-index="2">行业动态</li>
  5. <li data-index="3">常见问答</li>
  6. </ul>
  7. <ul class="item active" data-index="1">
  8. <li><a href="">公司新闻公司新闻公司新闻</a></li>
  9. <li><a href="">公司新闻公司新闻公司新闻</a></li>
  10. <li><a href="">公司新闻公司新闻公司新闻</a></li>
  11. </ul>
  12. <ul class="item" data-index="2">
  13. <li><a href="">行业动态行业动态行业动态</a></li>
  14. <li><a href="">行业动态行业动态行业动态</a></li>
  15. <li><a href="">行业动态行业动态行业动态</a></li>
  16. </ul>
  17. <ul class="item" data-index="3">
  18. <li><a href="">常见问答常见问答常见问答</a></li>
  19. <li><a href="">常见问答常见问答常见问答</a></li>
  20. <li><a href="">常见问答常见问答常见问答</a></li>
  21. </ul>
  22. </div>
  23. <script>
  24. const tab=document.querySelector(".tab")
  25. const list=document.querySelectorAll(".item")
  26. // 添加绑定事件
  27. tab.addEventListener("click",show,false);
  28. // 事件回调
  29. function show(ev){
  30. // 清空导航原有的样式 Array.from(tab.children).forEach((item)=>item.classList.remove("active"))
  31. // 激活当前选项
  32. ev.target.classList.toggle(("active"));
  33. // 清空列表原生的内容
  34. list.forEach((item)=>item.classList.remove("active"))
  35. // 利用三元运算符简写
  36. list.forEach((item)=>item.dataset.index===ev.target.dataset.index?item.classList.toggle("active"):null);
  37. }
  38. </script>

一键换肤实例

css部分

  1. .container {
  2. width: 300px;
  3. display: grid;
  4. grid-template-columns: repeat(3, 1fr);
  5. column-gap: 10px;
  6. }
  7. .container > img {
  8. width: 100%;
  9. border: 2px solid #fff;
  10. opacity: 0.6;
  11. }
  12. .container > img:active {
  13. opacity: 1;
  14. }
  15. .container > img:hover {
  16. opacity: 1;
  17. cursor: pointer;
  18. width: 102%;
  19. }
  20. body {
  21. background-image: url(static/images/timg1.jpg);
  22. background-repeat: no-repeat;
  23. background-size: cover;
  24. }

代码展示:

  1. <div class="container">
  2. <img src="static/images/timg1.jpg" alt="" />
  3. <img src="static/images/timg2.jpg" alt="" />
  4. <img src="static/images/timg3.jpg" alt="" />
  5. </div>
  6. <script>
  7. document.querySelector(".container").addEventListener("click",(ev)=>(document.body.style.backgroundImage="url("+ev.target.src+")"));
  8. </script>

懒加载实现

代码展示:

  1. <div class="container">
  2. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  3. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  4. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  5. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  6. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  7. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  8. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  9. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  10. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  11. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  12. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  13. <img src="static/images/temp.jpg" alt="" data-src="static/images/timg4.jpg" />
  14. </div>
  15. <script>
  16. const imgs=document.querySelectorAll(".container img")
  17. // 获取视口高度;
  18. const clientHeight=document.documentElement.clientHeight;
  19. // 监听窗口的滚动事件
  20. window.addEventListener("scroll",lan,false)
  21. // 页面加载成功时自动执行;
  22. window.addEventListener("load",lan,false)
  23. function lan(){
  24. let scrolltop=document.documentElement.scrollTop;
  25. // 遍历判断是否进入可视区域
  26. imgs.forEach((img)=>{
  27. // offsetTop图片距离顶部的高度;
  28. if(img.offsetTop<=clientHeight+scrolltop){
  29. img.src=img.dataset.src;
  30. setTimeout(()=>{img.src=img.dataset.src},500)
  31. }
  32. });
  33. }
  34. </script>

懒加载原理:
在页面存在大量图片的情况下,为了提高页面性能,当用户浏览到当前资源时,再对资源进行请求和加载;当元素偏移高度小于视口高度+滚动高度,则显示图片。

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