Animate elements using fixed position without unexpected jumps

Trying to create an animation that expands a box to full screen. However, when I set the position to fixed before the animation begins, the box quickly moves up to the upper left corner.

Is there a way to make it expand from its original location instead?

setTimeout(function() {
  $('.test').css({
    position: 'fixed',
    top: 0,
    left: 0,
    margin: 0
  });
    
  $('.test').animate({
    height: $(window).height(),
    width: $(window).width()
  }, 200);
}, 2000);
.test {
    margin: 0 50px;
    background: blue;
    height: 200px;
    width: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
<div class="test"></div>

Answer №1

To achieve the desired effect, a combination of CSS and JS can be used. Here is a suggested implementation: VIEW DEMO

JS:

$('.element').css({
    'top': $('.element').position().top,
});

setTimeout(function () {
    $('.element').addClass("full_screen");
}, 2000);


setTimeout(function () {
    $('.element').removeClass("full_screen");
}, 8000);

CSS:

html, body {
    width: 100%;
    height: 100%;
    position:relative;
}
.element {
    position: absolute;
    margin: 0 50px;
    background: #ccc;
    height: 200px;
    width: 200px;
    transition: all 4s;
}
.full_screen {
    top: 0px !important;
    height: 100%;
    width: 100%;
    margin: 0;
}

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

The horizontal scrolling with overflow-x is not functioning correctly as it is displaying the scrollbar for the vertical

I am perplexed as to why I am unable to scroll horizontally to view the other pink thumbs. Despite adding an overflow of scroll-x to the thumbs container, which should theoretically allow horizontal scrolling, it only seems to scroll vertically. Could som ...

Implementing ng-show based on a specific position on a webpage

I am a beginner in AngularJS and I'm struggling to understand how to customize ng-show/ng-hide. I want the navigation bar below to only appear once the user has scrolled 600px down the page, but since I can't use jQuery with Angular, I'm uns ...

What is the method for retrieving the font size of elements in the native view using appium?

Can the font size of text in the native view be retrieved using appium? I am aware of using driver.findElement(By.id("by-id")).getCssValue("font-size"); for web views. Is there a similar method for native views? ...

Modify the css with JQUERY when there are no rows inside the tbody section

Is it possible to change the css using jquery if there are no rows in the table body? I have attempted this but my current approach is not working. I tried adding an alert within the if statement, but the alert did not appear. My goal is to hide the table ...

Ways to update row background color based on specific column values

I need to customize the background color of my table rows based on the value in the "Category" column. For example: Name Category Subcategory A Paid B C Received D If the Category value is 'Paid', I want the ro ...

Show the checked items in the table based on the ID value stored in the array

When I click the button labeled Show checked if id values are 1 and 5, I encounter an issue in displaying checked items in the table based on their corresponding id value in the array. Essentially, I want to show the checked items in the table only if thei ...

An unknown browserLink expression was encountered

I encountered an issue with " browserLink " that you can see below <div ng-show="optList.editing && optList.BANKRUPT_FLG != 'Y' && (optList.OPT != 'TF' || (optList.OPT == 'TF' && optLi ...

Difficulty aligning two images in one div using HTML and CSS

Having trouble with HTML and CSS. I have a div containing 2 images - one for background and the other for an avatar image that I can't seem to center. I've tried using margin:0 auto; and display:block; but it's not working. Current HTML pag ...

Code written in Javascript will not function properly when placed within a while loop

I'm attempting to use AJAX to submit a form that is embedded in a table. I have set up the table with rows populated by a while loop, each row containing a button to trigger the submission upon click. However, when I click the button, nothing seems to ...

Guide to generating a dynamic array using the value of a checkbox data attribute

Whenever a checkbox is clicked, I want to create a dynamic array using its data-attribute value and then add all checked checkboxes with the same data-attribute value to this array. For example, if I click on "height," I should create an array with its da ...

Transform the styles from a React component into Cascading Style Sheets

I have been tasked with transitioning all the styles from the JavaScript file to the CSS file while ensuring the design remains intact. I am facing an issue where using the CSS styles breaks the search field design. The original design can be seen here: S ...

Python script unable to locate div popup using Selenium

Recently, I've been working on enhancing my selenium skills, but I'm facing a challenge in finding a solution to this particular issue. While experimenting with the functionality of the page located at: www.omegle.com When using selenium to cli ...

Tips for arranging cards in a stacked position: To create a visually

I'm currently developing a project using react typescript, and I need to showcase various projects in the form of cards. However, I would like these cards to be displayed in a stacked layout like this Here is the component for displaying the projects ...

Session values do not persist in Window Popups

While the system functions properly in the main window, there is an occasional issue when generating a bill through AJAX. After generating the bill, a window popup automatically opens to display the bill details. However, during this time, the login sess ...

Fluctuating problem arises when placing one div over another div while hovering

Sorry if the title isn't clear. Basically, I have a div that when hovered over, triggers another div to appear with display:block, showing it above the original div. It works well but there is some flickering when moving the cursor over the second div ...

Perhaps dividing numbers with regular expressions

My HTML textarea allows users to input numeric serial numbers in various formats - either inline, separated by any character, or in columns from an Excel file. The serial codes are either 14 characters long if the first character is "1," or always 15 char ...

iOS emphasizes special characters by printing them in bold

Some parts of the website I am managing are showing German umlauts in bold (as seen in the screenshot). The font style being used is font-family: Source Sans Pro, Arial, sans-serif; with font-weight: 300. This font is sourced from Google Fonts. When I cha ...

I am experiencing challenges with utilizing moment and angular filters

I was able to get this code working perfectly before the recent jsfiddle update. However, now it seems to be causing issues. Any assistance would be greatly appreciated. Let's start with the HTML code: <div ng-app="app" ng-controller="ctrl"> ...

The initial attempt to fetch a value using jQuery returned as undefined

When using jQuery to retrieve each message ID, I am facing an issue where the first instance of the message that the user has shows up as undefined. However, for all subsequent messages, it works perfectly fine. var mssg_id = $(this).find('input[name= ...

Issue with font-size changes using css variables in Angular not updating in browser for specific fields

Utilizing CSS variables, I have implemented a feature that allows users to adjust the font size to small, medium, or large. While this functionality works correctly for most fields, there are certain instances where the value is applied but not displayed. ...