Conceal an element using a transition effect, initiating its positioning at the center instead of the default

One of the challenges I'm facing is with a video element that I want to hide gradually when clicking on another element. The issue is that after the animation completes, the video ends up hidden at the top-left corner of the screen instead of at the center where I want it to be. Any suggestions on how I can achieve this?

Please take a look at my code below:

const div = document.querySelector('div');

        window.addEventListener('click', () => {

            div.style.width = '0'
            div.style.height = '0'

        })
div {
            width: 150px;
            height: 150px;
            background-color: blue;
            transition: ease-in-out 2s;
        }
<div></div>

Answer №1

To hide your video element, you can utilize the CSS property scale by scaling it to 0 and then triggering your JavaScript function. Simply assign the class "scale-to-0" to your video element and proceed with hiding it.

div.scale-to-0  {
    -ms-transform: scale(0, 0); 
    -webkit-transform: scale(0, 0); 
    transform: scale(0, 0);
}

Answer №2

Have you tested out using margin 0 auto?

const div = document.querySelector('div');

window.addEventListener('click', () => {

    div.style.width = '0'
    div.style.height = '0'

})
div {
    width: 150px;
    height: 150px;
    background-color: blue;
    transition: ease-in-out 2s;
    margin:0 auto;

}
<div></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

I can view the data retrieved in the console, but I am having difficulty displaying it on the screen

I have successfully retrieved data from the API, as indicated by the output in my console. However, I am facing issues in displaying this data on the webpage due to a potential error in my code structure. Despite this, I can confirm that the fetched data ...

Guide on merging the root route with child routes using react router v6

When a user visits "/", I want it to automatically redirect them to "/todo", but this functionality is not working as expected. let router = createBrowserRouter([ { path: "/", element: <Layout />, children: ...

The value property or method is not defined on the Vue.js 2 nested component instance

I created a simple Vue.js 2 example to test nested components. Here is the structure of the components and templates: Vue.component('form-component', { template: '#form', props: ['value'], metho ...

Initiate the command with given parameters

Currently, I am utilizing a combination of React, Redux, Rxjs, typesafe-actions, and TypeScript to invoke an action with parameters. Here is my current code: Actions: import { createAsyncAction } from 'typesafe-actions'; import {ICats} from &ap ...

Utilizing the React hook useReducer technique

Currently exploring the useReducer React API, and curious about the variations between the theoretical (documented) approach and the one I have developed. Initial state of the component using useReducer hook: const [fields, dispatch] = React.useReducer(f ...

Toggle checkbox feature in Bootstrap not functioning properly when placed within ng-view

When attempting to embed a bootstrap toggle checkbox within <ng-view></ng-view>, an issue arises where a regular HTML checkbox is displayed instead of the expected bootstrap toggle. Strangely, the same checkbox functions as a bootstrap toggle w ...

Having difficulty completing the text field and submitting it automatically

My goal is to automatically fill in the "Reason for Access" text box with the word "TEST" using Tampermonkey. I am new to using Tampermonkey and UserScript, so I appreciate your patience. Currently, I am facing an issue where the "Reason for Access" field ...

What is the best way to pass the index value of a v-for loop as an argument to a computed property function in Vue?

I'm looking to pass the index value from a v-for loop (inside a path tag) as a parameter to a function stateData(index) defined in a computed property in Vue. I attempted to achieve this using v-model="stateData[index]", but an error is being displaye ...

How do I dynamically adjust class names for menu items as I scroll using jQuery?

I have a website with different sections identified by unique IDs, such as: <section id="home"> First Section </section> <section id="about_us"> Second Section </section> <section id="what_we_do&q ...

Is it possible to create a fixed position shader background in three.js?

Trying to incorporate a pixel shader as a background in three.js without affecting the camera rotation with OrbitControls. Most implementations use a 2D plane, but I want it to remain fixed in the scene. Is there a way to achieve this with two separate can ...

Tips for addressing the Stale Element Reference Exception when locating an element in JMeter

Trying to locate an element with a structure similar to the following: <div> <div> <div> <ul> <object id = "obj" #document (link here) <html> <head> </head> ...

Unspecified data output from jquery datepicker

I am currently utilizing a datepicker to select a date. I have implemented the jQuery datepicker, however, it is returning an undefined value. Can someone please assist me with this issue? My HTML CODE: <input type="text" class="form-control fromdate" ...

Using HTTPS, you can access Flask from AJAX

I have encountered numerous inquiries concerning this issue, but none have proven effective for me. I recently switched my domain from HTTP to HTTPS. Everything was functioning properly on HTTP. The main issue lies in the fact that my javascript and flask ...

Having trouble locating the Nativescript-theme-core file for your Nativescript application?

I'm currently working on a basic barcode scanner app and encountering an unusual error once the app is deployed to a Genymotion emulator. It appears to be searching for the core theme in the incorrect location. Any thoughts on why this issue is occurr ...

similar element with alternate background shade

I have a custom component that I am using multiple times on a Vue page. I want to customize the background color for each instance of this component. Here is the code for my component: <template> <div class="project-card" :style="{backgroundCo ...

inSession variable in express: set to false

i keep receiving inSession:false https://i.sstatic.net/4IW0f.png when attempting to log in, it is expected to return true. I am utilizing express session, in combination with postges and sequalize. I have logged the state values and they are being ...

It is essential for every child within an array or iterator to possess its own distinctive "key" prop when working in reactjs

Despite consulting the ReactJS documentation and various tips on StackOverflow, I am still completely confused by an error in my console. The issue arises when trying to display a list of book titles ({item.volumeInfo.title}) while encountering an error. ...

Adding and adjusting the size of different DIV elements within a shared area

Looking to create a dynamic bar with the ability to add multiple child DIVs (similar to this: https://i.stack.imgur.com/tdWsq.jpg). Utilizing jQuery, jQuery UI, Bootstrap, and various plugins. The structure for the div (or span) system could be structured ...

Is using selectors a more effective way to retrieve computed data compared to using class methods?

When using react, redux, and reselect in a project, is it preferable to move all computable data from class methods to selectors and avoid mixing the use of both? Are there different concepts behind these approaches? class DocsListView { getOutdatedDocs ...

Override the default Bootstrap 5 styling for nav-links

I recently encountered a problem while trying to customize the appearance of the nav-link elements in my Bootstrap 5 navbar. I initially thought I could simply override the existing styles by targeting the class in my CSS. However, my attempts were unsucce ...