At the top of a mobile page, I have implemented two search boxes:
https://i.sstatic.net/gCfOG.png
When a user clicks inside one of the boxes, the desired behavior is as follows:
- The selected box expands
- The other box collapses horizontally
- A close icon expands on the right.
After the transition, this is what it looks like:
https://i.sstatic.net/odGJB.png
Below is the code snippet for this functionality:
<div class="navigation--mobile__search">
<div class="header-searchfield" id="kurssuche">
<input type="text" class="header-searchfield__input" placeholder="Search1">
<a href="">
<span class="icon icon--lupe">
<svg class="icon__svg">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/assets/svg/svg-symbol.svg#lupe"></use>
</svg>
</span>
</a>
</div>
<div class="header-searchfield" id="volltextsuche">
<input type="text" class="header-searchfield__input" placeholder="Search2">
<a href="">
<span class="icon icon--lupe">
<svg class="icon__svg">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/assets/svg/svg-symbol.svg#lupe"></use>
</svg>
</span>
</a>
</div>
<span class="icon icon--close-x header-searchfield__close-button" style="display: none;">
<svg class="icon__svg">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/assets/svg/svg-symbol.svg#close-x"></use>
</svg>
</span>
</div>
Here are the corresponding LESS classes:
.navigation--mobile__search {
background: @common-quarks-color--cd-anthracite;
display: flex;
padding: .625rem .75rem;
position: relative;
.header-searchfield {
display: flex;
flex: 1;
&__input {
flex-grow: 1;
padding: .5625rem 2rem .5625rem .8125rem;
}
&__close-button {
flex: 0 0 1.8rem;
color: white;
display: none;
margin: .7rem 0 .7rem .5rem;
svg {
fill: @searchfield--border-color;
height: .8125rem;
width: .8125rem;
}
}
}
}
And here's the JavaScript code for the implementation:
$(function () {
$("#kurssuche")
.focusin(function() {
$("#volltextsuche").hide(500);
$(".header-searchfield__close-button").show(500);
$(".navigation--mobile__search-results").animate({height: "400px"}, 500);
})
// Other functions for focusout and clicking
});
Issue:
In order to achieve the desired collapsing behavior, I attempted different approaches with flex divs but was unable to make it work properly due to the display: flex
property. Is there a workaround to animate flex divs to collapse horizontally?
Fiddle: https://jsfiddle.net/amear6x0/