2

I am new to html.I need a delete button with animated style. I have used a gif image for animation. I want this image to be run when the user place the mouse over it. Currently, the gif image is running infinite. How to make the image run when the user move the mouse over it. Help me with some solutions. If you have any other transparent delete image, please provide, it can help me.

Here's the markup I tried:

<html>
  <body>
    <a>
      <img src="https://i.postimg.cc/1RGh8PpK/delete.gif"
           alt="Delete image" width="100" height="100" />
    </a>
  </body>
</html>
T B Duzijn
  • 21
  • 10
Stack Kiddy
  • 556
  • 1
  • 4
  • 10

3 Answers3

6

Head on over EZGIF to crop your gif and make it an appropriate size for a button:

enter image description here

Use your computer to take a static screenshot of the gif (you'll have to time it right):

enter image description here

Use HTML to hold both the static and animated image elements:

<img class='static_trash' id = 'static_btn' src='https://collaboratescience.com/stack/googs/trash_static.png' alt='Delete image' width='100' height='100' />

<img class='live_trash' id = 'live_btn' src='https://i.postimg.cc/1RGh8PpK/delete.gif' alt='Delete image' width='100' height='100' />

Use CSS to set live_trash to "display: none"

. live_trash {
    "display" : "none"
}

Use JavaScript to change image source on hover:

Vanilla JavaScript:

let static_elem = document.getElementById("static_btn")
let live_elem = document.getElementById("live_btn")

static_elem.addEventListener("mouseenter", function( event ) {   
    event.target.style.display = "none";
    live_elem.style.display = "block"
})

static_elem.addEventListener("mouseleave", function( event ) {   
    event.target.style.display = "block";
    live_elem.style.display = "none"
})

Result:

enter image description here

You can play with crop sizing to get it better.

Cybernetic
  • 12,628
  • 16
  • 93
  • 132
1

You can achieve this with JavaScript or CSS. All you need is a static frame from the gif.

JavaScript

You can utilize the mouseenter and mouseleave mouse events to alter the src of the image.

const states = {
  'default': "https://i.stack.imgur.com/mJHTA.png",
  'hover': "https://i.stack.imgur.com/WwxRR.gif"
};

let img = document.querySelector('#hover-img');

img.addEventListener('mouseenter', function(e) {
  img.setAttribute('src', states.hover);
});
img.addEventListener('mouseleave', function(e) {
  img.setAttribute('src', states.default);
});
<a>
  <img src="https://i.stack.imgur.com/mJHTA.png" id="hover-img" alt="Delete image" width="100" height="100" />
</a>

CSS

Or, you can determine if the user if hovering over the element using the CSS pseudo-class :hover, but you will probably want to use a <div> or a <span> instead of a <img>.

.hoverable {
  display: inline-block;
  width: 100px;
  height: 100px;
  background: center url('https://i.stack.imgur.com/mJHTA.png') no-repeat;
  background-size: contain;
}

.hoverable:hover {
  background-image: url('https://i.stack.imgur.com/WwxRR.gif');
}
<a><span class="hoverable"></span></a>

JavaScript using data tags

Alternatively, you can associate the default and hover state with the element via data-* attributes.

You can utilize the mouseenter and mouseleave mouse events to alter the src of the image.

let img = document.querySelector('#hover-img');

img.addEventListener('mouseenter', function(e) {
  img.setAttribute('src', e.target.getAttribute('data-hover-src'));
});
img.addEventListener('mouseleave', function(e) {
  img.setAttribute('src', e.target.getAttribute('data-default-src'));
});
<a>
  <img id="hover-img" alt="Delete image" width="100" height="100"
    src="https://i.stack.imgur.com/mJHTA.png"
    data-default-src="https://i.stack.imgur.com/mJHTA.png"
    data-hover-src="https://i.stack.imgur.com/WwxRR.gif" />
</a>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0
a {
    background-image: url("https://i.postimg.cc/1RGh8PpK/static-delete.png")
}
a:hover {
    background-image: url("https://i.postimg.cc/1RGh8PpK/delete.gif")   
}