Creative Ways to Use jQuery Hover Effects: Make One Div Vanish While Unveiling Another

I posted a question earlier, but I forgot to include the second part of it.

The initial question was about creating a hover animation (which is working perfectly). However, I am facing an issue where one div disappears and another one needs to appear in its place.

Below is the code snippet:

HTML:

 <div class="col-xs-12 col-md-4">
        <div id="beach2"></div>
        <div class="layer3"></div>
        <div class="wrapper">
               <div class="layer4">
                <div class="magnifyingGlass"></div>
                    <a href="#"><img src="images/magnify.png" class="img-responsive" id="magnify" alt="Magnifying Glass" /></a>
               </div>
               <div class="layer5">
               <div class="label">
                        <p class="title">Lorem ipsum dolor sit</p>
                        <p class="title2">amet consetetur sadipscing eltir</p>
                </div>
         </div>
</div> 

I want '.layer3' to disappear while '.layer4' and '.layer5' appear on hover.

CSS:

.layer3{
    width: 100%;
    height: 300px;
    margin-top: -340px;
    background-color: rgba(226, 151, 145, 0.5);
}

.wrapper{
    display: none;
}

#magnify{
    margin-top: -35px;
    padding: 10px 10px;
    z-index: 9999;
}

.magnifyingGlass{
    height: 34px;
    width: 34px;
    background-color: #1abc9c;
    z-index: 999;
}

.layer4{
    height: 44px;
    width: 44px;
    background-color: rgba(26, 188, 156, .5);
    margin: -210px 20px 0 0;
    float: right;
    padding: 5px 5px;
    position: relative;
    margin-top: -205px;
}

.label{
    height: 65px;
    width: 99%;
    background-color: #1abc9c;
    display: block;
    position: relative;
    z-index: 999;
}

.layer5{
    height: 75px;   
    background-color: rgba(26, 188, 156, .5);
    margin-top: -75px;
    padding: 5px 0 0 5px;
    position: relative;
}


.title{
    font-size: 18px;
    color: #fff;
    text-align: left;
    display: block;
    position: relative;
    padding: 10px 5px;
}

#title2{
    color: #087253;
    font-size: 12px;
    text-align: left;
    padding: 0 5px;
    display: block;
    position: relative;
}

jQuery:

$(document).ready(function(){
    $('.layer3').hover(
        function(){
            $(this).animate({opacity:'0'});
        },
        function(){
            $(this).animate({opacity:'1'});
        }
    );

    $('.wrapper').mouseover(
        function(){
            $(this).show();
        },
        function(){
            $(this).hide();
        }
    );
});

Can anyone provide suggestions on how to solve this problem?

Answer №1

Implement CSS for Transitions

.box2, .container { transition: background-color 0.5s }
.box2:hover, .box2:not(:hover) + .container { background-color: #f2f2f2 }

Live Demo.

The + selector targets the next sibling element, and the :not() pseudo-class is used to apply styles when the specified condition is false, like in this example where it applies when .box2 is not hovered.

Answer №2

Why not simply include the actions within the hover function itself?
If this is your goal, it might be beneficial to enhance your knowledge on jQuery and its selection capabilities.

$(document).ready(function(){
    $('.layer3').hover(
        function(){
            $(".layer4, .layer5").show(); // Just inserted this line
            $(this).animate({opacity:'0'});
        },
        function(){
            $(".layer4, .layer5").hide(); // Also added this line
            $(this).animate({opacity:'1'});
        }
    );

    $('.wrapper').mouseover(
        function(){
            $(this).show();
        },
        function(){
            $(this).hide();
        }
    );
});

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

Making a POST request to a REST service in Rails 3 using Phonegap

I've encountered an issue with my rails3 rest service while developing a phonegap app using HTML5 and jQuery javascript. Specifically, I'm struggling to make a POST rest call with ajax jQuery due to cross domain security restrictions. Does anyon ...

Guide on transferring images from user interface to server

I'm struggling with saving images to my database. When the Upload Image button is clicked, a modal opens with fields for Image Title and Input File. After clicking Save, the data goes to the image.js file. Can someone please explain how to save these ...

Inserting a pause between a trio of separate phrases

I am dealing with three string variables that are stacked on top of each other without any spacing. Is there a way to add something similar to a tag in the ts file instead of the template? Alternatively, can I input multiple values into my angular compo ...

What is the best way to show the number of times a button has been clicked using javascript?

My task is to create a button that updates the status bar after each click, displaying "You have generated numbers x times" where x represents the number of times the button has been clicked since the page loaded. Below is my current code for a random num ...

What is the best way to add data to a URL in an ActionResult Method using window.location.href?

I am having trouble understanding how to retrieve data using window.location.href = '/Product/Success/'+data.OrderTrackNo+'';. I am able to get data using ajax, but it seems different when retrieving data with window.location.href, whic ...

Implementing a click function that toggles between adding and removing a class, keeping track of the number of clicks, and utilizing localStorage to prevent repeated clicking in the

Hi there! I'm currently working on a simple widget that features a clickable icon and a counter next to it. When the icon is clicked, it should toggle between an empty heart and a filled heart using addClass/removeClass. Additionally, each click incr ...

Displaying image titles when the source image cannot be located with the help of JavaScript or jQuery

I am currently facing an issue where I need to display the image title when the image is broken or cannot be found in the specified path. At the moment, I only have the options to either hide the image completely or replace it with a default image. $(&apo ...

What is the best way to utilize clip() in html canvas when using text as a path?

I have written the code below, which is close to what I am trying to achieve but doesn't quite get there. My goal is to fill text in multiple colors, currently it is only in #FF00FF. Check out the Playground I suspect the issue lies in my lack of kn ...

What could be the reason for the gap that shows up when transitioning from one image to the

Check out my website at: This section discusses: https://i.sstatic.net/WTTxJ.gif At the bottom of the image, a 'gap' appears. Instead of using standard HTML img tags, I used Next.js Image like this: <Image src="https://ticket- ...

Text exceeding boundaries within a horizontal flexbox (accordion)

Need help with a flexbox (accordion) issue. The demo below showcases multiple groups of content, each contained within a cell in a horizontal flexbox. However, when the content within a group is extensive, it overflows the flexbox causing display problems. ...

Scrolling an HTML element to a specific value within its content

Code: /* -------------------------------------NAV & BANNER------------------------ */ #logo{ height: 60px; width: 60px; } .nav-color{ transition: 0.5s; background-color: rgba(0, 0, 0, 0.75); } .nav-null-color{ transition: 0.5s; } .banner{ ...

Sophisticated Arrangement of Div Elements

Here's a scenario where Two divs are dynamically styled from the Database. I am attempting to align two divs in the layout shown here: https://i.stack.imgur.com/nYI7v.png Can this be achieved using Bootstrap's CSS class col-? In responsive mo ...

Tips for updating a dynamic form with new information

Users can generate a form based on their selection: Here is the code for the form: <select id="skillcat" name="skillcat[]" class="form-control" onChange="getSkillDescription(this.value)"> <option value ="">SELECT SKILL CATEGORY</option> ...

The ASP.NET MVC3 form collection registers as 0 when performing a jQuery ajax post

I'm currently developing a project on ASP.NET MVC3. My current challenge involves POSTing to a method that should return a set of data using the jQuery.ajax api. However, upon handling the request on the server, I noticed that the form collection&apo ...

Creating a secure Angular pipe to prevent DomSanitizer from removing content

I am currently utilizing Angular 9 to fetch movie data using the Youtube API. As a result, I had to develop a custom pipe in order for my links to function correctly within HTML Below is the code snippet for the pipe: import { Pipe, PipeTransform } fro ...

Hovering over an image and trying to rotate it results in

Check out my rotating image example on hover in React here This effect utilizes scale(), rotate(), and transition properties to create an animated rotation when hovering over the parent element. Additionally, overflow: hidden is applied to the parent elem ...

Achieving Equal Height for Two Bootstrap 4 Columns with Scrollbar

I've been searching everywhere for a solution to this problem. My goal is to design a page with a video player on the left side and a chat on the right side. I am using the embed-responsive class for the video player so that it adjusts its size based ...

Is it possible to adjust the position of a h2 heading on a particular webpage using CSS?

There is a situation where multiple pages contain h elements nested inside a standard container div class. When trying to edit a specific h2 element in a specific page using CSS, simply editing the h2 or the container does not work. Other methods attempte ...

Steps for assigning attributes to a variable when selecting multiple checkboxes

Hey there! I have a grid set up that looks like this: Serial No Document Name Attachment 1(checkbox) abc (img) 2(checkbox) xyz (img) 3(checkbox) uio ...

Reducing HTML content to 20 words efficiently through the use of JS and VueJS

Currently in the process of developing a news feed using VueJS and have encountered an issue with rendering the content. Unfortunately, the API I am working with does not allow for easy customization to fit my specific needs. This API provides all the cont ...