You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

加载时图片淡入效果实现求助:如何为mosaic图片添加fadeIn加载效果

How to Add Fade-In Effect to Loading Mosaic Images

Hey there! I’ve run into this exact issue before—getting those mosaic tiles to fade in smoothly instead of popping into view can make a huge difference in the user experience. Let’s break down two solid approaches using vanilla HTML/CSS/JS, plus a quick jQuery alternative if you’re using that library.

Method 1: Basic Native JS (Immediate Load)

First, we’ll use CSS to set up the fade transition, then JavaScript to trigger it once the image finishes loading.

Step 1: HTML Structure

Give your mosaic images a shared class so we can target them easily:

<div class="mosaic-container">
  <img src="tile-1.jpg" alt="Mosaic tile" class="mosaic-img">
  <img src="tile-2.jpg" alt="Mosaic tile" class="mosaic-img">
  <img src="tile-3.jpg" alt="Mosaic tile" class="mosaic-img">
  <!-- Add all your mosaic tiles here -->
</div>

Step 2: CSS for Transition

We’ll start the images fully transparent, then add a smooth transition for the opacity property:

.mosaic-img {
  opacity: 0;
  transition: opacity 0.6s ease-in-out; /* Adjust duration to your liking */
  /* Add your existing mosaic styling (width, float, grid layout, etc.) here */
}

.mosaic-img.loaded {
  opacity: 1;
}

Step 3: JavaScript to Trigger Fade

We need to listen for the load event on each image, and also handle cached images that might already be loaded when the script runs:

document.addEventListener('DOMContentLoaded', function() {
  const mosaicImages = document.querySelectorAll('.mosaic-img');

  mosaicImages.forEach(img => {
    // Check if image is already cached/loaded
    if (img.complete) {
      img.classList.add('loaded');
    } else {
      // Wait for the image to load
      img.addEventListener('load', function() {
        this.classList.add('loaded');
      });
      // Fallback in case image fails to load
      img.addEventListener('error', function() {
        this.classList.add('loaded'); // Show broken image icon instead of hiding it
      });
    }
  });
});

Method 2: Intersection Observer (For Lazy-Loaded Mosaics)

If your mosaic is large and you’re lazy-loading images (only loading them when they enter the viewport), the Intersection Observer API is perfect—it triggers the fade-in only when the image is visible:

Update the JavaScript

document.addEventListener('DOMContentLoaded', function() {
  const mosaicImages = document.querySelectorAll('.mosaic-img');

  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        const img = entry.target;
        // Load the image (if using lazy load with data-src)
        if (img.dataset.src) {
          img.src = img.dataset.src;
        }
        // Wait for load then fade in
        img.addEventListener('load', () => {
          img.classList.add('loaded');
        });
        // Stop observing once handled
        observer.unobserve(img);
      }
    });
  }, { threshold: 0.1 }); // Trigger when 10% of the image is visible

  mosaicImages.forEach(img => {
    observer.observe(img);
  });
});

Note: If using lazy load, update your HTML to use data-src instead of src: <img data-src="tile-1.jpg" class="mosaic-img">

jQuery Alternative (If You Prefer)

If you’re already using jQuery, this is a shorter way to achieve the same effect:

$(document).ready(function() {
  $('.mosaic-img').hide().each(function() {
    $(this).on('load', function() {
      $(this).fadeIn(600); // 600ms duration
    });
    // Handle cached images
    if (this.complete) $(this).trigger('load');
  });
});

You can remove the initial opacity:0 CSS if using this method, since jQuery’s fadeIn() handles the visibility.

Quick Troubleshooting Tips

  • Make sure your CSS transition isn’t being overridden by other styles (use browser dev tools to inspect the transition property).
  • If images are still popping in, check if the load event is firing—some cached images might skip the event, which is why we check the complete property.
  • Adjust the transition duration (in CSS) or fadeIn() time (in jQuery) to match your desired smoothness.

内容的提问来源于stack exchange,提问作者Joe278

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:28:14