Adding a Transition When Changing Images

. . . jQuery . . . $("img").on("click", function(){   $("#top").fadeToggle(2000); }); . . . HTML . . . <img id="bottom" src="lake.jpg" /> <img id="top" src="volcano.jpg" />

You cannot directly animate the transition from one image to another so that one image is fading in while the other is fading out. The way to overcome that challenge is to have two images—one on top of the other—and then animate fading the top one out.

The following code does just that. There are two images with fixed positioning. When you click on the top image, it toggles the image opacity in and out, as shown in Figure 12.2.

01 <html> 02   <head> 03   <title>Python ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.