相册管理学习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | <!DOCTYPE html> <html lang= "zh-CN" > <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>在线相册管理器</title> <link rel= "stylesheet" href= "style.css" /> </head> <body> <ul class = "container" ></ul> <script> // 所有图片放到一个数组中 const imgs = [ "images/img-1.jpg" , "images/img-2.jpg" , "images/img-3.jpg" , "images/img-4.jpg" , "images/img-5.jpg" , "images/img-6.jpg" , "images/img-7.jpg" , "images/img-8.jpg" , ]; // 动态生成图片 const ul = document.querySelector( "ul" ); // 当页面加载完成时将所有图片显示出来 window.onload = showImgs; // 显示所有图片 function showImgs() { // 使用数组迭代器来生成当前的图片元素 let res = imgs.reduce((prev, curr) => { let tpl = ` <li> <img src= "${curr}" alt= "" /> <div class = "btns" > <button onclick= "prev(this.parentNode.parentNode)" >向前</button> <button onclick= "next(this.parentNode.parentNode)" >向后</button> <button onclick= "del(this.parentNode.parentNode)" >删除</button> </div> </li> `; // 最终结果是通过prev返回的, 因为返回是一个字符串 // 建议传入第二参数,设置prev初值 return prev + tpl; }, "" ); console.log(res); // 插入到页面中 ul.insertAdjacentHTML( "beforeEnd" , res); } // 删除 function del(ele) { // ele: 要被删除的元素参数 // if (confirm("是否删除")) { // ele.remove(); // } // 三元 return confirm( "是否删除?" ) ? ele.remove() : false ; } // 向前 function prev(ele) { // 判断有没有前一个节点? if (ele.previousElementSibling === null ) { alert( "已经是第一张了" ); return false ; } let prevNode = ele.previousElementSibling; // parentNode.insertBefore(插入元素,插入位置 ) ul.insertBefore(ele, prevNode); } // 向后 function next(ele) { // 判断有没有下一个节点? if (ele.nextElementSibling === null ) { alert( "已经是最后一张了" ); return false ; } let nextNode = ele.nextElementSibling; // parentNode.insertBefore(插入元素,插入位置 ) ul.insertBefore(nextNode, ele); } </script> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | * { margin: 0; padding: 0; box-sizing: border-box; } html { font-size: 10px; } body { font-size: 1.6rem; } ul { display: flex; flex-flow: row wrap; justify-content: space-around; } li { list-style: none; width: 20rem; border: 1px solid seagreen; padding: 1rem 1rem 0; margin-top: 1rem; display: flex; flex-flow: column nowrap; } li:hover { background-color: lightcyan; cursor: pointer; } li img { width: 100%; height: 100%; } li button { background-color: seagreen; color: white; border: none; outline: none; margin: 0.2rem; border-radius: 0.3rem; height: 2rem; cursor: pointer; flex: 1; } li button:hover { background-color: coral; } li .btns { height: 4rem; display: flex; justify-content: space-around; align-items: center; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义