Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

Show a GIF on Button Click, Then Hide It with JavaScript

Reveal a centered GIF when a button is clicked, then hide it after a chosen delay. A shared class lets several buttons use the same JavaScript behavior.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Hide the GIF by default, reveal it when a button is clicked, and use a timer to hide it again after 2–3 seconds. For several buttons with the same behavior, give each button a shared class and attach one event handler to that class.

Show and hide a GIF with a button click

This native JavaScript example centers one GIF over the viewport. Add the image and a semantic button to your page:

<button type="button" class="show-gif">Summon Pokémon</button>

<div class="gif-overlay" hidden>
  <img src="pokemon.gif" alt="A Pokémon appears">
</div>

Then add CSS to center the image and JavaScript to show it temporarily:

.gif-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgb(0 0 0 / 35%);
  z-index: 1000;
}

.gif-overlay[hidden] {
  display: none;
}

.gif-overlay img {
  max-width: min(90vw, 500px);
  max-height: 90vh;
}

const overlay = document.querySelector('.gif-overlay');
let hideTimer;

document.querySelectorAll('.show-gif').forEach((button) => {
  button.addEventListener('click', () => {
    clearTimeout(hideTimer);
    overlay.hidden = false;
    hideTimer = setTimeout(() => {
      overlay.hidden = true;
    }, 2500);
  });
});

Change pokemon.gif to the path to your image. The timer delay is in milliseconds, so 2500 requests 2.5 seconds; use 2000 or 3000 for two or three seconds. The clearTimeout call makes a new click restart that display period rather than leaving an earlier timer scheduled to hide the image. MDN explains that setTimeout schedules a callback asynchronously; the actual callback may run later than its requested delay, depending on browser conditions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use the same behavior for multiple buttons

The example uses querySelectorAll('.show-gif') to select every button with the show-gif class, then attaches the same click handler to each. Put that class on each button that should show the image:

<button type="button" class="show-gif">Summon Pokémon</button>
<button type="button" class="show-gif">Show another animation</button>

Do not give multiple buttons the same ID. IDs are intended to identify a single element; a shared class is the appropriate selector for a group. Use a real <button> rather than a clickable <div>: buttons have built-in control semantics and keyboard behavior. MDN documents the button element.

How the older jQuery approach relates

The 2014 SitePoint forum thread used jQuery effects such as fadeIn and fadeOut, with a timeout to hide the image. Its sample used jQuery 1.9.1; that is a historical example, not a reason to load that old version for a new page. The discussion also notes that jQuery is optional. The native code above performs the same basic reveal-and-hide interaction without that library. Read the original SitePoint discussion.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Fixed display time or wait for the GIF to finish?

The timer approach hides the image after the duration you choose, whether or not the animation has reached its final frame. It is suitable when a brief, predictable display is the goal. If the image must remain visible until its animation genuinely finishes, a fixed timeout is not the same thing as detecting animation completion; the cited forum example does not establish an animation-end method.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.