Blogger Information
Blog 17
fans 0
comment 0
visits 12806
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
原生相册功能
再见羊肉串儿
Original
749 people have browsed it
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>相册</title>
  8. <style>
  9. /* 通用样式 */
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. li {
  16. list-style: none;
  17. }
  18. a {
  19. text-decoration: none;
  20. }
  21. .container {
  22. display: flex;
  23. flex-flow: row wrap;
  24. justify-content: space-around;
  25. }
  26. .container > .butgirl {
  27. width: 13rem;
  28. border: 1px solid green;
  29. padding: 0.5rem;
  30. margin-top: 0.5rem;
  31. }
  32. .container > .butgirl > img {
  33. width: 100%;
  34. }
  35. /* 按钮 */
  36. .container > .butgirl > .btn {
  37. display: flex;
  38. justify-content: space-between;
  39. margin-top: 0.5rem;
  40. }
  41. .container > .butgirl > .btn > button {
  42. background-color: green;
  43. height: 1.5rem;
  44. border-radius: 0.5rem;
  45. border: none;
  46. flex: 0 1 31%;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="container"></div>
  52. <script>
  53. let imgsGroup = [
  54. "images/img-1.jpg",
  55. "images/img-4.jpg",
  56. "images/img-8.jpg",
  57. "images/img-16.jpg",
  58. "images/img-24.jpg",
  59. "images/img-20.jpg",
  60. "images/img-25.jpg",
  61. "images/img-39.jpg",
  62. ];
  63. // 获取图片容器
  64. let container = document.querySelector(".container");
  65. //当页面加载完成之后显示图片
  66. window.onload = showImgs;
  67. //图片展示方法
  68. function showImgs() {
  69. let res = imgsGroup.reduce(function (prev, curr) {
  70. let html = `
  71. <div class="butgirl">
  72. <img src="${curr}" />
  73. <div class="btn">
  74. <button onclick=goBefer(this)>向前</button>
  75. <button onclick=goAfter(this)>向后</button>
  76. <button onclick=goDel(this)>删除</button>
  77. </div>
  78. </div>
  79. `;
  80. return prev + html;
  81. }, "");
  82. container.insertAdjacentHTML("afterbegin", res);
  83. }
  84. //图片向前
  85. function goBefer(obj) {
  86. //获取点击的图片元素
  87. let father = obj.parentNode.parentNode;
  88. //点击的图片元素的上一个兄弟元素
  89. let befor = father.previousElementSibling;
  90. // console.log(befor);
  91. if (befor == null) {
  92. alert("不能向前了!");
  93. return false;
  94. }
  95. // 将兄弟元素插入到点击元素之前
  96. container.insertBefore(father, befor);
  97. }
  98. // 图片向后
  99. function goAfter(obj) {
  100. //获取点击的图片元素
  101. let father = obj.parentNode.parentNode;
  102. //点击的图片元素的下一个兄弟元素
  103. let next = father.nextElementSibling;
  104. // console.log(befor);
  105. if (next == null) {
  106. alert("不能向后了!");
  107. return false;
  108. }
  109. // 将点击元素插入到兄弟元素之后
  110. // dom只提供了
  111. container.insertBefore(next, father);
  112. }
  113. // 图片删除
  114. function goDel(obj) {
  115. let father = obj.parentNode.parentNode;
  116. if (confirm("是否删除?")) father.remove();
  117. }
  118. </script>
  119. </body>
  120. </html>
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