| 1 | document.addEventListener("DOMContentLoaded", function () {
|
| 2 | const gifContainers = document.querySelectorAll(".gif-container");
|
| 3 |
|
| 4 | gifContainers.forEach((container) => {
|
| 5 | const img = container.querySelector("img[data-gif]");
|
| 6 |
|
| 7 | if (img) {
|
| 8 | const pngSrc = img.src;
|
| 9 | const gifSrc = img.getAttribute("data-gif");
|
| 10 | let isGif = false;
|
| 11 |
|
| 12 | // Create play button dynamically
|
| 13 | const playButton = document.createElement("button");
|
| 14 | playButton.className = "gif-play-button";
|
| 15 | playButton.setAttribute("aria-label", "Play demo");
|
| 16 | container.appendChild(playButton);
|
| 17 |
|
| 18 | function toggleGif() {
|
| 19 | if (!isGif) {
|
| 20 | // Switch to GIF
|
| 21 | img.src = gifSrc;
|
| 22 | isGif = true;
|
| 23 | container.classList.add("playing");
|
| 24 | } else {
|
| 25 | // Switch back to PNG
|
| 26 | img.src = pngSrc;
|
| 27 | isGif = false;
|
| 28 | container.classList.remove("playing");
|
| 29 | }
|
| 30 | }
|
| 31 |
|
| 32 | // Play button click
|
| 33 | playButton.addEventListener("click", function (e) {
|
| 34 | e.preventDefault();
|
| 35 | e.stopPropagation();
|
| 36 | toggleGif();
|
| 37 | });
|
| 38 |
|
| 39 | // Image click (play or pause)
|
| 40 | img.addEventListener("click", function (e) {
|
| 41 | e.preventDefault();
|
| 42 | e.stopPropagation();
|
| 43 | toggleGif();
|
| 44 | });
|
| 45 | }
|
| 46 | });
|
| 47 | });
|
| 48 |
|