Javascript css display none display block transition

user3018000

CSS can't make a transition from display: none to display: block for that i'm trying to make it using javascript i don't want to use visibility: hidden and height: 0 or transform: scale(0)

for that i'm using javascript to make a transition from display: none to display: block

The code below work when i add the active class the transition working fine, But when i remove the class active the transition it's not working

How can i make the transition work in both when i add and when i remove the class active using javascript and css ?

let parents = document.querySelectorAll('.parent');

parents.forEach(p => {
    p.addEventListener("click",() => {
        if(p.classList.contains('active')){
            setTimeout(function () {
                p.nextElementSibling.classList.remove("visually");
                
              }, 20);
                p.classList.remove("active");
           
        }else{
            p.classList.add("active");
            setTimeout(function () {
                p.nextElementSibling.classList.add('visually');
              }, 20);
        }

    });
});
.link-inner {
cursor: pointer;
}

.toggle {
    opacity: 0;
    display: none;
    transition: all .5s ease-in-out;
}

.parent.active+.toggle {
    display: block;
}

.parent.active+.visually {
    opacity: 1;
}
    <div class="link">
    <div class="link-inner parent">
        <div class="linkText">
            <h4>Title</h4>
        </div>
    </div>
    <div class="linkInfo toggle ">
    <a href="#" target="_blank">link 1</a>
    </div>
</div>

    <div class="link">
    <div class="link-inner parent">
        <div class="linkText">
            <h4>Title</h4>
        </div>
    </div>
    <div class="linkInfo toggle ">
    <a href="#" target="_blank">link 2</a>
    </div>
</div>

Tom

You were actually really nearly there. You just had to swap the calls when removing classes and add a bit more of a delay in your setTimeout

let parents = document.querySelectorAll('.parent');

parents.forEach(p => {
    p.addEventListener("click",() => {
        if(p.classList.contains('active')){
            // remove the class that sets opacity so it fades away
            p.nextElementSibling.classList.remove("visually");
            setTimeout(function () {
                // remove the class that sets display: block after the opacity is gone
                p.classList.remove("active");
                
              }, 500);
        } else {
            p.classList.add("active");
            setTimeout(
                function () {
                    p.nextElementSibling.classList.add('visually');
                },
                20
            );
        }

    });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Css transition from display none to display block, navigation with subnav

CSS Transition from display none to display block and vice versa

JavaScript - add transition between display:none and display:block

css transition not working for display: none

CSS Opacity transition with display: none

CSS transition disabled by `display:none`

CSS transition on display:block not working

css transition opacity is not working where element had display:none then changed to display:block

CSS : Image hover transition not working with display none / display:block and image swap

How do you transition between display:none and display:block?

CSS transition display: none is getting reversed

Transition not working when changing from display none to block

Print Javascript is blank with Display : block/none

CSS image overlay delay display:none to display:block

CSS FadeIn effect from display: none to display: block

css onclick change display:none to display:block property

Css ul display is none when hover display is block

Onclick Display Block to None and None to Block in jQUERY

Hover transition on display block not working

How to change css with javascript of multiple images - display: none to display: inline

Display:inline-block before transition with pure Javascript

How to change css display none or block with button click

How the css property display none, and block works in html file?

Get modal fade in in with display block CSS Javascript

display certain element inside a display:none block

How to toggle between display:none and display:block

How to toggle display:none, display:block

Display:none / Display:block with Combined Sibling Selectors

CSS Animation and Display None