I want to hide the content by positioning it bottom: 100% in parent;
Then on click I want it to slide down to top:0;
Currently this snippet does not work (at least no animation).
I dont want to position content to top:-100% because I want animation to start immediately, not wait for content to travel whole parent height (that why I put bottom:100%).
var content = document.querySelector('.content')
document.querySelector('#toggle').addEventListener('click', function() {
if (content.classList.contains('opened')) {
content.classList.remove('opened')
} else {
content.classList.add('opened')
}
})
.wrap {
position: relative;
width: 300px;
height: 500px;
background: #ddd;
overflow: hidden;
}
.content {
position: absolute;
bottom: 100%;
transition: all 0.5s;
}
.opened {
bottom: auto;
top: 0;
}
#toggle {
position: absolute;
top: 0;
left: 340px;
}
<div class="wrap">
<div class="content">
Maecenas eu erat condimentum neque molestie tincidunt. Fusce egestas, est ut fringilla facilisis, quam purus blandit dui, eget egestas mauris nibh ut diam. Phasellus volutpat. Sed fringilla tellus in sem. Curabitur dignissim nunc id arcu. Nulla facilisi.
</div>
</div>
<a href="#" id="toggle">toggle</a>
2
Answers
Don’t use
position
, use atransform
ortranslate
.Just add a
transform: translateY(100%);
to the.opened
class to achieve what you want with smooth animation.Run the snippet below to see the preview: