Is there a way for me to have an image smoothly ascend as the caption rises?

I have put together the following html snippet: http://jsfiddle.net/4gDMK/ Everything is working smoothly, but I am struggling to figure out how to simultaneously move both the captions and the image upwards so that the entire image is no longer visible.

Below is the basic html code:

<figure>
    <img src="http://4.bp.blogspot.com/_1vc-O2P4PHQ/S1bI97AgDoI/AAAAAAAACxs/MOo533hzeSw/s200/technical_stockphoto2.jpg" alt="Stockphoto 1" />
    <figcaption>Caption text</figcaption>
</figure>

For reference, check out this website for an example: On certain blocks, you can click, which then reveals the caption and moves the image upwards. This is what I am aiming for, but on hover instead of click.

If anyone has insight on how to achieve this effect, please let me know!

Best regards

Answer №1

Alright, brace yourself for a lot of changes coming up.

Let's simplify the CSS to this (since all effects will be handled by JQuery):

figure {
    display: block;
    position: relative;
    float: left;
    width:200px;
    height: 200px;
    overflow: hidden;
    margin: 20px;
}
figure img {
    display: block;
    max-width:200px;
}
figcaption {
    position: absolute;
    background: black;
    background: rgba(0, 0, 0, 0.75);
    color: white;
    padding: 10px 20px;
    width:160px;
}

This setup ensures that the caption remains hidden beneath the image until it is ready to be displayed.

Now onto the JavaScript:

//Credit goes to roXon
$(function(){
    $('figure').on('mouseenter mouseleave', function( e ){
        var toPos = e.type=="mouseenter" ? $('figcaption',this).innerHeight() : 0 ;
        $('img', this).stop().animate({marginTop: -toPos}, 200);
    });
});

When hovering over any figure, the caption below it expands in height, revealing itself and covering the top part of the image. Moving the cursor away resets the margin back to 0.

Updated fiddle link: http://jsfiddle.net/NkXCd/1/

Opting for CSS3 instead of JQuery (suggested by roXon once again)

figure {
    display: block;
    position: relative;
    float: left;
    width:200px;
    height: 200px;
    overflow: hidden;
    margin: 20px;
}
figure img {
    display:block;
    max-width:200px;
    margin-top:0;
       -webkit-transition: 0.3s;
           transition: 0.3s;  
}
figcaption {
    position: absolute;
    background: black;
    background: rgba(0, 0, 0, 1);
    color: white;
    padding: 10px 20px;
    width:160px;
}

figure:hover img{
    margin-top:-40px;
}

CSS3 Fiddle link:http://jsfiddle.net/NkXCd/2/

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

I cannot locate the dropdown list anywhere

As a beginner in html and css, I decided to follow a tutorial on youtube. The tutorial focuses on creating a navigation bar with dropdown menus using html and css. I wanted the names Ria, Kezia, and Gelia to be displayed when hovering my mouse over the Su ...

Steps to deactivate child rows in a Responsive Datatable using jQuery after pagination

Procedure steps: I followed a tutorial on how to render content in a responsive table, you can check it out here: https://datatables.net/extensions/responsive/examples/child-rows/custom-renderer.html When I click on the expand button in any row on pag ...

"Enhancing your text input fields with the CKEditor jQuery plugin and

Currently, I am working with ckeditor and utilizing the jquery plugin to initialize everything upon document loading. My goal is to establish a blur event for the created instance of ckeditor. The snippet below showcases how I am initializing ckeditor: $( ...

Exporting CSS file from css-loader in Webpack: A step-by-step guide

My application structure is set up like this: /src /app.js /styles.css /images /img.png The content of the app.js file is as follows: const styles = require('./styles.css'); console.log(styles) // => I want a URL to t ...

Unlocking two features with just a single tap

I am currently working on a website for a kiosk, where the site transitions like a photoslide between each section. To achieve this effect, I have added a layover/mask on the initial page. The layover/mask is removed using a mouse click function. This is ...

Toggle sideBar within an iFrame by implementing Media Queries

I have implemented media queries in my code to hide the .sideBar when the screen resolution is less than 768px, showing only the .main section. @media (max-width: 768px) { .sideBar { display: none !important; } } Now, I want to add a ha ...

Tips on adjusting the font size of headers in a Vuetify v-data-table?

I've been struggling to change the font size of the headers in my v-data-table. No matter what I try, the font doesn't seem to update. I even experimented with the Chrome developer tool and managed to make it work there. However, when I attempt t ...

Why does this function execute even after clicking only one of the two keys when using jQuery?

Here is the code snippet I am working with: $(document).bind('keydown', 'ctrl+1', function () { alert('You found the hotkey ctrl+1!'); }); However, when I press either the Ctrl or the 1 key, this code triggers. I specific ...

Exploring the functionality of $.param in jQuery

After extensive research online, I found that the most helpful information was on the official jQuery site. Here is the code snippet I am currently using: var param = { branch_id : branch_id}; var str = $.param(param); alert(str); However, when I log or ...

Issue with Angular: Unable to locate a differ that supports the object '[object Object]' of type 'object'. NgFor is only compatible with binding to Iterables such as Arrays

As someone who is new to Angular, I am facing a challenge while working on my portfolio project. The issue arises when trying to receive a list of nested objects structured like this: "$id": "1", "data": { &quo ...

error: cannot parse data with Jquery, AJAx, and JSON

Can you assist me with this issue: I am currently building a search page using PHP and attempting to fetch a specific record from a MySQL table through JQuery-AJAX-JSON. However, the problem I'm facing is that I do not receive any data from process.p ...

What is the best way to utilize toggle("slide") in order to reveal a message letter by letter?

I am currently experimenting with the `.toggle("slide") function to create an effect where a piece of text appears as though each letter is sliding in. However, I've noticed that instead of a smooth slide, it looks more like the text is flying in abru ...

What is the best way to ensure my responsive form is centered and aligned properly with all other elements on the page

Link to website This final step is all that stands between me and the completion of my website deployment. However, I am faced with a persistent issue that has me stumped. It seems that there is an unexplained margin on the left side of the contact form, ...

Issue with Safari: Unable to trigger CSS :hover when tapping SVG element

On an iOS device in Safari, tapping this SVG element does not trigger the transition animation to fade in the replacement <g id="training-sub-menu">. The animation is confirmed to work, as there is a strange behavior where long-pressing whe ...

Is there a way to direct back to the AJAX error function from the application_error in the

Why does my code keep ending up in the AJAX success function instead of the error function? What mistake am I making? $.ajax({ url: url, type: "POST", data: data, contentType: "application/json; charset=utf-8", success: function(data) ...

Pressing the dart key will alter the sprite's direction in a 2D game

Recently, I've been working on a Dart game where the user controls a space ship sprite using the W, A, S, and D keys. Pressing these keys will move the sprite up, left, right, and down respectively. Additionally, pressing the Space bar will launch a p ...

Creating a Javascript countdown timer that does not involve displaying the

I stumbled upon this code on a website, but there's one tweak I'd like to make. Unfortunately, I can't seem to figure it out myself, so I'm reaching out for some help. What I want to achieve is removing the year from the date so that th ...

Ajax response values overlap

I am developing an application that requires multiple Ajax requests, but I encountered a problem where I am receiving the same response values for both requests. Each request must include a data field called activityCode, yet I keep getting the value of Sc ...

Tips for locating all events linked to a specific text box using JQUERY

I'm currently encountering a major issue with textboxes on a page that I've been tasked to update. Whenever I attempt to input text into the textboxes, the span element next to them disappears. My application is built using ASP.NET and relies on ...

Is it possible for you to simulate the shift key being pressed prior to the event execution?

Is there a way to allow the user to scroll left and right horizontally without having to hold the shift key down? I want to achieve this effect by setting the "shiftKey" variable to true even when it is not physically pressed. Any suggestions on how to ...