Moving from a static full screen div to a dynamic relative position with a smooth transition and animated effects

I am in need of a div element that initially occupies the entire viewport. After a specified delay, this div should transition to a relative position and adjust its dimensions to fit the width while maintaining a 16/9 ratio. The transition must be smooth and animated.

I attempted to achieve this by adding a class after a delay, but unfortunately, it did not result in the desired animation...

setTimeout(function() {
  var deckContainers = document.querySelectorAll('div.deck-container');
  deckContainers.forEach(function(deckContainer) {
    deckContainer.classList.toggle('collapsed');
  });
}, 800);
.deck-container {
  background-color: red;
  top:0;
  left:0;
  width:100vw;
  height:100vh;  
  display: inline-block;
  transition: all 0.5s ease-in-out;
  position: fixed;
}

.deck-container.collapsed {
  position: relative;
  width: 100%;
  padding-bottom: 56.25%;
  overflow: hidden;
}

https://jsfiddle.net/LaitEau/feo78wyg/ https://jsfiddle.net/LaitEau/n2swL6k3/1/

If you have any suggestions on how I can achieve this effect successfully, I would greatly appreciate it.

For reference, here is a gif demonstrating the prototype of the behavior I am aiming for:

Answer №1

If you want a similar effect, try adjusting the margin of the body and adding a transition.

Check out this example on JSFiddle

setTimeout(function() {
  var deckContainers = document.querySelectorAll('div.deck-container');
  deckContainers.forEach(function(deckContainer) {
    deckContainer.classList.toggle('collapsed');
  });
}, 1300);

setTimeout(() => {
  document.body.style.margin = '16px';
}, 1300);
body {
  margin: 0px;
  transition: margin 1s;
}

div.deck-container {
  background-color: red;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  display: inline-block;
  position: fixed;
}

div.deck-container.collapsed {
  position: relative;
  width: 100%;
  padding-bottom: 56.25%;
  overflow: hidden;
}

.div400px {
  height: 400px;
  width: 100%;
  background: grey;
}

.div100px {
  height: 100px;
  width: 100%;
  background: blue;
}
<div class="div100px"></div>
<div class="deck-container"></div>
<div class="div400px"></div>

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

Positioning a div at the bottom of a webpage without using absolute positioning in HTML

Check out the fiddle. I've included some CSS to position the tab on the right side. My goal was to have those tabs in the lower right corner of the container. If I use absolute positioning for the div, it messes up the tab content width. I tried ad ...

When hovering over elements, my li tags undergo a transformation

I am working on a simple menu code, which is just a list of items. ul li { position: relative; top: 55px; height: 30px; background: #212; display: inline-block; width: 29%; margin: 1%; margin-bottom: 0; padding: 5px; -webkit-border-t ...

How to temporarily change CSS color for 5 seconds using JavaScript and incorporate an easing effect?

Regarding this query: JavaScript - Modifying CSS color for 5 seconds Here is a live example of the solution: http://jsfiddle.net/maniator/dG2ks/ I am interested in adding an easing effect to the transition, gradually making the color fully opaque and t ...

Click anywhere on the body to conceal this element

I am currently working on a website where I have implemented a hidden menu that is functioning correctly. However, I want to make it so that when the menu is visible, users can click anywhere on the body to hide it. Unfortunately, my current code is not w ...

fixed footer with fixed height on parent container

I have successfully implemented a sticky footer on my web pages by following specific instructions. http://css-tricks.com/snippets/css/sticky-footer/ The recommended approach includes using min-height:100% and not specifying the height property. .page-w ...

A step-by-step guide to invoking a function upon submitting a form with an external JavaScript file

How can I execute a function when the user submits a form using an external JavaScript file? index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>example</title> ...

Applying scoped CSS styles to parent elements in HTML5

I've been delving into the details of the HTML5 specification, specifically regarding the scoped attribute on style elements. According to the specification: The scoped attribute is a boolean attribute. When used, it signifies that the styles are mean ...

PrimeFaces: Achieving Conditional Coloring Based on Status (Passed/Failed)

Attempting to dynamically change the color of a column based on its status value (Passed/Failed/InProgress) using jQuery. I tried copying and pasting the table into jsfiddle, where it worked. However, when implemented in the actual XHTML file, the jQuery ...

Display every div element if none of the links have been clicked

On my webpage at url.com/yourfirstpage/, all div elements are hidden by default with a display:none property. If we specifically target #sec1 by going to url.com/yourfirstpage/#sec1, only sec1 is displayed while the others remain hidden. But what if we acc ...

The input field within the mat-form has been styled to match the background of the page using CSS

I am currently facing an issue with the code below: Html: <form fxLayout="column" fxLayoutAlign="center center" [formGroup]="deleteForm" (ngSubmit)="onSubmitForm()"> <mat-form-field color="accent" appearance="outline"> <input ...

Implementing dynamic CSS in AngularJS using conditional statements

One technique I've been using in angularjs involves toggling between two windows using the ng-click and ng-show directives: <div ng-init="showTab2 = false"> <a ng-click="showTab2 = true; showTab1 = false ">#Tab1 </a> ...

Why is having <html lang="en"> essential?

Is the tag essential for website development? Will my code be impacted if I choose not to include it? ...

What could be the reason for my Bootstrap collapse navigation bar not functioning properly?

<!DOCTYPE html> <html lang="en"> <head> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> <!-- Optional theme --> ...

Creating a checklist using HTML and CSS can be achieved by utilizing the <span> element and

I'm having trouble changing the color of a span box to red when I focus on a link. The span successfully changes color when activated, but nothing happens when I try to use the 'focus' pseudo-class. On click, different categories will displa ...

Using Vue.js to pass an image URL as a prop for CSS animation by converting it to a CSS variable

Need help with a component that features a hover animation displaying 4 rotating images: animation: changeImg-1 2.5s linear infinite; @keyframes changeImg-1 { 0%, 100% { background-image: url('images/wel1.png'); } 25% { background-image: ur ...

The header one tag does not wrap around content when the window size is adjusted; instead, it

I have been struggling to position my page title in the center of the header image both horizontally and vertically on my portfolio pages. I managed to get the h1 title where I want it but now I need to make it larger. The issue arises when I resize the wi ...

Issues with Bootstrap's "img-fluid" class functionality

I recently started the web development course by Colt Steele and ran into an issue while working on the "Museum of Candy project" (I made some modifications to it). The problem is that I am using a widescreen monitor, and when I expand my window, the image ...

Type content in HTML5

Can you help me with a simple question? I am currently working on building my portfolio using html https://i.stack.imgur.com/rxYRS.png I want to be able to click on an image and add the description of my choice. Right now, it is showing something else: ...

Is there a way to set the background image to scroll vertically in a looping pattern?

Is it possible to achieve this using only HTML5 and CSS3, or is JavaScript/jQuery necessary? ...

What is the best way to showcase a photo selected using an HTML input within a div container?

My goal is to select a photo from a folder and display it on a webpage, but I have struggled to find a working solution. Can this be achieved using just basic HTML, CSS, and JS? I am new to web development, so please forgive me for any beginner questions. ...