Button animation when showing/hiding

1 week ago 9
ARTICLE AD BOX

The empty space happens because you’re only hiding the content visually (opacity/visibility). It still takes up layout height, so the button is pushed down.

Fix: collapse the content’s height (and optionally padding/margin) when hidden, and animate the button with a transform so it “slides up” into view.

youssef belkassemi's user avatar

1 Comment

Thanks for the answer. It would be great if you could show an example of animation based on my code.

2026-01-16T14:35:27.177Z+00:00

You can add a class to the scrollable content and have two states for it: the first would be when it's visible, the second when it's hidden:

.scrolling-content { height:500px; transition: all 5s ease; } .scrolling-content.hidden { height: 0px; opacity: 0; }

Of course, you'll also need to handle button clicks and add/remove the hidden class when/where appropriate:

document.addEventListener("DOMContentLoaded", function() { const contentBlock = document.querySelector('.content'); const toggleButton = document.querySelector('.toggle-button'); const scrollingContent = document.querySelector('.scrolling-content') // Manually switching the visibility of a block (by clicking a button) function toggleVisibility() { if (contentBlock.classList.contains('hidden-content')) { contentBlock.classList.remove('hidden-content'); contentBlock.classList.add('show-content'); scrollingContent.classList.remove('hidden'); } else { contentBlock.classList.remove('show-content'); contentBlock.classList.add('hidden-content'); scrollingContent.classList.add('hidden'); } } // Button click event handler toggleButton.addEventListener('click', () => { toggleVisibility(); }); // Handling browser window scrolling window.addEventListener('scroll', () => { if (scrollingContent.classList.contains('hidden')) return; if (window.scrollY > 150) { // If you scrolled down the page by 250px toggleButton.style.display = 'block'; // The appearance of the button contentBlock.classList.remove('show-content'); // The content block is hidden contentBlock.classList.add('hidden-content'); scrollingContent.classList.add("hidden"); setTimeout(function() { window.scroll(0, 0); } , 1000); } else { // If you scrolled back up toggleButton.style.display = 'none'; // The button is hidden contentBlock.classList.remove('hidden-content'); // The content block appears again contentBlock.classList.add('show-content'); } }); }); .sticky-block { position: sticky; top: 90px; z-index: 999; transition: all .2s ease-in-out; height:750px; } .content { transition: opacity 0.5s ease-in-out; } .toggle-button { display: none; max-width: 200px; margin: 0 auto; background-color: rgb(191, 79, 203); border-radius: 50px; cursor: pointer; transition: all 0.3s ease; } .toggle-button:hover { background-color: rgb(143, 62, 184); } .hidden-content { opacity: 0; visibility: hidden; } .show-content { opacity: 1; visibility: visible; } .scrolling-content { height:500px; transition: all 5s ease; } .scrolling-content.hidden { height: 0px; opacity: 0; } <div class="sticky-block"> <span class="content">Content Here</span> <div class="scrolling-content">Scrolling content</div> <button class="toggle-button">Show/Hide</button> </div>

EDIT

document.addEventListener("DOMContentLoaded", function() { const contentBlock = document.querySelector('.content'); const toggleButton = document.querySelector('.toggle-button'); // Manually switching the visibility of a block (by clicking a button) function toggleVisibility() { if (contentBlock.classList.contains('hidden-content')) { contentBlock.classList.remove('hidden-content'); contentBlock.classList.add('show-content'); } else { contentBlock.classList.remove('show-content'); contentBlock.classList.add('hidden-content'); } } // Button click event handler toggleButton.addEventListener('click', () => { toggleVisibility(); contentBlock.classList.add('wait'); setTimeout(function() {contentBlock.classList.remove('wait')}, 200); }); // Handling browser window scrolling window.addEventListener('scroll', () => { if (contentBlock.classList.contains("wait")) return; if (window.scrollY > 50) { // If you scrolled down the page by 50px toggleButton.style.display = 'block'; // The appearance of the button contentBlock.classList.remove('show-content'); // The content block is hidden contentBlock.classList.add('hidden-content'); contentBlock.classList.add('wait'); } else { // If you scrolled back up contentBlock.classList.remove('hidden-content'); // The content block appears again contentBlock.classList.add('show-content'); } }); }); .sticky-category { position: sticky; top: 10px; z-index: 999; transition: all .2s ease-in-out; } .content { transition: opacity 0.5s ease-in-out; } .toggle-button { display: none; max-width: 200px; margin: 0 auto; padding: 10px; background-color: rgb(191, 79, 203); border-radius: 50px; border: none; cursor: pointer; transition: all 0.3s ease; color: #fff; } .toggle-button:hover { background-color: rgb(143, 62, 184); } .hidden-content { display: none; } <div class="sticky-category"> <span class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in felis in felis placerat pulvinar. Sed eget fermentum justo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Quisque placerat condimentum laoreet. Mauris facilisis mi tellus, a blandit magna viverra in. Aenean varius pretium elit vitae finibus. Fusce vel cursus tellus, non sollicitudin mi.</span> <button class="toggle-button">Show/Hide</button> </div> <div style="height: 500px;"></div>

Lajos Arpad's user avatar

5 Comments

Thank you for your answer. I updated the code because the previous one was incorrect. Could you please correct your answer a little?

2026-01-16T17:54:15.837Z+00:00

I would be glad to improve upon the answer, but I'm not sure what's wrong with it. Can you elaborate on 1. Whether you have already managed to solve the issue, 2. If not, what is currently missing, 3. How is my answer not a solution? That would help me to apply a meaningful improvement, because to my understanding my answer was correct, but as you pointed out, the premise was incorrect and likely my answer is also incorrect. If I could understand the exact goals, then I could offer more help. Thank you.

2026-01-16T18:01:57.23Z+00:00

I changed the HTML layout and style in my code. That's why my code works slightly differently. Unfortunately, your code only works with the old layout. I need to create a sliding effect so that when the content is hidden the button moves up, and when the content is shown the button moves down.

2026-01-16T19:03:27.64Z+00:00

I have added a new snippet based on your code, implementing what I think could be the expectation, please let me know if it's wrong.

2026-01-16T19:44:01.557Z+00:00

You probably haven't noticed. When I scroll up the screen, the button disappears and the content appears. This doesn't happen in your code.

2026-01-17T03:50:42.307Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article