Using jQuery to animate a div when clicked by the mouse

Here's the scenario: I have 3 block elements arranged in a specific order:

  1. Image
  2. Hidden Text Block (.hidden)
  3. Footer Block (.blockimage)

When the page loads, the image is displayed over the hidden text block (which contains further information), followed by a small header block. When the .blockimage is clicked, I want the .hidden block to slide above the image.

Currently, the .hidden element has been set to position:absolute and has inline style display:none. If I inspect the .hidden element and uncheck display:none, it appears as expected. However, I am having trouble animating it on a click event.

This is what I have attempted so far:

$('.blockimage').click(function() {
  $('.blockimage .hidden').slideUp('fast', function() {
    // code to remove display:none from inline CSS of .hidden and slide content up (the class already exists)
  });
});

Without considering rotation, the slide-up effect should be similar to this example: http://css-tricks.com/examples/SlideupBoxes/

Any assistance would be greatly appreciated :)

Answer №1

One effective method is to create a parent div element that contains both the image and the information, set to have overflow: hidden, and then animate its position upon clicking. You can view a live demonstration here.

HTML

<div class="container">
    <img src="{yourimage}" />
    <div class="info-block">
     {information}
    </div>
</div>

CSS

Below is the essential CSS needed for this functionality.

.container {

  position: relative;
  overflow: hidden;

}

.info-block {

  position: absolute;
  bottom: -xx; // adjust as per your element height

}

Javascript

jQuery(document).ready(function(){

  $('img').click(function(){

    $(this).siblings('.info-block').animate({
      bottom: '0'}, 'fast');

  });

});

Update: Revert to Original State (using jQuery or CSS3 Transition)

If you wish for it to return to its initial state, you can enhance the provided code by having your JavaScript check the current bottom property like this:

if( $(this).siblings('.info-block').css('bottom') !== '0px' )
    $(this).siblings('.info-block').animate({bottom: 0}, 'fast');
else
    $(this).siblings('.info-block').animate({bottom: '-{some-other-height}'}, 'fast');

You can see a functioning example here.

In such cases, using a CSS3 transition might be preferable for cleaner and more efficient code. Even if you do not require it to revert back, transitioning can still be useful. In those instances, I found jQuery to be quicker.

Solution 2: Implementing CSS3 Transitions

To utilize CSS3 Transition, define an additional class as follows:

.info-block.shown {

    bottom: 0;

}

Then, simply toggle this class using jQuery on click events:

$('img').click(function(){

      $(this).siblings('.info-block').toggleClass('shown');

});

See a functional demo here.

Answer №2

Instead of targeting all elements with both classes, you can try using $('.hidden').slideUp.

Another option is to use show instead of slideUp for displaying the selected elements. You can find more information about slideUp 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

Utilizing AJAX to dynamically update a div's content by extracting a specific div from the retrieved data

Although I believe my code is correct, I am not very familiar with AJAX and have been struggling for hours to get it right. I've tried various approaches, including using filters, but nothing seems to work. The issue I'm facing is that the chat m ...

Disappearing Into the Background Excluding Specific Divs

I have a dilemma with fading in and out a background image using jQuery fadeIn and fadeOut. The issue arises because my wrapper div contains elements such as sidebar and navigation that are positioned absolutely within the wrapper div, causing them to also ...

The sliding effect in react-owl-carousel seems to be malfunctioning once new data is dynamically incorporated into the carousel

Hey there, I'm currently working on a React JS project where I am utilizing react-owl-carousel. Initially, I'm loading 5 items through an API call, and then upon clicking the next button, I make another API call to fetch more data. However, I&apo ...

Reveal the concealed button with a jQuery click event

I have a simple question that has been elusive to find an answer for on the internet. Can anyone please help? <input hidden="true" class="btnsubmit" id="myaddslide2" onClick="UPCURSLIDE()" type="button" value="UPDATE"> <script> function ...

Removing scrollbar from table in React using Material UI

I successfully created a basic table using react and material UI by following the instructions found at: https://material-ui.com/components/tables/#table. The table is functioning properly, but I am finding the scrollbar to be a bit inconvenient. https:// ...

Flyer - Chart "dimmed" and unselectable when dimensions are specified as a proportion

I am currently working on displaying a map within a container. I have successfully been able to display the map by setting its size to a specified number of pixels. However, my goal is to have the height of the map adapt to the size of the container dynami ...

Detecting collisions on a tile map using only JavaScript

Currently, I am developing a tile-based game using a traditional method (utilizing two for loops) and struggling with writing collision detection. I want the blue square to remain on the screen and appear on top of the tiles once the start button is clicke ...

Attempting to utilize Ajax to save a file in Laravel, however unsure of what may be causing the issue in my code

Looking for a way to save an image file using ajax in Laravel, here is the code I am currently using: Controller public function store(Request $request) { $item = new Item; // if remove $it_files_path , $it_files_name , $path code work co ...

Is there a way to increase the total of each row by two when a button is clicked?

Looking to enhance the sum of each row by two depending on whether a button is clicked. HTML: <table> <tr> <td> <input type="checkbox" class="prof" name="prof" value="0"> <input class=&quo ...

Does CSS padding-top: 100% take into account the parent's width?

I'm a bit perplexed by this discovery and I can't quite comprehend the rationale behind it. The provided code snippet showcases two nested DIVs. The outer DIV has specific dimensions and a relative position set, while the inner DIV also has fixe ...

Is there a way to identify whether the image file is present in my specific situation?

I have a collection of images laid out as shown below image1.png image2.png image3.png image4.png … … image20.png secondImg1.png secondImg2.png secondImg3.png secondImg4.png secondImg5.png ……. …….. secondImg18.png My goal is to dynamically ...

Discovering parent elements far up the DOM hierarchy using jQuery

I'm a bit confused about how to locate an element that is a parent element further up the tree. $('.btn-action').hover( function(){ $(this).find('.product-card').addClass('animated bounce'); }, function(){ ...

Updating the Animation for Datepicker Closure

While using the date picker, I want it to match the width of the input text box. When closing the date picker, I prefer a smooth and single motion. However, after selecting a from and to date, the datepicker no longer closes smoothly. I have attempted sol ...

Using Props with jQuery in React Components: A Comprehensive Guide

I trust you comprehend this straightforward example. I attempted to modify the background color of my HTML element during initial rendering by managing it in a React Component with a touch of jQuery assistance. Here is the code within my React Component ...

Creating a notification system similar to Facebook's in an HTML button using just HTML and CSS

I'm attempting to create a button with a notification feature that mimics the style of Facebook notifications. I want a small red circle in the top right corner, showing a numerical value. However, I'm running into some difficulties. I can' ...

Guide to generating a text string by utilizing the foreach loop

Is there a way to combine text strings from interfaces into a single file for display in UI? The current code is generating separate files for each interface. How can I achieve the expected result of having all interfaces in one file? Additionally, is it ...

What is the best way to make Bootstrap 3 move to a new line when there is not enough space?

I am facing an issue with a lengthy text inside a div with a class of "col-md-3". The text is so long that it appears to be overlapping with the content of another div. Adding spaces is not an option as it is an email address, and it displays fine on some ...

Adjust the ZIndex on a div through interactive clicking

For my new project, I'm exploring a concept inspired by Windows 7. The idea is that when you double click on an icon, a window will open. If you keep double clicking on the same icon, multiple windows should appear. The challenge I'm facing is im ...

Assistance needed with updating a related element using jQuery AJAX upon successfully completing a

Currently, I am in the process of creating a database-driven jokes website where users have the ability to vote on their favorite jokes. Each time a user votes for a joke, the "score" column in the MySQL database is incremented. Now, my main concern is upd ...

Executing a final PHP script termination with die(), along with Jquery AJAX

I am facing an issue with Internet Explorer. I'm currently using the jQuery ajax method to make a call to a PHP script. The PHP script only contains a die() function. In Firefox, the error message is displayed when this happens, but in IE, the success ...