# Add fade effect on element expand/collapse

**URL:** <https://forum.wixstudio.com/t/add-fade-effect-on-element-expand-collapse/41790>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [May 20, 2022, 4:28pm UTC](https://forum.wixstudio.com/t/add-fade-effect-on-element-expand-collapse/41790 "2022-05-20T16:28:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastienroten](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sebastienroten/32/5850_2.png) [@sebastienroten](https://forum.wixstudio.com/u/sebastienroten)\
**Post date:** [May 20, 2022, 4:28pm UTC](https://forum.wixstudio.com/t/add-fade-effect-on-element-expand-collapse/41790/1 "2022-05-20T16:28:26Z")

</div>

Hi everybody,  
I created a section with 2 elements (boxes) that appear when you click on a button (#button1, #button2 in my example).  
But the boxes appear in an abrupt/direct way.  
I wish to add a fade effect when they appear and I don’t know what part of the code I should modify or add?

Here is my code :

$w . onReady ( **function** () {  
$w ( “#button1” ). onClick (() =\> {  
**if** ( $w ( “#box1” ). collapsed ) {  
$w ( “#box1” ). expand ();  
$w ( “#box2” ). collapse ();  
}  
});

$w ( “#button2” ). onClick (() =\> {  
**if** ( $w ( “#box2” ). collapsed ) {  
$w ( “#box2” ). expand ();  
$w ( “#box1” ). collapse ();  
}  
});

Someone to help me ? 😂  
Thanks for reading  
Best

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [May 20, 2022, 4:52pm UTC](https://forum.wixstudio.com/t/add-fade-effect-on-element-expand-collapse/41790/2 "2022-05-20T16:52:33Z")

</div>

I do not see any integrated (coded) FADE-EFFECT inside your provided code!  
Where is your, coded “fade–action” ???

You will have to use some code like…

//----showing element (box)--------------------------  
$w ( “#box2” ). expand (); $w ( “#box2” ). show (‘fade’);

//----hiding element (box)--------------------------  
$w ( “#box1” ). hide (); $w ( “#box1” ). collapse ();

But this still won’t work properly! WHY??? → Because you do not wait the fade-out-time, before collapsing the box!!!

How to solve it??? → maybe using a async+await? (optionaly → setTimeOut)

```javascript
$w.onReady(function () {
    $w("#button1").onClick(async()=> {console.log("Button was clicked!!!);
        if ($w("#box1").collapsed) {console.log("Box-1 is collapsed!!!);
            $w("#box1").expand(); $w("#box1").show('fade');
            await $w("#box2").hide('fade'); $w("#box2").collapse();
        }
    });

    $w("#button2").onClick(()=> {
        if ($w("#box2").collapsed) {
            $w("#box2").expand();
            $w("#box1").collapse();
        }
    });
});

```

Do you recognize the difference between the action of button1 and button2 ???

---

<div class="post-metadata">

**Author:** ![sebastienroten](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sebastienroten/32/5850_2.png) [@sebastienroten](https://forum.wixstudio.com/u/sebastienroten)\
**Post date:** [May 24, 2022, 1:14pm UTC](https://forum.wixstudio.com/t/add-fade-effect-on-element-expand-collapse/41790/3 "2022-05-24T13:14:51Z")

</div>

@russian-dima  
Thanks a lot, yes I understood and applied this and it works very well. Thanks.

But I face another problem for the creation of my element… I don’t know if it’s because of the “async/await” or my global code but its work perfectly on desktop but doesn’t work on tab and mobile …  
On tab and mobile, after the first “CTA call”, all boxes disapear !? I don’t know why !

If you see where is the problem, I would be very grateful 🙂

here is my editorx building and my js code :  
(online link : [https://www.monwww.ch/solutions](https://www.monwww.ch/solutions))

The goal is that only one of the 4 boxes appears at a time …  
#box1 #box2 and #box3 are set to “collapse” (on default value)

[https://static.wixstatic.com/media/c1cae1\_9616b4e78adf4c01848a807951bc04cb~mv2.png(image larger than 4 MB)](https://static.wixstatic.com/media/c1cae1_9616b4e78adf4c01848a807951bc04cb~mv2.png)

$w . onReady ( function () {  
$w ( "# button01 " ). onClick ( **async** ()=\> {  
if ( $w ( “#box1” ). collapsed ) {  
$w ( “#box1” ). expand (), $w ( “#box1” ). show ( “fade” )  
**await** $w ( “#boxInitial,#box2, #box3” ). hide (); $w ( “#boxInitial,#box2, #box3” ). collapse ();  
}  
});

```
$w ( "# button02 " ). onClick ( **async** ()=> { 
    if ( $w ( "#box2" ). collapsed ) { 
        $w ( "#box2" ). expand (), $w ( "#box2" ). show ( "fade" ) 
        **await** $w ( "#boxInitial,#box1, #box3" ). hide (); $w ( "#boxInitial,#box1, #box3" ). collapse (); 
    } 
}); 

 $w ( "# button03 " ). onClick ( **async** ()=> { 
    if ( $w ( "#box3" ). collapsed ) { 
        $w ( "#box3" ). expand (), $w ( "#box3" ). show ( "fade" ) 
        **await** $w ( "#boxInitial,#box1, #box2" ). hide (); $w ( "#boxInitial,#box1, #box2" ). collapse (); 
    } 
}); 

$w ( "# button002 " ). onClick ( **async** ()=> { 
    if ( $w ( "#box2" ). collapsed ) { 
        $w ( "#box2" ). expand (), $w ( "#box2" ). show ( "fade" ) 
        **await** $w ( "#boxInitial,#box1, #box3" ). hide (); $w ( "#boxInitial,#box1, #box3" ). collapse (); 
    } 
});     
    
$w ( "# button003 " ). onClick ( **async** ()=> { 
    if ( $w ( "#box3" ). collapsed ) { 
        $w ( "#box3" ). expand (), $w ( "#box3" ). show ( "fade" ) 
        **await** $w ( "#boxInitial,#box1, #box2" ). hide (); $w ( "#boxInitial,#box1, #box2" ). collapse (); 
    } 
}); 

$w ( "# button0001 " ). onClick ( **async** ()=> { 
    if ( $w ( "#box1" ). collapsed ) { 
        $w ( "#box1" ). expand (), $w ( "#box1" ). show ( "fade" ) 
        **await** $w ( "#boxInitial,#box2, #box3" ). hide (); $w ( "#boxInitial,#box2, #box3" ). collapse (); 
    } 
}); 

$w ( "# button0003 " ). onClick ( **async** ()=> { 
    if ( $w ( "#box3" ). collapsed ) { 
        $w ( "#box3" ). expand (), $w ( "#box3" ). show ( "fade" ) 
        **await** $w ( "#boxInitial,#box1, #box2" ). hide (); $w ( "#boxInitial,#box1, #box2" ). collapse (); 
    } 
}); 

$w ( "# button00001 " ). onClick ( **async** ()=> { 
    if ( $w ( "#box1" ). collapsed ) { 
        $w ( "#box1" ). expand (), $w ( "#box1" ). show ( "fade" ) 
        **await** $w ( "#boxInitial,#box2, #box3" ). hide (); $w ( "#boxInitial,#box2, #box3" ). collapse (); 
    } 
}); 

$w ( "# button00002 " ). onClick ( **async** ()=> { 
    if ( $w ( "#box2" ). collapsed ) { 
        $w ( "#box2" ). expand (), $w ( "#box2" ). show ( "fade" ) 
        **await** $w ( "#boxInitial,#box1, #box3" ). hide (); $w ( "#boxInitial,#box1, #box3" ). collapse (); 
    } 
});  

```

});

😪

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [May 24, 2022, 4:54pm UTC](https://forum.wixstudio.com/t/add-fade-effect-on-element-expand-collapse/41790/4 "2022-05-24T16:54:36Z")

</div>

@sebastienroten  
You are coding a lot for a little function.

Maybe you first test this one, and try to understand it…

```javascript
let myBoxIDs=[]; 
    myBoxIDs[0] = "#box1";
    myBoxIDs[1] = "#box2";
    myBoxIDs[2] = "#box3";
  //myBoxIDs[3] = "#box4"; <-- activate more boxes here

$w.onReady(function () {
    $w("#button01").onClick(async()=> {close_allBoxes(); open_Box(0);});
    $w("#button02").onClick(async()=> {close_allBoxes(); open_Box(1);});
    $w("#button03").onClick(async()=> {close_allBoxes(); open_Box(2);});
});

function close_allBoxes() {
    for (let i = 0; i < myBoxIDs.length; i++) {
       $w("#box"+i).hide();
       $w("#box"+i).collapse();        
    }
}

function open_Box(ID) {$w("#box"+ID).expand(); $w("#box"+ID).show();}

```

What is different to your code?

- this one first closes all boxes and immediately opens just the one you want have to be opened on the according button-click.  
-This one is more flexible → because you can easely add more buttons to work the same way, or you maybe want to delete some buttons.  
-this code is more compact.

After you got it to work, do the next step and add the ASYNC-AWAIT-function.  
If it still do not work, you could maybe use → “setTimeout(()=\>{},delayTimeHere);”  
for your purposes as workaround.

Try it out and expand your coding horizons!

GOOD-LUCK!

---

<div class="post-metadata">

**Author:** ![sebastienroten](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sebastienroten/32/5850_2.png) [@sebastienroten](https://forum.wixstudio.com/u/sebastienroten)\
**Post date:** [June 1, 2022, 6:46pm UTC](https://forum.wixstudio.com/t/add-fade-effect-on-element-expand-collapse/41790/5 "2022-06-01T18:46:25Z")

</div>

@russian-dima  
Many thanks for your time and help.  
I understand your code and construction even if I am a rookie in velo …  
It work perfectly.  
I m going to add the fade effect and maybe back to you if … 😀  
Thanks again and have a nice evening.  
Seb
