Steps for creating a fade effect between two different elements when the mouse is interacting with one of them

I want to create a unique effect with an image in the background and a button above it. When the mouse hovers over the button, I want its opacity to decrease while the image's opacity increases simultaneously. I have experience applying fade effects to individual elements, but I'm unsure how to achieve this dual effect.

For reference, you can see a similar effect in action at this link. In my case, the effect should be triggered when the mouse is on the title, causing its opacity to change from 1 to 0.3 without affecting the image directly.

.domicilio {
    width:10%; 
    text-align:center; 
    padding:5px;
    position: relative;
    margin-left: 7%;
}

.domicilio img {
...
}

.domicilio button{
    position: absolute;
    top: 50%;
    left: 165%;


    
    /*Styling*/

    font-family: OldEnglish;
    background-color: #803c25;
    color: #FFC107;
    border-color: #FFC107;
    font-size: 30px;
    padding: 12px 24px;
    cursor: pointer;
    border-radius: 5px;
}

.domicilio button:hover{
    opacity: 0.3;
    transition: 0.3s;
}
<div class="domicilio">
                        <img src="...">
                        <a href="https://www.google.it">
                            <button>I'm disappearing, the image behind me should light up</button>
                        </a>
</div>

Answer №1

While a CSS expert may uncover a solution using only CSS, for the sake of discussion, here is an example utilizing jQuery.

It's important to add the initial state CSS opacity on the image in your CSS to apply it on the first hover. After that, you can alter states on mouse hover.

If you want to learn more about hover effects, check out: https://api.jquery.com/hover/, and for information on changing CSS: https://api.jquery.com/css/.

Hopefully, this information proves helpful; I'm certain Baby Yoda would approve.

$( "#fade" ).hover(
  function() {
  $( "#img" ).css( "opacity", "1" );
  $( "#img" ).css( "transition", "0.3s" );
  }, function() {
      $( "#img" ).css( "opacity", "0.3" );
      $( "#img" ).css( "transition", "0.3s" );
  }
);
.domicilio {
    width:10%; 
    text-align:center; 
    padding:5px;
    position: relative;
    margin-left: 7%;
}

.domicilio img {
max-width: 400%;
opacity: 0.3;
transition: 0.3s;
}

.domicilio button{
    position: absolute;
    top: 50%;
    left: 165%;

    
    /*Appearance*/

    font-family: OldEnglish;
    background-color: #803c25;
    color: #FFC107;
    border-color: #FFC107;
    font-size: 30px;
    padding: 12px 24px;
    cursor: pointer;
    border-radius: 5px;
}

.domicilio button:hover{
    opacity: 0.3;
    transition: 0.3s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="domicilio">
                        <img id="img"src="https://miro.medium.com/max/1400/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg">
                        <a href="https://www.google.it">
                            <button id="fade">I'm disappearing, the image behind me should light up</button>
                        </a>
</div>

EDIT:

A pure JS solution upon user request.

document.getElementById("fade").onmouseover = function() 
{
    document.getElementById("img").style.opacity = "1";
    document.getElementById("img").style.transition = "0.3s";
}
document.getElementById("fade").onmouseleave = function() 
{
    document.getElementById("img").style.opacity = "0.3";
    document.getElementById("img").style.transition = "0.3s";
}
.domicilio {
    width:10%; 
    text-align:center; 
    padding:5px;
    position: relative;
    margin-left: 7%;
}

.domicilio img {
max-width: 400%;
opacity: 0.3;
transition: 0.3s;
}


.domicilio button{
    position: absolute;
    top: 50%;
    left: 165%;

    
    /*Appearance*/

    font-family: OldEnglish;
    background-color: #803c25;
    color: #FFC107;
    border-color: #FFC107;
    font-size: 30px;
    padding: 12px 24px;
    cursor: pointer;
    border-radius: 5px;
}

.domicilio button:hover{
    opacity: 0.3;
    transition: 0.3s;
}
<div class="domicilio">
                        <img id="img"src="https://miro.medium.com/max/1400/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg">
                        <a href="https://www.google.it">
                            <button id="fade">I'm disappearing, the image behind me should light up</button>
                        </a>
</div>

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

Utilizing GET parameters containing spaces

Is it possible to retrieve a variable that contains spaces? When I attempt to do so, only the first word is returned. I attempted to use the htmlspecialchars function, but it did not resolve the issue. In my code, within categories.php, I have the followi ...

HTML Buttons Remain Selected After Being Clicked

Currently working on a calculator app for a class project but I've hit a roadblock while setting up keyboard listeners. The keyboard functionality is fine, however, when it comes to clicking on buttons, specifically the non-keyboard functions, they re ...

Generating HTML table rows dynamically from objects using Angular's ng-repeat functionality

In my Node-RED setup, I am utilizing the HTML angular code within a "template" node. Within my system, I have an object that consists of circuit boards distinguished by serial numbers. Each circuit board is equipped with two channels, each having its own ...

Troubleshooting Safari's Issue with onclick() Functionality

I encountered an issue where the code was working perfectly on IE7 but not on Safari (5.0.5). I would like to find a solution without relying on jQuery. The ultimate goal is for this functionality to work seamlessly on iPad, but currently, I am testing it ...

Troubleshooting issue with AngularJS $http.get function failing to show retrieved

My data is showing up when I call the URL from dataSource, but it's not displaying when I use $http.get. I am sending this data to a dx-chart using a JsonResult from my controller. Below is the code snippet that includes my AngularJS file and MVC View ...

The erratic nature of Tailwind actions

Currently, I am immersed in a Livewire project and attempting to implement some Tailwind theme configurations. However, the process does not appear to be coherent whatsoever. Here is an excerpt of my Tailwind configuration: theme: { extend: { f ...

Comparison of universal and descending selector in CSS

Can someone clarify when to use the universal selector versus the descendant selector in CSS? I've tried finding a clear explanation, but haven't come across one that explains the difference and when each should be used: div * .test{ color: re ...

What is the best way to configure my card components for a flex display?

Currently, I am in the process of learning React. I have a file named new_card_component.js that is responsible for displaying data fetched from the data.js file. The issue arises in my AirBnB.js file, where I'm passing the AirbnbApp module to a secti ...

"Interaction with jQuery causes button element to shift position upon resizing of browser

I am encountering an issue with a div element that has jQuery appended to it for a bounce effect. The element is absolutely positioned within its relative parent. However, when the browser is resized, the child element does not remain centrally aligned dur ...

Using jQuery to store the name of a Div in a variable and subsequently invoking it in a function

Recently, I've been grappling with a task involving storing a div name in a variable for easier editing and incorporating it into standard actions like show/hide. My code functions perfectly without the variables, but introducing them causes the div ...

What is the most effective method for establishing a notification system?

My PHP-based CMS includes internal messaging functionality. While currently I can receive notifications for new messages upon page refresh, I am looking to implement real-time notifications similar to those on Facebook. What would be the most efficient ap ...

Dropdown menu similar to the one utilized in Bootstrap

I am curious to understand how the drop-down menu system in Bootstrap3's tutorial page operates. It seems that it collapses all subtopics under a main topic by default, allowing users to either click or scroll to expand further. Additionally, the menu ...

Background image for input button in Internet Explorer 6

Can you create a custom background image for the input type="button" in Internet Explorer 6? ...

Tips for accurately placing the iOS audio player in the footer section

In my web page, I have created a simple footer that shows the currently playing track title and includes an HTML audio player. The layout looks great on desktop browsers and Android devices, but on iOS, the player is positioned far below the footer. To ad ...

How do I create more space in Vitepress to prevent the text from feeling cramped and allow it to display in a larger area?

Below is the content of my index.md file: --- # https://vitepress.dev/reference/default-theme-home-page layout: home hero: name: "TP2" text: "Analyzing the code of TP2 by Breno Mazzali Medeiros Tomazelli and Philippe Bouchard" ta ...

Does the same rule apply in a CSS file for <variable name="key" value="#576767">?

While examining the HTML code of my blog, I came across a CSS variable declared in CDTA within the index.html file. <![CDATA[ <Variable name="keycolor" description="Main Color" type="color" default="#2196f3" ...

Troubleshooting Challenges in JavaScript/jQuery Hangman Game

Having trouble with my hangman game's game loop. Attempting to replace correct letters as the game runs through the word, but it's currently: looping through the word checking if the guessed letter is in the word returns index[0] Tried splitti ...

jQuery's show/hide functionality allows for the dynamic resizing of images,

I am experiencing an issue with a Joomla template that has a custom jQuery menu. When I hover over the map with my mouse, the overlay appears slightly larger than expected. This problem seems to be occurring in Firefox and IE 11, leading me to believe it ...

Is transforming lengthy code into a function the way to go?

I have successfully implemented this code on my page without any errors, but I am wondering if there is a more concise way to achieve the same result. Currently, there is a lot of repeated code with only minor changes in class names. Is it possible to sh ...

A guide on how to selectively blur and make the background transparent in jgrowl notifications without affecting the content

After some experimentation, I found that making the jGrowl notification background both transparent and blurry was not possible. When applying blur to the background, the content also became blurry, which is not the desired effect. If you want a transparen ...