Mouseover Magic: Text and Captions Come Alive with JQuery

I am trying to create a rollover effect that displays text when the user hovers over an image. Despite researching numerous tutorials, I have found limited resources on incorporating text or captions using jQuery. Most of the available tutorials focus on CSS3 rather than just jQuery. Below is my attempt at implementing this feature:

jQuery

// The code below waits for all images to be loaded before executing
$(window).load(function(){
    // Loop through each description div...
    $('div.description').each(function(){
        //...set the opacity to 0...
        $(this).css('opacity', 0);
        //..set width same as the image...
        $(this).css('width', $(this).siblings('img').width());
        //...get the parent (the wrapper) and set its width same as the image width... '
        $(this).parent().css('width', $(this).siblings('img').width());
        //...set the display to block
        $(this).css('display', 'block');
    });

    $('div.wrapper').hover(function(){
        // When mouse hovers over the wrapper div
        // retrieve children elements with class description and show them using fadeTo
        $(this).children('.description').stop().fadeTo(500, 0.7);
    },function(){
        // When mouse exits the wrapper div
        // use fadeTo to hide the div
        $(this).children('.description').stop().fadeTo(500, 0);
    });

});

Here is the corresponding HTML...

HTML

<table id="intro"> 
    <tr>
        <div class="wrapper">
            <td>
                <a href="headwear.html"><img src="images/headwear.png" alt="headwear" /></a>
                <div class="description">
                    <div class="description_content">
                        This is just a test.
                    </div>
                </div>
                <a href="apparel.html"><img src="images/apparel.png" alt="apparel" /></a>
                <a href="accessories.html"><img src="images/accessories.png" alt="accessories" /></a>
            </td>
        </div>
    </tr>
</table>

This is how the styles are configured...

CSS

div.description{
    position: absolute; /* absolute position (so we can position it where we want)*/
    bottom: 0px; /* position will be on bottom */
    left: 0px;
    display: none; /* hide it */
    /* styling below */
    background-color: black;
    font-size: 15px;
    color: white;
}
div.description_content{
    padding: 10px;
}

div.wrapper{
    position: relative; /* important(so we can absolutely position the description div */
}
#intro {
    text-align: left;
    margin-right: 5px;
    width: 1100px;
}
#intro img {
    margin: 5px;
}

Fiddle

http://fiddle.jshell.net/yutikohercell/brxu8w8m/

Live

http://jsfiddle.net/yutikohercell/brxu8w8m/embedded/result/

Answer №1

Give this a shot: Try adjusting the CSS for div.description by removing display:none and adding opacity : 0. Keep in mind that using FadeTo will only alter the opacity, not the display property like block.

$('img').hover(function() {
     $(this).closest('a').siblings('.description').fadeTo(500, 0.7); // locates the parent 'a' element of the hovered image and then targets its sibling with the class 'description'.
  }, function() {
     $(this).closest('a').siblings('.description').fadeTo(500, 0);
  });

http://example.com/unique-link

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

Activate the function for items that are overlapping

Here is a scenario I am working on: HTML <body> <div> <p>Text</p> </div> <body> JQuery $('div').click(function(){ $(this).find('p').fadeToggle('fast'); }); $('bo ...

Extracting information from XML to display on my HTML webpage

I am utilizing the PHP library SimpleXML to extract information from an existing XML file on my website and incorporate it into an HTML page. Below is a snippet from the XML located at : <DTilt> <frontSide imagePath="94341/20130510150043_ACX ...

Using Jquery variables for calculations and they do not work with numerical values

Hey there, I'm new to this so bear with me. I have a jQuery question that's puzzling me... I'm attempting to set the line height using the following method: var line_height = $('.container').css("height"); console.log(line_height ...

Show a collection of elements containing attributes in JSX

I have a React component where I am displaying data from an array called pkgData: <div className="mainApp"> <div className="pkgsList"> {pkgData.map((p) => <h3 key={p.name}>{p.name}</h3> ...

Adding variables to a div using jquery

Is there a way to use jQuery to append variables to a div? Below are my codes, but I am looking to display div tags in the append. For example: .append("" + p + "") var image = item.image; var label = item.label; var price = item.price; ...

What is the best way to ensure that TwentyTwenty.js images are always positioned in the

Looking to utilize the TwentyTwenty.js code for image comparison, but facing an issue where the images are aligned to the left side. Is there a way to keep them centered without explicitly setting the width on the container? <div id="beforeafter" class ...

Padding in Bootstrap 4 columns in a vertical alignment when breaking the layout

I created a customized form-group to accommodate both large and small screens. The structure looks like this: <div class="form-group row"> @Html.LabelFor(model => model.ValidFrom, "Valid from", htmlAttributes: new { @class = "col-lg-3 ...

I'm struggling to make background-image display properly in my CSS code

In my quest to enhance the appearance of my website, I've been grappling with getting the background-image property to cooperate in CSS for the past three days. It's frustrating because I've successfully implemented this feature before, but ...

Dynamic background featuring a tall vertical image

I have an image that is extremely long, measuring 2263 x 8192 pixels. I want to design a website with a background that spans the width of the window and the full height of the image, all while maintaining responsiveness. The concept is to have this elong ...

Issue with selecting text using jQuery draggable

Is it possible that there is a bug with the selection of text underneath draggable elements in this example? This issue also seems to affect form elements. Here is where you can view the example: http://jqueryui.com/demos/draggable/handle.html Any sugge ...

Implementing hover effect triggered by keyboard input

Is there a way to create a hover effect on a div when a specific key is pressed on the keyboard using jQuery? <div id="d-up" class="button"></div> Appreciate any help with this! Thank you. ...

Display the div element only when the mouse is hovering over the button

I need a hidden text inside a div that will only appear when the mouse pointer is hovering over a specific button. Here is my current code: // Show help-text on hover $("#help-container") .mouseover(function(e) { $(this).find("#help-t ...

Firefox failing to interpret nested element background

I'm facing an unusual problem with Firefox while developing a menu that has a maximum of three levels. The issue seems to be isolated to Firefox as everything works perfectly in Chrome. When I click on 'First Level Menu', a ul opens with two ...

Hide a div when multiple classes are filtered using jQuery

I have several divs with the class .item. When the user clicks on the Classfilter submit button, all .item elements that also have at least one class from the dateClasses array (for example ['28.09.2015', '29.09.2015']) should be hidden ...

Tips for toggling password visibility by clicking outside a password field

As I work on creating a password field that allows users to toggle the visibility of their password by clicking an eye icon, I am facing a challenge. I want the password to be hidden automatically when the user clicks outside the field, which requires a co ...

Step-by-step guide for embedding a Big Commerce website into an iframe

Can a website be opened inside an iframe? If not, is it possible to display a message like 'page not found' or 'this website does not allow data fetching in iframes'? <!DOCTYPE html> <html> <body> <h1>Using the ...

Steps for navigating to a different page by clicking on a gridview row

Currently, I am utilizing a grid view on my webpage. My specific request is that upon clicking on any row within the grid, it should redirect to a separate page where all the details of the selected row will be displayed. Appreciate your assistance! ...

Ensure that three spans are precisely aligned to occupy a single line

My dilemma is as follows: I have a line within a document that has a set width. Now, there are 3 elements involved - one <a> tag and two <span> tags. The content in the <a> tag varies in width each time it appears on the line. The thi ...

Arrange elements in a vertical layout and align them to the left when they exceed the boundaries of their container

I am seeking a solution for laying out my design, similar to the one shown in this image Essentially, I have a container with a fixed height of 300px. I want to display a list vertically with each item taking up a width of 33%. If the list becomes too lon ...

The mystery behind the enigmatic combination of ajax, JQuery,

Seeking Assistance! All fields are displaying undefined values function UpdateData(){ var id = $('#id').attr('value'); var name = $('#name').attr('value'); var department = $('#departament'). ...