Swap the text inside a div with new content by smoothly sliding it upwards with jQuery

Currently, I have 2 divs set up like this:

<div id="content">
    Content 1
</div>
<div id="rollover-content" style="display: none;">
    Content 2
</div>

I am trying to achieve a 'slide up' transition effect on the #rollover-content div when the mouse hovers over the #content div. And when the mouse leaves the #content div, I want the #rollover-content div to slide back down.

If you visit this demo and hover over the first tile, you will see the desired effect. However, I would like to implement it using pure jQuery.

In order to create a visually appealing card design, I have created the following CSS class:

.card {
        position: relative;
        margin: 0.5rem 0 1rem 0;
        background-color: #F1F1F1;
        transition: box-shadow .25s;
        border-radius: 2px;
        box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12);
        padding: 20px;
}

How can I apply these transitions within a div that has this wrapping card class?

Answer №1

No need for jQuery here - just a few lines of CSS can achieve the same effect. The transition effect is applied to the .box-2 class.

.box-container {
  background: blue;
  width: 200px;
  height: 400px;
  position: relative;
  overflow: hidden;
}
.box-1 {
  position: absolute;
  background: lightblue;
  width: 200px;
  height: 400px;
  top: 0;
  left: 0;
}
.box-2 {
  position: absolute;
  background: darkblue;
  color: white;
  width: 200px;
  height: 0;
  bottom: 0;
  left: 0;
  transition: height .4s ease-in-out;
}
.box-container:hover .box-2 {
  height: 400px;
}
<div class="box-container">

  <div class="box-1">

    Hello

  </div>

  <div class="box-2">

    New content

  </div>
</div>

</div>

Answer №2

If you want to achieve this effect, simply enclose your content within two divs. Firstly, include an outer div that matches the size of the content slides and has overflow: hidden to hide any content outside its boundaries. Then, add another div inside that which contains all the slides to be moved up or down when hovering. Lastly, place your actual content within that innermost div. Here's a basic example:

<div class="container">
    <div class="slide">
        <div class="content" id="first">
            Content 1
        </div>
        <div class="content" id="second">
            Content 2
        </div>
    </div>
</div>
.container {
    width: 200px;
    height: 150px;
    overflow: hidden;
    position: relative;
}
.slide {
    position: absolute;
}
.content {
    width: 200px;
    height: 150px;
}
$('.container').hover(function(e) {
    var $container = $(this), 
        top = e.type == 'mouseenter' ? -$container.height() : 0;
    $container.find('.slide').stop(true).animate({
        top: top
    })
})

See it in action here

Alternatively, you can achieve this solely with CSS as long as the .content elements will always be the specified height from your CSS:

.slide:hover {
    top: -150px;
}

Here's another working example

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

Navigating through multiple pages with React Native stack navigation

I'm currently in the process of developing a react native app and I'm facing some confusion with regards to page navigation. It appears that there is a glitch in the navigation flow, causing it to skip a page. <NavigationContainer> ...

Image not showing correctly after closing the bootstrap modal

I am currently working on creating a gallery with hover effects for each image, similar to the one found here. My goal is to have a Bootstrap modal open when I click on a link within the hovered image, displaying additional content. Everything seems to be ...

An issue arises in Django where a particular field fails to display the accurate value when presented in a template

models.py The Order model in models.py defines various fields such as user, customer, card_id, mobile, email, total_price, payment_method, status, take_method, points_earned, date, and address. It also includes preset choices for payment options, order sta ...

The input text in the Typeahead field does not reset even after calling this.setState

As I work on creating a watchlist with typeahead functionality to suggest options as the user types, I encountered an issue where the text box is not resetting after submission. I attempted the solution mentioned in this resource by calling this.setState( ...

Deleting a node from a jstree based on its ID upon clicking a button

I have a tree structure using jstree and I need to be able to delete a specific node by its ID when a button is clicked. Below is my tree structure represented in HTML list format: <div id="testtree"> <ul> <li id="1" title="ID: ...

The TypeScript compilation failed for the Next.js module

Working on a project using Next.js with typescript. The dev server was running smoothly, and I could see frontend changes instantly. However, after modifying the next.config.js file and restarting the server (later reverting the changes), compilation issue ...

Turn off hover effect for MUI DataGrid

I'm having trouble figuring out how to disable the hover effect on a row in the MUI Datagrid. I've searched through the API documentation but couldn't find a straightforward solution. Any suggestions on how to achieve this? <DataGrid ...

Tips for adjusting the maximum height of the column panel within the DataGrid element

I'm trying to adjust the max-height of a column panel that opens when clicking the "Columns" button in the Toolbar component used in the DataGrid. I am working with an older version of @material-ui/data-grid: "^4.0.0-alpha.8". https://i.stack.imgur.c ...

The final row of the jQuery Autocomplete list is concealed inside the div element

I have integrated a jQuery autocomplete feature on our website, which generally works well. However, sometimes the returned rows from the autocomplete are not fully visible and appear to be hidden within the containing div. Can anyone suggest the easiest ...

Extracting the toArray value from the sortable() function

It feels like I must be making a simple mistake here, as this seems like such a basic issue. The problem I'm facing involves using the sortable() function on a short list and wanting to retrieve an array of the sorted list's IDs. While my code a ...

Struggling with NodeJs POST request issue

Let's imagine I have a specific URL https://exampleAPI.com/authorize?param1=value1&param2=value2 Where param1 and param2 are the payload values being sent. How can I execute a POST request in this particular way? I attempted the following: var ...

Using nested asynchronous loops to push items to an asynchronous queue without triggering the main callback

I have an async queue that I am populating with items from nested lists within a data object. Despite the queue processing everything, I am unable to reach my main callback function console.log('All done.'). I've stripped away unnecessary co ...

Steps for creating a horizontal card design

Looking to achieve a card style similar to this: http://prntscr.com/p6svjf How can I create this design to ensure it remains responsive? <div class="recent-work"> <img src="work/mercedes.png"> <h3>Modern website concept</h3&g ...

Express callback delaying with setTimeout

I'm working on a route that involves setting a data attribute called "active" to true initially, but then switching it to false after an hour. I'm curious if it's considered bad practice or feasible to use a setTimeout function within the ex ...

Require guidance and resolution

<div class="container"> <div class="top-section"> <img id="image1" /> <img id="image2" /> </div> <div class="content"> ... </div> <div class="bottom-section"> ... </div> </div ...

Asynchronous Middleware in ExpressJS Routing

I'm having trouble implementing a middleware in Express. Whenever I make a request, I end up with infinite loading times. I've searched on various platforms but couldn't find any examples that utilize an async await middleware stored in a se ...

Tips for dynamically loading content in a jQueryUI dialog box

One of my favorite features in jQueryUI is the dialog boxes. But unfortunately, there doesn't appear to be a built-in method for dynamically loading content. Should I explore alternative approaches to accomplish this task? Will iframes only load conte ...

How can I include JavaScript files in Maya or 3DS Max?

My dilemma lies in attempting to access a js file on Maya (2013) and 3ds max (2016) without any available three.js importers for these particular softwares. How can I effectively import a js file on either Maya or 3ds max? Alternatively, are there other ...

Unable to save captured signature image during saveEvent function in React Native

I am a beginner in the world of React Native and I am facing an issue with saving a signature image. It seems like the function responsible for this task is not being called properly. I suspect there might be an issue with the onPress event handler, as whe ...

The D3 path is generating an unexpected output of 0px by 0px, causing the map not to display properly. I am currently unsure how to adjust the projection to

I am currently working on creating a world map using D3. I obtained the JSON file from the following link: https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json Below is the code snippet I'm using: // Defining SVG dimensio ...