I want to slide my search box from right to left on the click of search button. The search box should be hidden. The transition works fine when I switch the margin-right
property from -220px
to 0px
.
However, when I add the display: none
or display: inline-block
property, it does not work. I also tried visibility
property. It slides, but I want it to slide from the start of the button and not behind the button.
I don't know where I am going wrong.
Please help. Please check the jsfiddle here...
$(document).on("click", "#searchtoolbar", function() {
var hasclass = $("#search").hasClass("searchshow");
if (!hasclass) {
$("#search").addClass("searchshow").focus();
} else {
$("#search").removeClass("searchshow");
}
});
@import "http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css";
#inputbar {
border-color: red;
list-style: none;
}
#search {
background: #007F64;
/*display:none;*/ /*It does not work with the transition when I do this.*/
visibility: hidden;
right: 300px;
color: white;
position: absolute;
border: none;
top: 18px;
height: 27px;
width: 177px;
padding: 8px 4px;
margin-right: -220px;
transition: margin-right;
transition-duration: .5s;
}
#search.searchshow {
border-color: #41A940;
outline: none;
margin-right: 100px;
visibility: visible;
/* display:inline-block; */ /* The transition does not work if I do so*/
}
#searchtoolbar {
background: #007F64;
color: white;
position: absolute;
right: 260px;
top: 18px;
border: none;
font-size: 18px;
font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<li id="inputbar">
<input type="search" id="search" placeholder="What are you looking for?">
</li>
<button id="searchtoolbar" type="submit" aria-label="Search button">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>