Effortlessly Transition to Full Screen with Div Expansion on Click

I'm currently working on creating a smooth transition for a div to expand fullscreen when clicked. My goal is to achieve a similar effect as the case studies on this website:

Although my code can make the div go fullscreen, there's an issue with the abrupt jump caused by adding position fixed. I am open to using either CSS or JavaScript/jQuery to handle the animation.

$(function() {
  $(".block").on("click", function() {
    $(this).addClass("fullscreen");
  });
});
html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}
.block {
  width: 50%;
  margin: 0 auto 50px auto;
  height: 300px;
  background-color: red;
  transition: all 0.5s linear;
}
.block.fullscreen {
  position: fixed;
  top: 0;
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="block"></div>
<div class="block"></div>

You can view my progress so far on this pen: http://codepen.io/anon/pen/RKGeYj

Answer №1

Discover an amazing animation tutorial at . This resource will provide you with the animation effect you desire.

Remember, when setting the position as fixed, ensure to define the initial top & left properties as well. You can easily obtain these values using the getBoundingClientRect method. In addition to animating top & left, consider animating width & height for a smoother visual transition.

.in-animation {
    animation: inlightbox 0.8s forwards;
    position: fixed !important;
}

@keyframes inlightbox 
{ 
    50% { 
        width: 100%;
        left: 0;
        height: 200px;
    }
    100% {
        height: 100%;
        width: 100%;
        top: 0;
        left: 0;
    }
}

Answer №2

Start by making your #block go fullscreen first, and then apply the position:absolute; after waiting for a delay longer than the animation speed for going fullscreen.

Check out this functional code snippet below.

var isFullscreen = false;
$("#block").click(function (){ 
    var prop = {};
    var speed = 910;
    if(!isFullscreen){ // MAXIMIZATION
       prop.width = "100%";
       prop.height = "100vh";
       isFullscreen = true;
      $("#block").animate(prop,speed); 
      setTimeout(function() { 
        $("#block").css("position","absolute");
      }, 920);
    }
    else{         
      prop.width = "50%";
      prop.height = "250px";            
      isFullscreen = false;
      $("#block").animate(prop,speed); 
      setTimeout(function() { 
        $("#block").css("position","relative"); 
      }, 920);
    }
    
});
html,body{
  width:100%;
  height:100%;
  margin:0;
  padding:0;
}
#block,#blockTwo{
  width:50%;
  height:250px;
  margin:0 auto;
  background-color: red;
}
#block{
  z-index:100;
}
#blockTwo{
  background-color:green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="block"></div>
<div id="blockTwo"></div>

Answer №3

Implementing a fresh strategy, I opted to utilize added and removed classlists through a Javascript onclick function. In order to ensure that the image occupied the entire page size without extending downward if there was content above it, I enclosed the images in a div element and applied classlists to manage these areas dynamically based on the picture's expansion. To replicate this functionality, make sure to enlarge your image. If this aligns with your website design, consider incorporating the following code snippet:

<html>
    <head>
        <style>
            .image {
                margin: 0px;
                transition: all 0.5s linear;
                background-image: url('https://tse2.mm.bing.net/th?id=OIP.4_3Eev4xNVvGA5aRvaevLAHaJa&pid=Api&P=0&w=300&h=300');
                background-repeat: no-repeat;
            }
            .image.small {
                width: 200px;
                height: 100px;  
                background-size: cover;
                background-size: 100% 100%;
            }
            .image.fullScreen {
                width: 100%;
                height: 100%;
                background-size: cover;
                background-size: 100% 100%;
            }
            .topContent {
                display: contents;
            }
            .bottomContent {
                display: contents;
            }
            .topContent.remove {
                display: none;
            }
            .bottomContent.remove {
                display: none;
            }
        </style>
    </head>
    <body>
        <div class="topContent">
            <h1>Hello</h1>
        </div>
        <div class="image" onclick="imageChange()"></div>
        <div class="bottomContent">
            <h1>Hello</h1>
        </div>
        <script>
            window.onload = function() {
                document.querySelector('.image').classList.add('small')
            }
            function imageChange() {
                if (document.querySelector('.image').classList.contains('small')) {
                    document.querySelector('.image').classList.remove('small')
                    document.querySelector('.image').classList.add('fullScreen')
                    document.querySelector('.topContent').classList.add('remove')
                    document.querySelector('.bottomContent').classList.add('remove')
                } else {
                    document.querySelector('.topContent').classList.remove('remove')
                    document.querySelector('.bottomContent').classList.remove('remove')
                    document.querySelector('.image').classList.remove('fullScreen')
                    document.querySelector('.image').classList.add('small')
                }
            }
        </script>
    </body>
</html>

If you desire the image to extend right to the very edge, that can be easily achieved. Additionally, by leveraging classlists, you could even create a black border around the image.

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

Guide on incorporating Bootstrap JS into HTML5 reusable web elements

RESOLVED: the solution is in a comment TL;DR: Issues triggering Bootstrap's JS, likely due to incorrect import of JS scripts I've been working on integrating Bootstrap with my custom reusable web components across all pages. Specifically, I&apo ...

What is the best way to arrange the elements vertically within a flex container?

In my current code snippet, I have the following setup: <section class="body1"> <h1 class="text1">Tours around the world</h1> <h3 class="text1">Great experiences</h3> <form action="/respuestaUsuario/"> ...

Ensure two CSS components occupy the entirety of their container, positioned next to each other, with margin spaces between

I am looking to make two elements take up a precise percentage of the parent's width, while also including margins between them. Here is my current setup: <div class='wrap'> <div class='element'>HELLO</div>< ...

Error TS2322: The function expecting a type of 'FormEventHandler<HTMLFormElement>' cannot be assigned the type '(data: TicketFullDTO) => Promise<void>'

I am currently working on creating an edit form to modify data from a database based on its ID. Here is my approach: import React, {FormEvent, useEffect, useState} from "react"; import TextField from "@material-ui/core/TextField" ...

Error: Cannot access 'muiName' property as it is undefined

i am trying to display the elements of an array within a custom card component in a grid layout. However, when the page loads it freezes and the console shows "Uncaught TypeError: Cannot read property 'muiName' of undefined" custom car ...

Create a speech bubble using only CSS, featuring a partially see-through background and a stylish border

I wish to design a speech bubble using only CSS. There are specific requirements I need to meet: It should adjust its size based on content with some padding or a set size. The background color must be semi-transparent. You must be able to define a borde ...

Navigating with Anchors, Styling and jQuery

Firstly: Apologies in advance for any language errors as English is not my native tongue. :) The Scenario Here's the deal: I'm attempting to create a single button that, when clicked by the user, automatically scrolls down to the next DIV. Each ...

The concept of circular dependencies in ES6 JavaScript regarding require statements

After transferring a significant amount of data onto the UI and representing them as classes, I encountered some challenges with managing references between these classes. To prevent confusing pointers, I decided to limit references to the data classes to ...

What is the best way to rotate a cube when it is clicked on?

My current project involves rotating a cube by clicking on buttons either on the cube itself or floating next to it. At the moment, I have them floating for easier testing purposes, but placing them directly on the cube is not an issue. The main issue I&a ...

Issue Installing Npm Package (detected 23 security vulnerabilities)

My attempt to install the package resulted in an error message. How can I resolve this issue? ...

filling out a document by utilizing a separate form embedded within a modal using the power of bootstrap

I'm in need of some assistance with a small task. In my profile.ejs page, I have a form that is populated by data from a mongo database. Instead of directly editing the fields in the main form and submitting all the data to update the database, I&apo ...

The POST variable consistently remains void

The approach I am using to handle the data sent with jquery.ajax involves sending an empty string by default. Whenever there is a change, I monitor the input for changes and resend the data. Everything seems to work fine in the console, but in PHP, $this-& ...

Transforming JavaScript information into PHP with ajax for WordPress plugin creation

I'm currently navigating the realm of creating my inaugural WordPress plugin and encountering a hurdle in transposing a Javascript array of objects into PHP. My aim is to utilize this data to craft individual tables within the wp-admin user interface. ...

Guide to incorporating WebElement scrolling in Selenium using Java?

I need to implement a scroll function for a table on my webpage rather than scrolling the entire page, so using window.scrollBy is not an option. After attempting to find the container responsible for the scroll functionality in the DOM (with no luck), I ...

Run JavaScript code whenever the table is modified

I have a dynamic table that loads data asynchronously, and I am looking for a way to trigger a function every time the content of the table changes - whether it's new data being added or modifications to existing data. Is there a method to achieve th ...

Uploading Files with PhoneGap using Ajax

I have been attempting to execute this PhoneGap example for uploading images from a device to a server. // Wait for PhoneGap to load // document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap is ready // functi ...

The JQuery datepicker is having trouble functioning when used with a masterpage in ASP.NET

Welcome to my Masterpage with the best source code <%@ Page Title="" Language="C#" MasterPageFile="~/Usermaster.Master" AutoEventWireup="true" CodeBehind="ApproveLoanpage.aspx.cs" Inherits="WebLoanCalculator.ApproveLoanpage" %> <%@ Register Asse ...

Stylish Zigzag Border with a Patterned Background

Recently, I've been experimenting with creating a header that features a unique zigzag border. Traditionally, this effect is achieved using images to achieve the desired look. (1) I am curious if there is a way to implement a cross-browser compatible ...

How do I navigate to a different page in Vue.js HTML based on user selection?

Here is my first attempt at writing HTML code: <div class="col-md-4" > <div class="form-group label-floating"> <label class="control-label">Select No</label> <select class="form-control" v-model="or" required=""> ...

I am experiencing issues with the ng-dropdown-multiselect library and it is not functioning

Check out this awesome library for creating dropdown menus using angularjs and twitter-bootstrap-3 at: . I am trying to implement the examples provided. In my html, I have: <div ng-dropdown-multiselect="" options="stringData" selected-model="stringMod ...