Utilizing transitions to control the visibility of div elements

This is what I have achieved so far:

function getOffsetRect(elem) {
    var box = elem.getBoundingClientRect()
    var body = document.body
    var docElem = document.documentElement
    var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
    var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft
    var clientTop = docElem.clientTop || body.clientTop || 0
    var clientLeft = docElem.clientLeft || body.clientLeft || 0
    var top = box.top + scrollTop - clientTop
    var left = box.left + scrollLeft - clientLeft
    return { top: Math.round(top), left: Math.round(left) }
}
function getOffsetSum(elem) {
    var box = elem.getBoundingClientRect()
    var body = document.body
    var docElem = document.documentElement
    var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
    var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft
    var clientTop = docElem.clientTop || body.clientTop || 0
    var clientLeft = docElem.clientLeft || body.clientLeft || 0
    var top = box.top + scrollTop - clientTop
    var left = box.left + scrollLeft - clientLeft
    return { top: Math.round(top), left: Math.round(left) }
}
function getOffset(elem) {
    if (elem.getBoundingClientRect) {
    var regdiv = document.getElementById("register");
    var button = document.getElementById("regbtn");
    var a = getOffsetRect(elem);
    var top = parseInt(a.top);
    var bheight = button.style.height;
    var top_ = top + bheight;
    regdiv.style.top = top_ + "px";
    regdiv.style.left = a.left + "px";
    return a;
    } else { // old browser
        var a = getOffsetSum(elem);
        //window.alert("1\nTop:" + a.top + "\nLeft: " + a.left);
        var regdiv = document.getElementById("register");
        regdiv.style.top = a.top + "px";
        regdiv.style.left = a.left + "px";
        return a;
    }
}
function CloseRegister(elem) {
    var regdiv = document.getElementById("register");
    var button = document.getElementById("regbtn");
    if (register.style.display == 'none') {
        getOffset(button);
        register.classList.remove('hidden');
        register.classList.add('visible');
        register.style.display = "block";
        register.onmouseup = function () {
            register.onmouseup = null;
            register.classList.remove('visible');
            register.classList.add('hidden');
            setTimeout(function () { register.style.display = 'none'; }, 2000);
        }
    }
}
.register
{
    background-color:Purple;
    height:0;
    width:0;
    transition: 2s;
    -o-transition: 2s;
    -webkit-transition: 2s;
    -moz-transition: 2s;
}
.register.visible
{
    width:250px;
    height:250px;
    opacity:1;
}
.register.hidden
{
    font-size:0;
    width:0%;
    height:0%;
    opacity:0;
}
<center><div dir="rtl">
    <button style='margin-right:3px' type='button' id='regbtn' onclick='CloseRegister(this)'>Register</button>
    <div onmousedown="CloseRegister(this)" id="register" class="register" style="position:absolute; z-index:2; display:none;" dir="ltr">Register??</div>
</div></center>

Currently, it functions partially as intended. When the registration div is open and clicked, it performs well with a transition. However, when the div is closed and the registration button is clicked to open it again, it instantly opens without a transition (It should smoothly transition from 0 width and height to the specified values in .visible). I have tried numerous approaches but have been unable to resolve this issue..

Answer №1

Simply incorporate a setTimeout function when displaying your DIV element.

Instead of using register.classList.add('visible'); try the following:

setTimeout(function(){register.classList.add('visible');}, 10);

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

Adjust the height of the drawer in material-ui

In my current project using react and material-ui, I am facing a simple issue that has me stumped. I am trying to create a drawer with a specific height so that it does not overlap the app bar when opened. Unfortunately, there is no built-in parameter in ...

Support for Vue 3.4 same-name shorthand has been added to VS Code

After upgrading my Vue 3 project to version 3.4, I encountered an issue with vs-code highlighting same-name shorthand as an error, even though it was functioning correctly in my code. I am using the volar extension. Is there a way to resolve this so that v ...

I am facing a challenge with my data, as it includes start and end values representing character indexes for styles. I need to find a way to convert this information into valid HTML tags

I have some content from another source that I must transform into proper HTML. The content contains a string such as: "Hello, how are you?" Additionally, there is a separate section detailing styling instructions. For example: 'bold:star ...

What is the best way to access an excel file using JavaScript and Protractor?

Is it possible to read an Excel file dynamically in JavaScript and iterate through cell values using row and column counts, without converting it into a JSON object? I have searched through several posts on Stack Overflow but have not found a solution yet. ...

Gatsby's build process encounters a stumbling block when webpack fails due to issues

Every time I try to run gatsby build, an error pops up: failed Building static HTML for pages - 10.179s ERROR #95313 Building static HTML failed Refer to our documentation page for more details on this issue: https://gatsby.dev/debug-html 343 | ...

suggestions for customizing Angular Material

The guidelines regarding materials specify that: "For any Angular Material component, you are allowed to define custom CSS for the component's host element that impacts its positioning or layout, such as margin, position, top, left, transform, and z- ...

Clear out the classes of the other children within the identical parent division

I'm currently in the process of replacing my radio circles with div elements. I've successfully implemented the functionality for selecting options by clicking on the divs, as well as highlighting the selected div. However, I'm facing trou ...

Retrieving custom attribute values during a drop event in Jquery drag and drop operations

I'm currently working on a moodboard creator, but I've hit a snag while trying to transfer the values from custom attributes of an image to a new div once the drop action is completed. Every time the drop is finished, it always fetches the value ...

Arranging arrangements in javascript

I am dealing with objects that contain the fields id and position. const items = [{id: 11, position: 1}, {id: 12, position: 2}, {id: 13, position: 3}, {id: 14, position: 4}, {id: 15, position: 5}, {id: 16, position: 6}]; These objects represent folders st ...

Is it feasible to relocate an embedded swf file from one tag to another?

My HTML contains two containers for embedded swf files: <div id="player1"></div> <div id="player2"></div> If I load an embedded video into the first div (player1) like this: swfobject.embedSWF("someURL","player1"); Is it possibl ...

Steps for designing image animations with fade in and fade out effects

I have a task to enhance the current code provided below, which currently works with only two images. HTML: <div id="cf"> <img class="bottom" src="<?php bloginfo('template_directory') ?>/assets/img/image1" /> <img class ...

What is the reason behind one function triggering a re-render of a component while the other does not in Next.js?

I am currently working on a Next.js web application where one of the pages contains two functions that utilize useState() to add or remove emails from an array. const [invites, setInvites] = useState([]) // other code const lmao = () => { console.lo ...

Which element can be slotted?

Is it possible to use a tr (table row) for a named slot? What is the outcome when a tr is used as a slot's fallback content? For example: <x-table> <tr slot="x-row"> <td>data</td> <td>data</td> ...

Transition effects on table images using CSS

How can I incorporate CSS transitions into my holiday list? When hovering over an image, I want it to display a larger size and then return to normal when the mouse is moved off. I understand how to do this with JavaScript, but I prefer to achieve this e ...

How many intricate objects are instantiated in memory when running the code snippet provided above?

When looking at the given code, how many complex objects can we identify in memory? function Foo() { this.one = function() { return "one"; } } var f = new Foo(); Foo.two = function() { return "two"; }; Here's what I see: The Foo co ...

Adjust the element based on hover over the pseudo element

Looking to modify an element based on the hover state of a pseudo-element. Here's what I've tried so far, but it isn't working as expected: nav .logo { display: none; } nav:before{} nav:hover:before .logo { display: block; } I want th ...

Python is the way to go for clicking on a link

I have a piece of HTML and I am in need of a Python script using selenium to click on the element "Odhlásit se". <div class="no-top-bar-right"> <ul class="vertical medium-horizontal menu" dat ...

Develop a list of findings from the search

Is there a way to extract the name from the image shown below? return testData.then((data) => { console.log(data) var results = []; var toSearch = params.suggestTerm; data = data["data"]["0"]; console.log(" ...

Issue encountered while attempting to remove a row from a table (JavaScript)

I'm encountering an error when attempting to delete a table row: "Uncaught ReferenceError: remTable is not defined index.html:1:1". When I inspect index.html to identify the issue, I find this: remTable(this) This is my code: const transact ...

Is there a similar function in AngularJS that is equivalent to getJSON? Just curious, as I am new to this

As a beginner in javascript, angularJS, and JQuery, I recently embarked on programming an angularJS app that involves using JQuery to fetch JSON data from a webserver. Here is the code snippet I am working with: var obj = $.getJSON( "http://something.com/ ...