Animate an element to shift towards the lower left corner

My goal is to smoothly move a <p> element to the bottom left of the page using CSS animation. However, I'm encountering issues with the animation as it starts at the original place and abruptly ends at the bottom. I need the animation to be seamless throughout the entire movement.

Could someone provide me with the correct code to achieve this?

Here is the code I am currently using:

$("#expandable").on('click', function(event) {
  $("#expandable p").addClass("animated rotateOutUpLeft");
});
.animated {
  animation-duration: 2s;
  animation-fill-mode: both;
}

@keyframes rotateOutUpLeft {
  from {
    transform-origin: left bottom;
    opacity: 1;
  }
  to {
    transform-origin: left bottom;
    transform: rotate3d(0, 0, 1, -90deg);
    opacity: 1;
    margin: 5px;
    position: fixed;
    left: 20px;
    bottom: 100px;
  }
}

.rotateOutUpLeft {
  animation-name: rotateOutUpLeft;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="container">
    <h1>Header</h1>

    <div id="expandable">
      <div style="border: grey solid 2px;">
        <h2>clickable div</h2><br>
        <p><strong>text to move</strong></p>
      </div>
    </div>

</body>

</html>

Answer №1

You can achieve the desired effect without relying on CSS @keyframes:

$("#expandable").on('click', function(event) {
  $("#expandable p").addClass("animate");
});
.animate {
    transform-origin: left bottom;
    transform: rotate3d(0, 0, 1, -90deg) translateX(-50px);
    margin: 5px;
    transition: all 2s ease;
}
<!DOCTYPE html>
<html lang="en>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="container">
    <h1>Header</h1>

    <div id="expandable">
      <div style="border: grey solid 2px;">
        <h2>clickable div</h2><br>
        <p><strong>text to move</strong></p>
      </div>
    </div>

</body>

</html>

Answer №2

The issue arises when the element's position is changed from static (the default) to fixed, which is not something that can be animated.

A potential solution is to initially set the position to fixed and only animate the bottom property during the animation.

$("#expandable").on('click', function(event) {
  $("#expandable p").addClass("animated rotateOutUpLeft");
});
body {
  position: relative;
  min-height: 100vh;
}

.animated {
  animation-duration: 2s;
  animation-fill-mode: both;
}

#expandable p {
  left: 40px;
  bottom: calc(100% - 160px);
  position: absolute;
}

@keyframes rotateOutUpLeft {
  from {
    transform-origin: left bottom;
    opacity: 1;
  }
  to {
    transform-origin: left bottom;
    transform: rotate3d(0, 0, 1, -90deg);
    opacity: 1;
    margin: 5px;
    left: 20px;
    bottom: 10px;
  }
}

.rotateOutUpLeft {
  animation-name: rotateOutUpLeft;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="container">
    <h1>Header</h1>

    <div id="expandable">
      <div style="border: grey solid 2px;">
        <h2>clickable div</h2><br>
        <p><strong>text to move</strong></p>
      </div>
    </div>

</body>

</html>

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

Personalize the code for tab navigation by incorporating href links

Original HTML Code The following HTML code is designed for a table, but I require it to be used for anchor elements. <table> <tr> <td>click me</td> </tr> <tr> <td>click me</td> ...

Is there a method to play videos automatically without turning off the sound? How exactly does YouTube manage to achieve this feature?

Is there a way to automatically play a video with sound, without the muted attribute? I noticed that when clicking on a YouTube video link, it plays automatically. Looking forward to hearing your thoughts on this! If I can open a YouTube link and have the ...

How can I create square corners within other square corners?

https://i.sstatic.net/OsDsg.png Hey there everyone, I'm curious if it's achievable to create borders like these using only CSS, without relying on a set image. If anyone has insight on how this can be accomplished, please share your knowledge. ...

Position a dynamic <div> in the center of the screen

My goal is to design a gallery page with a list of thumbnails, where clicking on a thumbnail will open the related image in a popup div showing its full size. The issue I'm facing is how to center the popup div on the screen, especially when each pic ...

What is the best time to fetch the height of an element containing an image?

I am working on my web app and I want to implement a popup element that contains an <img> element. Typically, the image source is larger than necessary, so I resize it using CSS. However, before displaying the popup, I need to determine its outerHeig ...

Error: Unexpected syntax error occurred due to the use of '===' operator

Click here for image descriptionError: There is a syntax error in line 7 of the file add.php Code: <?php require_once('db.php'); if ( !empty($_POST['name']) &&!empty($_POST['alias']) &&!empty($_POST[&apo ...

Aligning an image within the layout of a Ghost theme

I'm currently using Ghost's free Starter theme and I'm attempting to center an image that is wider than the paragraph and main element (60rem). These are the key CSS elements that control the positioning of the image. I can achieve full wid ...

The OK Button Dialogbox is currently malfunctioning

While using jQuery for a dialog box, I encountered an issue when trying to delete an element within it. After clicking the ok button, the dialog box did not redirect or close itself properly. The initial content in the dialog box seems to work fine. < ...

Creating a custom dynamic favicon and title in NextJS

Hello there! I am in the process of creating a web constructor. Currently, my application functions as follows: I verify the URL that the user is visiting (such as localhost:3000) I identify their project name within my web constructor (localhost:3000 -&g ...

Is there a way to place the icon on the right side of the @mui/Chip component?

Currently, I am working with MUI version 5.15.0. I have a component called Chip, and my goal is to display the icon after the label on the right side. I attempted to use the CSS rule - .MuiChip-icon{ order:1 }, but it resulted in too much spacing. Additio ...

Achieve the effect of bringing the div and its contents to the top when hovered over, all while keeping

I have designed a popup that includes multiple boxes. I want the width of the box to expand and be visible over all content on the webpage when a user hovers over any ".deviceboxes". I tried using ".deviceboxes:hover" which worked fine for the first box in ...

Could there be a coding issue stopping <div class="rating"> from aligning more centrally along the horizontal axis on the screen?

Just wondering, could there be something in my code that's preventing <div class="rating"> from moving closer to the center? And is there a way to make it move closer horizontally? (On a side note, CSS layout and positioning still puzz ...

The image does not properly scale within the div as it rotates

I am currently attempting to use jQuery to rotate an image, but I am facing an issue. When I rotate the image left or right, a portion of it extends beyond the frame. The image is not resizing based on the CSS properties set as max-height: 100%; max-width: ...

Tips for using the window.print() function to display and print another php or html page

<script type="text/javascript> window.print('page.php'); //Can you help me understand how to use window.print() function to print another php or html page? ...

Create a recursive CSS style for an angular template and its container

I am struggling with styling CSS inside an ng-container that is used in a tree recursive call to display a tree hierarchy. <ul> <ng-template #recursiveList let-list> <li *ngFor="let item of list" [selected]="isSelected"> <sp ...

Display 1 row of content on larger LG screens and 2 rows on medium-sized MD screens using Bootstrap

How can I achieve 1 row with 12 cells on a large screen in Bootstrap, and for a smaller screen have 2 rows with 6 cells? Here is a visual representation of what I am trying to accomplish: https://i.stack.imgur.com/NgfaP.png This is the code I attempted: ...

Slideshow plugin glitch on Safari and Opera caused by jQuery implementation

My current project involves utilizing a fantastic plugin called Sequence.js for creating animations and transitions with CSS3 based on jQuery. I have set up a simple responsive fadeIn-fadeOut slideshow using this plugin. While everything works smoothly an ...

Aligning a navigation bar with a hamburger menu in the center

I recently implemented a hamburger menu with some cool animations into my site for mobile devices. Now, I am facing the challenge of centering the menu on desktop screens and it's proving to be tricky. The positioning is off, and traditional methods l ...

An error occurred while validating with JQuery AJAX: Undefined object does not contain any properties

Apologies for the premature post... Encountering an error after clicking the SUBMIT button: Error Log TypeError: undefined has no properties [More information] jquery.validate.min.js:4:22946 a.ajax https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/ ...

Steps for using selenium to select a radio button

Looking to leverage Selenium in Python 3.9 for button clicking automation. Link to the website https://i.stack.imgur.com/KWsKG.png The goal is to click on "Effective Date", then select "July 2021", and finally click "OK". Initially, the first two steps ...