# scrollTo with collapsing boxes

**URL:** <https://forum.wixstudio.com/t/scrollto-with-collapsing-boxes/50411>\
**Category:** Ask the community\
**Created:** [April 24, 2021, 6:51pm UTC](https://forum.wixstudio.com/t/scrollto-with-collapsing-boxes/50411 "2021-04-24T18:51:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![calumnfisher](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/calumnfisher/32/7995_2.png) [@calumnfisher](https://forum.wixstudio.com/u/calumnfisher)\
**Post date:** [April 24, 2021, 6:51pm UTC](https://forum.wixstudio.com/t/scrollto-with-collapsing-boxes/50411/1 "2021-04-24T18:51:13Z")

</div>

I was wondering if someone could help me with this, I have set up some repeaters with expanding/collapsing boxes. I have set them up so that opening the boxes of one section closes the open boxes of the other sections, and scrolls to the top of that section - 100px to account for my header.

My problem is - when the buttons trigger the collapse of boxes in the section above the one I need the box to open in, it seems to affect the position of the anchor. Probably my code is bad (I am learning as I am going), but this is how I have it set up for now:

```javascript
    $w('#systemsrepeater, #toprepeater, #bottomrepeater').forEachItem(( $item, itemData, index) => {
        $item('#SODbutton').onClick(() => {
            $item('#PPCbox, #EDRbox, #btdbox, #lmdbox, #citbox, #tmbox').collapse(), $item('#SODbox').expand(), $item('#systemsanchor').scrollTo();
        })
        $item('#EDRbutton').onClick(() => {
            $item('#PPCbox, #SODbox, #btdbox, #lmdbox, #citbox, #tmbox').collapse(), $item('#EDRbox').expand(), $item('#systemsanchor').scrollTo();
        })
        $item('#PPCbutton').onClick(() => {
            $item('#SODbox, #EDRbox, #btdbox, #lmdbox, #citbox, #tmbox').collapse(), $item('#PPCbox').expand(), $item('#systemsanchor').scrollTo();
        })
        $item('#btdbutton').onClick(() => {
            $item('#SODbox, #EDRbox, #PPCbox, #lmdbox, #citbox, #tmbox').collapse(), $item('#btdbox').expand(), $item('#serviceanchor').scrollTo();
        })
        $item('#lmdbutton').onClick(() => {
            $item('#SODbox, #EDRbox, #btdbox, #PPCbox, #citbox, #tmbox').collapse(), $item('#lmdbox').expand(), $item('#serviceanchor').scrollTo();
        })
        $item('#citbutton').onClick(() => {
             $item('#SODbox, #EDRbox, #btdbox, #PPCbox, #lmdbox, #tmbox').collapse(), $item('#citbox').expand(),$item('#serviceanchor2').scrollTo();
        })
        $item('#tmbutton').onClick(() => {
            $item('#SODbox, #EDRbox, #btdbox, #PPCbox, #lmdbox, #citbox').collapse(), $item('#tmbox').expand(), $item('#serviceanchor2').scrollTo();
        })
        $item('#closesod, #closeppc, #closeedr').onClick(() => {
            $item('#SODbox, #EDRbox, #PPCbox').collapse();
        })
        $item('#closeb, #closel, #closec, #closet').onClick(() => {
            $item('#btdbox, #lmdbox, #citbox, #tmbox').collapse(), $item('#serviceanchor').scrollTo();
        })
})
}

```

I am also sure that there is a much simpler way of programming this! If anyone can help out that would be much appreciated

---

<div class="post-metadata">

**Author:** ![md60th](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/md60th/32/14986_2.png) [@md60th](https://forum.wixstudio.com/u/md60th)\
**Post date:** [April 28, 2021, 4:07pm UTC](https://forum.wixstudio.com/t/scrollto-with-collapsing-boxes/50411/2 "2021-04-28T16:07:27Z")

</div>

Hey,  
I think the problem is that the expand and scrollTo happened together so you get the position when the box is collapsed. Because collapse and expand return a promise you can force to wait until the box finish to expand and only then scroll to this box.  
So you can add AWAIT to the expand or call .then() after expand().

For example:

```javascript
$w('#repeater1').forEachItem(($item, itemData, index) => {
 
    $item('#button_id').onClick(() => {
        $item('#box_id').expand().then(() => {
            $item('#box_id').scrollTo();
        });
    });

 // or

    $item('#button_id').onClick(async() => {
        await $item('#box_id').expand();
        $item('#box_id').scrollTo();
    });
    
});

```

Try it and let me know if it works for you.

---

<div class="post-metadata">

**Author:** ![calumnfisher](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/calumnfisher/32/7995_2.png) [@calumnfisher](https://forum.wixstudio.com/u/calumnfisher)\
**Post date:** [April 29, 2021, 5:50pm UTC](https://forum.wixstudio.com/t/scrollto-with-collapsing-boxes/50411/3 "2021-04-29T17:50:59Z")

</div>

@md60th Thanks for this, this works for me!

I have one other question. If I choose for example, to scroll to a section, it hides that section behind the header which is always visible on my website. Is it possible to take into account the height of the header (90px)?

---

<div class="post-metadata">

**Author:** ![md60th](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/md60th/32/14986_2.png) [@md60th](https://forum.wixstudio.com/u/md60th)\
**Post date:** [April 30, 2021, 12:19pm UTC](https://forum.wixstudio.com/t/scrollto-with-collapsing-boxes/50411/4 "2021-04-30T12:19:50Z")

</div>

There is a method I use - I do not know if it is the best but it works.  
I add to the section a **horizontal line with color 0%** and dock it to the top. Then i set **negative margin as the header height**. Then I give the scrollTo to the line and not to the section.  
For example: if I have a header that is 90px high. I give the line inside the section - margin-top: -90px

---

<div class="post-metadata">

**Author:** ![calumnfisher](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/calumnfisher/32/7995_2.png) [@calumnfisher](https://forum.wixstudio.com/u/calumnfisher)\
**Post date:** [April 30, 2021, 1:50pm UTC](https://forum.wixstudio.com/t/scrollto-with-collapsing-boxes/50411/5 "2021-04-30T13:50:31Z")

</div>

Thanks, I have also been doing that but thought it felt a little clunky. Although if it works, it works!
