Conceal image description while hovering and reveal overlay text

Navigating image captions: I'm looking to hide the title caption on hover and display overlay text instead. However, when hovering over the image, both the title and overlay text disappear. Where am I going wrong with this?

If anyone could provide insight into what might be causing this issue, I would greatly appreciate it.

$(document).ready(function() {

  $('.overlay').hide();
  $('.section-solution').hover(
    function() {
      $('.hide-on-hover').hide();
      $('.text').show();
    },

    function() {
      $('.hide-on-hover').show();
      $('.text').hide();
    }
  );

});
.container-img {
  position: relative;
  padding: 20px;
}

.wp-caption {
  position: relative;
  padding: 0;
  margin: 0;
}

.fullwidth-img img {
  margin-bottom: 70px;
  width: 100%;
  height: 400px;
  object-fit: cover;
}

.wp-caption img {
  display: block;
  max-width: 100%;
  height: auto;
}

.wp-caption-text {
  display: block;
  position: absolute;
  width: 100%;
  color: #fff;
  left: 0;
  bottom: 0;
  padding: 4em;
  font-weight: 700;
  z-index: 2;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.wp-caption-text p {
  color: white;
  font-size: 26px;
  font-weight: 500;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: black;
  color: white;
  font-size: 20px;
  padding: 2em;
}

.container-img:hover .overlay {
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<section class="section-solution ">
  <div class="container">
    <div class="row">
      <div class="container-img fullwidth-img" id="last">
        <figure class="wp-caption">
          <div class="demo">
            <img width="300" height="200" src="https://img.purch.com/w/660/aHR0cDovL3d3dy5saXZlc2NpZW5jZS...>
          </div>
          <div class="overlay">
            <div class="text">
              <figcaption class="wp-caption-text on-hover">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</figcaption>
            </div>
          </div>
          <figcaption class="wp-caption-text">
            <p class="hide-on-hover">
              Lorem ipsum
            </p>
          </figcaption>
        </figure>
      </div>

    </div>
  </div>

</section>

Explore the fiddle here: https://jsfiddle.net/98j07g4k/1/

Answer №1

Don't include $('.overlay').hide(); in the code to make it functional.

$(document).ready(function() {

  
  $('.section-solution').hover(
    function() {
      $('.hide-on-hover').hide();
      $('.show_text').show();
    },

    function() {
      $('.hide-on-hover').show();
      $('.show_text').hide();
    }
  );

});
.container-img {
    position: relative;
    padding: 20px;
}

.wp-caption {
    position: relative;
    padding: 0;
    margin: 0;
}

.fullwidth-img img {
    margin-bottom: 70px;
    width: 100%;
    height: 400px;
    object-fit: cover;
}

.wp-caption img {
    display: block;
    max-width: 100%;
    height: auto;
}

.wp-caption-text {
    display: block;
    position: absolute;
    width: 100%;
    color: #fff;
    left: 0;
    bottom: 0;
    padding: 4em;
    font-weight: 700;
    z-index: 2;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
.wp-caption-text p {
    color: white;
    font-size: 26px;
    font-weight: 500;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: black;
  color: white;
  font-size: 20px;
  padding: 2em;
}

.container-img:hover .overlay {
  opacity: 1;
}
<section class="section-solution">
<div class="container">
    <div class="row">
                    <div class="container-img fullwidth-img" id="last">
                        <figure class="wp-caption">
                            <div class="demo">
                                <img width="300" height="200" src="https://img.purch.com/w/660/aHR0cDovL3d3dy5saXZlc2NpZW5jZS5jb20vaW1hZ2VzL2kvMDAwLzEwMC8zNDkvb3JpZ2luYWwvc2liZXJpYW4tdGlnZXIuanBn">
                            </div>
                            <div class="overlay">
                              <div class="text"><figcaption class="wp-caption-text on-hover"><p class="show_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p></figcaption></div>
                            </div>                            
                            <figcaption class="wp-caption-text">
                              <p class="hide-on-hover">
                              Lorem ipsum
                              </p>
                            </figcaption>
                        </figure>
                    </div>                    
                       
</div>
</div>

</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Answer №2

A different approach could be to conceal the title using CSS without relying on JavaScript. By targeting the hide-on-hover class in your stylesheet and adding display: none;, you can achieve the desired effect.

Here is an example of how it would appear:

.container-img {
    position: relative;
    padding: 20px;
}

/* Other CSS rules omitted for brevity */

.container-img:hover .overlay {
  opacity: 1;
}

.container-img:hover .hide-on-hover {
  display: none;
}
<section class="section-solution ">
<div class="container">
    <div class="row">
        <div class="container-img fullwidth-img" id="last">
            <figure class="wp-caption">
                <div class="demo">
                    <img width="300" height="200" src="https://example.com/image.jpg">
                </div>
                <div class="overlay">
                    <div class="text"><figcaption class="wp-caption-text on-hover"><p class="show_text">Sample text</p></figcaption></div>
                </div>                            
                <figcaption class="wp-caption-text">
                    <p class="hide-on-hover">
                    Hide this content
                    </p>
                </figcaption>
            </figure>
        </div>                    
    </div>
</div>

</section>

This method could be one of the simplest and most efficient options available.

Answer №3

Replace your javascript file with this code snippet and I will include comments for better understanding.

$(document).ready(function() {

        var trade = true; // Control variable to track mouse hover over image.

        $('.overlay').hide();
            $('.section-solution').hover(
                function () {
                    if(trade) {
                      $('.hide-on-hover').html("test");
                      $('.text').show();
                      trade = false;
                    } else {
                        // Set new text content dynamically.
                        $('.hide-on-hover').html("Lorem ipsum");
                      trade = true;
                    }
                }
            );

         });

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

Do not use href for ngview change

I have been using ng-view to switch views by setting href="/..." in anchor tags and clicking on them. However, I am now looking for a way to change the view within the ng-view tag based on an event. Can anyone provide guidance on how to accomplish this? ...

What is causing the large build size when using Webpack with Vue.js?

After cloning the repository at: https://github.com/Microsoft/TypeScript-Vue-Starter I executed npm scripts: npm install npm run build The outcome: the size of build.js file is approximately 1MB. Can anyone explain why the build.js file is significant ...

Add the div id to the script

Currently, I have a script set up where by clicking on a specific div (for example id=packtoa), it will add a class 'show' to listview items that have a matching class with the clicked div's id. While this system works well, I find myself n ...

Tips for incorporating a JavaScript script into local HTML code

I am facing an issue with my code on jsfiddle. It works perfectly there, but when I try to run it locally, it doesn't seem to work. I have checked the code multiple times and even downloaded the jQuery file to link it, but still no luck. I feel like i ...

React JS Calendar

I'm currently working on a project where I need to create a calendar component using React. The design requires the days to be displayed like in the Windows calendar, starting from Sunday even if it's another month and ending with Saturday. The ...

Tips for making a React/Socket.IO app accessible from external sources

Trying to get my React app (built with Node.js and Socket.IO) running locally and accessible from external sources has been a challenge for me. I've managed to expose port 3000 using ngrok tunneling, but my socket connection is listening on port 3001. ...

"Clicking on AppBar MenuItem functions properly on desktop, but it does not work on mobile devices in Material UI using

Within my reactjs application that employs Material UI, I have a Appbar containing a dropdown menu positioned in the top right corner. On desktop, the onClick function for each MenuItem works as expected. However, on mobile devices, the function does not t ...

Retrieve the updated text content from a textarea using JavaScript/jQuery

For a beginner in javascript and jquery like me, I encountered an issue with a textarea element that has preset content. When I change the value on the site to "new" and click the "showme" button, the alert box displays the preset value instead of the new ...

Discover the methods for accessing an array of IDs by implementing an event trigger in conjunction with checkboxes

Can you assist me with a dilemma I'm facing? I've developed a static form containing numerous checkboxes and checklists. My challenge arises when trying to integrate a JavaScript code that automatically selects subcategories when parent checkboxe ...

Scaling and mapping textures onto meshes using Three.js TextureLoader

I have a cube and I'm attempting to project an image onto it. I am using a loading Manager to load the image, but I am encountering an issue where material.map is showing as undefined. I suspect that there may be a problem with scaling. The original i ...

Why isn't the calendar appearing below the textbox for me?

I found this code on a website and I'm having trouble getting the expected output. I am new to this, but the calendar is not displaying below the textbox as it should according to the code. <!DOCTYPE html> <html xmlns="http://www.w3.org/1 ...

What causes the server to give an incorrect response despite receiving a correctly read request?

After setting up a new project folder and initializing NPM in the Node.js repl, I proceeded to install the Express package. In my JavaScript file, I included the following code: const express = require('express'); const app = express(); ...

Implementing a dynamic menu feature using Ruby on Rails

I have created a secondary navigation bar to help users easily navigate through different sections of their account. DASHBOARD | ACTIVITY | PORTFOLIO | PROJECTS My goal is to make the current page stand out by making its CSS bold. For example, when a use ...

Can we specify the inherit value for font family on an element to ensure consistent style when the specified font is not available?

Suppose I have a page with the following HTML content: <body style="font-family:Helvetica"> <span style="font-family:Segue">Hello World</span> </body> Would it be considered valid to set the font family of the span to Segue, ...

Handling Exceptions in Node.js and Express

Having recently delved into the world of javascript and node, I've been working on creating an application using node.js and express. While I've implemented appropriate error callbacks in my code, there are instances where the node.js server abr ...

What is causing two inline-blocks not to align at the top within the parent wrapper div?

I have set up my HTML structure in the following way: <div id="wrapper"> <div id="main"> <p>test</p> </div> <div id="sidebar"> <p>test</p> </div> </div> Here is t ...

Using Webflow code in ReactJS/NextJS: Best practices and implementation tips

After exporting my Webflow code to my NextJS app, I noticed that many of the HTML tags are being flagged as errors because they appear to be missing closing tags. Can anyone shed some light on why this is happening and suggest potential solutions? It&apos ...

What is the most effective way to loop through a JSON object and apply filtering based on user input?

I am currently working with a dataset stored in a MongoDB database, and below is the format of the data. I have been looking for something similar to streams functionality in Java but haven't had any luck. Data from the Database: [ { &quo ...

Error: Unexpected identifier encountered while using the bcrypt module

Working on a JavaScript project, I created a file called app.js. In this file, I implemented a login function using bcrypt for encryption and MySQL for the database. However, I am facing an error and having trouble identifying which part of the code is ca ...

Two jQuery event handlers with similar functionality are displaying distinct behaviors

I've encountered an issue with two identical document fragment objects where separate event listeners are attached using jQuery, as demonstrated in this fiddle. Although the two event listeners should function the same way, only the first one behaves ...