Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Replace <picture> src onmouseover with animated webp

I have a <picture> element with 2 images. An .avif image and a .jpg fallback.

<picture id="mouse-event" onmouseover="this.src='/animated.webp'">
    <source type="image/avif" srcset="/sm.avif">
    <img src="/sm.jpg">
</picture>

When I move my mouse over the <picture> element, I want to replace the the shown image with an animated .webp (smiliar to youtube thumbnail hover).

I have tried putting onmouseoveron the picture element but that didn't work.

Also I tried replacing the src with javascript without success:

document.getElementById("mouse-event").src = "/animated.webp";

(Just for fun I added the onmouseover event to the img tag. That works, but only if I disable browser support for .avif so that the jpg is shown.)

How can I achieve an image replacement onmouseover with a picture element so that avif/jpg fallback is still possible?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to replace the img element's src or the source element's srcset, not the picture element's src.

For example

<picture id="mouse-event" onmouseover="doMouseover()">
  <source type="image/avif" srcset="/sm.avif">
  <img id="theImage" src="/sm.jpg">
</picture>

<script>
  function doMouseover() {
    document.getElementById("theImage").src = "/animated.webp";
  }
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!