What is the best way to switch between my two images upon clicking?

When I click on an image, it changes to a different image. I achieved this by altering the source of the image upon clicking. Now, my goal is to incorporate a smooth transition between the two images after they are clicked.

I attempted to stack the two images on top of each other and adjust the opacity of the upper image upon clicking. However, I ran into difficulties changing the opacity through JavaScript, so I moved on from that approach in search of a simpler solution.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
.slidecontainer {
  width: 100%;
  opacity: 0;
  transition: opacity .3s cubic-bezier(.4, 0, .2, 1);
}

.slider {
  -webkit-appearance: none;
  width: 100px;
  height: 15px;
  border-radius: 5px;  
  background: #d3d3d3;
  outline: none;
  -webkit-transition: .2s;
  transition: opacity .2s;
  position: relative;
}



.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #4CAF50;
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #4CAF50;
  cursor: pointer;
}

.slidecontainer:hover, img:hover + .slidecontainer {
  opacity: 1;
}
</style>

<img id="Lightning" src="https://static.wixstatic.com/media/38d0c2_1f8b71c0bf644137ab295604ceaaecaa~mv2.png" height="100" width="100">

<div class="slidecontainer">
  <input type="range" min="0" max="100" value="100" class="slider" id="volume">
</div>

<audio id="Thunder" loop>
   <source src="http://music.wixstatic.com/preview/38d0c2_064b409cb5594774ab3c1fa24f9afa2f-128.mp3" type="audio/wav" />
</audio>

<script type="text/javascript">

function LightningChange() {
if (Lightning.src == "https://static.wixstatic.com/media/38d0c2_1f8b71c0bf644137ab295604ceaaecaa~mv2.png") {
    Lightning.src = "https://static.wixstatic.com/media/38d0c2_5609c3592b894208b679ce8641031ae8~mv2.png"; 
} else {
    Lightning.src = "https://static.wixstatic.com/media/38d0c2_1f8b71c0bf644137ab295604ceaaecaa~mv2.png";
}}

document.getElementById("Lightning").onclick = function() {
    LightningChange()
    var thunderAudio = document.getElementById("Thunder");
    if (thunderAudio.paused) thunderAudio.play();
    else thunderAudio.pause();
};

volume.addEventListener("mousemove", thunderVolume);

function thunderVolume(){
    document.getElementById("Thunder").volume = document.getElementById("volume").value / 100;
}
    </script>

Answer №1

I managed to achieve this by layering two images on top of each other and modifying their styles upon click

<style>
    .imageContainer{
         position: absolute;
     }

.hide{
  visibility: hidden;
  opacity: 0;
  transition: opacity .5s, visibility .5s;
}

.visible{
  visibility: visible;
  opacity: 1;
}
</style>


<div id="Lightning" class="container">
    <img class="imageContainer visible" src="https://static.wixstatic.com/media/38d0c2_1f8b71c0bf644137ab295604ceaaecaa~mv2.png" height="100" width="100">
    <img id="secondImg" class="imageContainer hide"   src="https://static.wixstatic.com/media/38d0c2_5609c3592b894208b679ce8641031ae8~mv2.png" height="100" width="100">
</div>


<script type="text/javascript">
document.getElementById("Lightning").onclick = function() {
   document.getElementById("secondImg").classList.add("visible");
};
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Reverse the order of jQuery toggle animations

Looking for something specific: How can I create a button that triggers a script and then, when the script is done, reverses the action (toggles)? (I am currently learning javascript/jquery, so I am a beginner in this field) Here's an example: ...

Struggling with CSS float problems

I'm currently working with the Pure CSS framework and my code resembles this: <div class="container pure-g"> <header class='pure-u-1'> <h1 class='logo'> <a href="#">TEST</a> </h1> &l ...

Issue with clearTimeout function not functioning properly on keyup event in iFrame

While there may be many similar questions out there, I have yet to find a solution that works for me. Currently, I am developing a WYSIWYG editor and I want it to save when the user performs a keyup action. However, I do not want it to update after every ...

Finding the nearest span element with a selector in styled components

Currently experimenting with integrating Styled Components to target the nearest span element. <label> <span className="">Password</span> <input type="password" id="passwordInput" /> </label> ...

Animate the centering of a DIV

Currently, I am working on an animation featuring a series of DIV's contained within a div with the identifier id="wrapper", all styled using CSS3. However, when I hover over the rounded box, the animation is not properly aligned in the center. You ca ...

The process of generating an efficient build gets stuck and never finishes

I am currently facing an issue while attempting to build my React app. Whenever I execute "npm run build," the terminal gets stuck at "Creating and optimized production build..." and doesn't complete the process. I have tried running this on a system ...

Is React failing to insert a span element in the client-side code?

First off, I'm a beginner in the world of react and recently tackled this guide on creating a real-time Twitter stream To cut to the chase, I wanted to include a link next to each tweet that followed its text. To achieve this, I simply added props to ...

Set the CSS table to have a width of 100% and ensure that the table data cells have hidden

My table has a 100% width, and some of the td elements have overflow hidden. I want to make their text appear as ellipsis when it's too long. I can achieve this with fixed td sizes, but I need them to be relative to the content. This question was fi ...

What is the best way to create a dropdown menu that smoothly slides in from the bottom of the screen?

Is it possible to create dropdown menus in the navigation bar that slide in from the bottom with a smooth transition effect, similar to this example: Although I am working on building my own template, so my code may differ from the original theme. Here is ...

Perform calculations for product and sum when button is clicked

I received the following task for an assignment: Develop 3 functions along with a form. The first function should execute upon button press and utilize the other 2 functions. These two functions are supposed to compute the sum of two numbers and the ...

Encountering issues with the phaser framework while setting up a server on xampp, faced with errors in javascript and html

Recently, I've been working on a game using Phaser and encountered some issues while trying to run the code in XAMPP. The game's base is located at htdocs/itw/test.html, with the necessary pngs and json file stored in htdocs/itw/assets/. The pngs ...

Utilizing useState for React Data Picker

I recently attempted to implement the React Data Picker in my React project using npmjs. However, I encountered an issue when trying to import useState from import React, { useState } from "react"; The error message displayed: 'useState&a ...

Setting up vue-resource root and authentication configuration

Currently, I am reviewing the documentation for vue-resource that outlines how to configure it: https://github.com/vuejs/vue-resource/blob/master/docs/config.md The documentation specifies setting headers with a common authorization value: Vue.http.hea ...

Having trouble getting jQuery .append() to behave the way you want?

I have created a code snippet to display a table like this: $("#results").append("<table><tr><th>Name</th><th>Phone Number</th></tr>"); $("#results").append("<tr><td>John</td><td>123123123 ...

Problem with BeautifulSoup table parsing

I've been trying to create a table with expenses, amounts, and actions, but I'm having trouble with the alignment. I attempted to adjust it by changing the width, but that didn't solve the issue. Can someone point out what I'm doing wro ...

How can we transform words with double "OO" in a paragraph into green squares using React? For instance, changing "clooney" to "cl[green square]ey"

import React, { Component } from 'react'; class App extends Component { constructor() { super(); this.state = { modifiedPar: "", newPar: "modified paragraph will be displayed here"}; } greenSquareMaker = word = ...

I am having an issue with my jQuery form submission not sending any POST variables

My code seems to be having issues as the PHP file is not receiving the POST-variables. I am unsure of what could be going wrong, so I am reaching out for some guidance. Here is the HTML: <div id="preloader" class="preload"></div> <div id=" ...

Using Fetch API in NextJS requires pressing the Enter key twice for it to work properly

The functionality of this component should display JSON data in the console log after entering input into the search bar and hitting the enter key. However, there is a lag where the data doesn't show until the enter key is pressed a second time. Addit ...

Utilizing Selenium automation to navigate a website that features a custom jQuery plugin-checkbox functionality

Utilizing Selenium WebDriver along with JavaScript to automate the testing of a website that features a custom jquery plugin named "jcf" (JavaScript Custom Forms) for aesthetically pleasing forms. You can find more information about this plugin here. I am ...

Ways to expand the tooltip width in Bootstrap-Vue

Is there a way to make the tooltip wider in Bootstrap Vue? I have a long statement to display in the tooltip, but it is only showing three words per row, resulting in a taller tooltip with less width. <div> <span id="disabled-wrapper" class=" ...