The function to make the text div fade in and out when hovering over an image is not functioning

I want to create an effect where a caption div fades in directly over an image when the mouse enters the div and fades out when the mouse leaves. Additionally, I would like to implement a click toggleClass function to select and deselect the image. When the image is selected, the caption should remain visible. When the image is deselected with a click, the caption should fade out.

In summary: 1) Use mouse enter/leave to fade in/out the caption. 2) Implement click functionality to toggle the select class for displaying or hiding the caption.

To view the FiddleJS example, visit: http://jsfiddle.net/jhyqt5/cBsqN/

HTML:

 <div class="caption">Into the Night</div>

<img src="https://scontent-a-pao.xx.fbcdn.net/hphotos-frc1/189612_10100244250167513_7332862_n.jpg" class="img-circle">
</div>

CSS:

.img-circle{
    border-radius: 50%; height: 140px; width: 140px;
}
.caption{
    background-color: black; opacity: .7;
    text-align: center; line-height: 120px; color: white;
    position: absolute; display: none;
    border-radius: 50%; height: 140px; width: 140px;
}

JS:

$('.caption').mouseenter(function(){
    var image = $(this).find('img'),
        caption = $(this).find('div');
    caption.height(image.height());
    caption.width(image.width());
    caption.fadeIn();
}).mouseleave(function(){
    var image = $(this).find('img'),
        caption = $(this).find('div');
    caption.height(image.height());
    caption.width(image.width());
    caption.fadeOut();
});

Answer №1

To start off, make sure to include jquery in your fiddle. You can create the hover effect using just CSS without relying on jquery for anything other than one specific aspect. Simply wrap your image and overlay in a wrapper div and apply some basic CSS rules.

CSS:

.wrapper{
    position:relative;
}
.caption {
    background-color: black;
    opacity: .7;
    text-align: center;
    line-height: 120px;
    color: white;
    position: absolute;
    display: none;
    border-radius: 50%;
    width:140px;
    height:140px;
}
.wrapper:hover .caption{ /*Display when hovered*/
     display: block;
}
.wrapper.active .caption{ /*Always display when parent has active class*/
     display: block;
}

HTML:

<div class="wrapper">
    <div class="caption">Into the Night</div>
    <img src="https://scontent-a-pao.xx.fbcdn.net/hphotos-frc1/189612_10100244250167513_7332862_n.jpg" class="img-circle">
</div>

JS:

$('.wrapper').on('click', function () {
    $(this).toggleClass('active'); //toggle class on each click
});

Check out the Demo here

Answer №2

Looks like your HTML is not properly formatted, give this a try to achieve the fade effect

<div class="image">
<div class="caption">Into the Night</div>

<img src="https://scontent-a-pao.xx.fbcdn.net/hphotos-frc1/189612_10100244250167513_7332862_n.jpg" class="img-circle">
</div>

Javascript

$('.image').mouseenter(function(){
    var image = $(this).find('img'),
        caption = $(this).find('div');
    caption.height(image.height());
    caption.width(image.width());
    caption.fadeIn();
}).mouseleave(function(){
    var image = $(this).find('img'),
        caption = $(this).find('div');
    caption.height(image.height());
    caption.width(image.width());
    caption.fadeOut();
 });

You can view the demo here

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

Substitute the aircraft in the cosmos with ThreeJS

I am trying to adjust the position of a plane in ThreeJS to a specific Z value. Using shapeBufferGeometry with vectors containing x, y, and z values. Below is my code and the current output. I want the white plane to align with the aqua lines. The code ...

Correct validation is not achieved when the "!" symbol is used in the matches function

I am working on a React and Next.js project where I am using Formik for handling forms and Yup for validations. One specific input field requires some validations to be performed. This field must be required, so if the user does not enter any information, ...

Vertical progress bar containing data percentage and CSS not rendering properly

I have been attempting to create a vertical progress bar using the data-procentage tag. I have almost completed it, but for some reason, the progress bars are not aligned properly and I can't figure out why. This is what my code looks like so far: T ...

Ways to create a clickable red button

I'm working with bootstrap, CSS, and particle.js for this project. I have also linked a custom CSS file here. However, when I apply bootstrap and particle.js, the red color generate password button becomes unclickable. After checking in Chrome dev too ...

Implement a polyfill for the requestNextAnimationFrame function

The code snippet below is taken from the Core HTML 5 canvas: window.requestNextAnimationFrame = (function() { var originalWebkitRequestAnimationFrame = undefined, wrapper = undefined, callback = undefined, geckoVersion = 0, userAge ...

Issue encountered when attempting to switch the style of a header element from an HTML h3 tag to an h

One of the challenges I'm facing involves an HTML h3 tag, as shown below: <div class="centrar"> <h3>Lorem ipsum dolor!</h3> </div> I attempted to change it to an H4 using CSS, but unfortunately, my efforts have been u ...

Using rowspan to display JSON data in AngularJS

Unique Json data: [{ cityName: Seattle, population: 1000000, rank: 1 }, { cityName: Portland, population: 800000, rank: 2 }, { cityName: San Francisco, population: 900000, rank: 3 }, { cityName: Los Ange ...

Python Selenium does not show any variations in Performance.timing values for multiple requests on the same page while measuring page load time

My goal is to measure page load times using Python and Selenium by executing the JavaScript command performance.timing. When navigating to another page, I can retrieve the values correctly. However, when performing a transaction or action from the same pa ...

Establishing a minimum width for the value of zero in AmCharts column series by customizing the bullet labels

Here's an interesting example where I have arranged column series with bullet labels. However, you may notice that there are some 0 values present as well. My goal is to establish a minimum width for the series so that even if the value is small or 0, ...

Please insert a decimal point and thousand separator into the text field

I'm trying to incorporate thousand separators and decimal points into my text box. Additionally, I have implemented the following directive: .directive('format', function ($filter) { 'use strict'; return { requir ...

Customizing React-Data-Grid styles using Material-UI in a React application

Imagine a scenario where we have a file containing themes: themes.js: import {createMuiTheme} from "@material-ui/core/styles"; export const myTheme = createMuiTheme({ palette: { text: { color: "#545F66", }, }, }); In ...

When using React, the auto-fill suggestions in forms are not displayed

Looking to enhance a React panel for adding new products, I've developed a separate auto-complete class. This special class should display an input field along with a list of suggested autofill items below. However, it seems that only the input elemen ...

CSS3 translate3D causing input element glitches on Android Webkit

I am encountering some challenges with the Input element in a Webkit Android application that I am currently working on. While testing on version 2.X, it seems that version 3.x does not exhibit these same issues... The structure of the app involves indivi ...

Is there a way to ensure that my mouseenter function does not run when it is currently in the process of running?

Issue at hand: Currently, I am tackling a problem with the mouseenter function $('.filmstrip').bind('mouseenter',function(){ var isRunning = false; var $elem = $(this), width = $elem.width(), ...

Why isn't my Vue2 data updating in the HTML?

Starting my journey with Vue, I have been finding my way through the documentation and seeking support from the Vue community on Stack Overflow. Slowly but steadily, I am gaining a better understanding of how to create more complex components. The issue I ...

What is the best way to display up to 8 items in a row using flexbox?

I'm looking to display my items in a grid system with equal spacing between each item per row. While using space-between works for the most part, I've encountered an issue with odd numbers of items. In this case, the last items in a row should be ...

I am having trouble getting my navigation bar to center itself

I'm struggling to center my navbar. My goal is to have the entire navbar consistently at the bottom of the screen, with the logo in the exact middle and two buttons on each side that stretch to the edges. Below is the current code I'm using: .n ...

Does every controller page need to verify the Login Function?

In my PHP pages, I have implemented the MVC Pattern by creating a controller page for each view page to interact with the Model page. However, I have only checked user login at the top of every view page and not in the controller page. This leaves a potent ...

Creating an input in Vue3/Javascript that restricts input to only numbers seems to be a challenge

I have a project that involves an input field which should only accept numerical values. Within the template, I've set up an input element with the value bound to a variable and the input event linked to a function that verifies if the entered value ...

Achieve dynamic styling and adjust window size using window.open

My mind is exhausted after days of trying: function prtDiv( o ) { var css = 'body {font:normal 10px Arial;}' ; var wo = window.open('','print','width=600,height=600,resizable=yes'); wo.document.write( '<he ...