placeholder.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>图片占位符示例</title>
  7. <style>
  8. /* 容器样式 - 关键:通过JS动态设置尺寸 */
  9. .image-container {
  10. position: relative;
  11. max-width: 90%;
  12. margin: 20px auto;
  13. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  14. border-radius: 8px;
  15. overflow: hidden;
  16. }
  17. /* 占位符样式 - 与图片尺寸完全一致 */
  18. .image-placeholder {
  19. width: 100%;
  20. height: 100%;
  21. background-color: #f5f5f5;
  22. /* 浅灰色背景 */
  23. display: flex;
  24. align-items: center;
  25. justify-content: center;
  26. }
  27. /* 加载动画 - 旋转圆圈 */
  28. .loading-spinner {
  29. width: 40px;
  30. height: 40px;
  31. border: 4px solid #e0e0e0;
  32. border-top: 4px solid #ff6b6b;
  33. /* 主题色 */
  34. border-radius: 50%;
  35. animation: spin 1s linear infinite;
  36. }
  37. @keyframes spin {
  38. 0% {
  39. transform: rotate(0deg);
  40. }
  41. 100% {
  42. transform: rotate(360deg);
  43. }
  44. }
  45. /* 图片样式 - 初始隐藏,加载后显示 */
  46. .target-image {
  47. width: 100%;
  48. height: 100%;
  49. object-fit: contain;
  50. opacity: 0;
  51. /* 初始透明 */
  52. transition: opacity 0.3s ease;
  53. /* 淡入效果 */
  54. }
  55. /* 图片加载完成后显示 */
  56. .target-image.loaded {
  57. opacity: 1;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <!-- 图片容器:包含占位符和图片 -->
  63. <div class="image-container">
  64. <div class="image-placeholder" id="placeholder">
  65. <div class="loading-spinner"></div>
  66. </div>
  67. <img id="targetImg" class="target-image"
  68. src="http://color.jccytech.cn/thumbs/coloring-page/done/480/614d6e7987ca20188d3cc089.webp" alt="示例图片">
  69. </div>
  70. <script>
  71. // 目标图片URL
  72. const imageUrl = "http://color.jccytech.cn/thumbs/coloring-page/done/480/614d6e7987ca20188d3cc089.webp";
  73. const container = document.querySelector('.image-container');
  74. const placeholder = document.getElementById('placeholder');
  75. const targetImg = document.getElementById('targetImg');
  76. // 步骤1:预加载图片获取尺寸
  77. function getImageDimensions(url) {
  78. return new Promise((resolve, reject) => {
  79. const tempImg = new Image();
  80. tempImg.src = url;
  81. // 图片元信息加载完成(无需加载完整内容)
  82. tempImg.onload = () => {
  83. resolve({
  84. width: tempImg.naturalWidth,
  85. height: tempImg.naturalHeight
  86. });
  87. };
  88. tempImg.onerror = () => reject(new Error("图片尺寸获取失败"));
  89. });
  90. }
  91. // 步骤2:设置占位容器尺寸(与图片一致)
  92. async function setupPlaceholder() {
  93. try {
  94. const { width, height } = await getImageDimensions(imageUrl);
  95. // 设置容器尺寸(与图片完全一致)
  96. container.style.width = `${width}px`;
  97. container.style.height = `${height}px`;
  98. // 对于响应式设计,可改为百分比宽度(根据实际需求)
  99. // container.style.width = '100%';
  100. // container.style.maxWidth = `${width}px`;
  101. // container.style.height = 'auto';
  102. // container.style.aspectRatio = `${width}/${height}`; // 宽高比
  103. } catch (error) {
  104. console.error(error);
  105. // 失败时使用默认尺寸
  106. container.style.width = '800px';
  107. container.style.height = '600px';
  108. }
  109. }
  110. // 步骤3:图片加载完成后隐藏占位符
  111. targetImg.onload = function () {
  112. targetImg.classList.add('loaded'); // 图片淡入
  113. placeholder.style.display = 'none'; // 隐藏占位符
  114. };
  115. // 初始化
  116. setupPlaceholder();
  117. </script>
  118. </body>
  119. </html>