I've added a search bar in the header that is set to display: none
by default and I used js to make it appear on a button click via assigning a .show
class which contains display: block !important
to the search bar element (#search
). It's working fine but my only problem is the rough transition from display: none
to block
, so I've been looking into ways to make this transition smooth and most of the answers I found were using jQuery, which I don't really want to do since I'm still in the learning phase of js, so if there's a way I can do this using vanilla js, please help me with it.
Here's my code https://jsfiddle.net/5jxLq9ck/
In CSS line 38, I add the .show
utility class
.show {
display: block !important;
}
And I'm assuming I'll have to edit something in here (js) to get the desired effect:
function showSearch(e) {
e.preventDefault;
if (
e.target.classList.contains("show-btn") ||
e.target.classList.contains("fas")
) {
const searchBar = document.querySelector("#search");
searchBar.classList.add("show");
}
}
Additional question: is my use of e.preventDefault
correct here? The functionality didn't work until I used it.
Thanks a lot in advance.