Despite having released a new version of CSS, CSS3 is not yet defined to change the appearance of some things and it does exist. There is the compatibility issue between some browsers.
The use of a library is a good alternative, but for a design where speed and better performance are required, it is not the best option, even more so when these libraries tend to load unnecessary css styles and to talk about the extensive js script, when wanting to add things. attractive still site and relying on libraries that would mean having to use a library for each new thing you want to add.
There are other alternatives such as a similar design in select option
order to give a more attractive design to the default design.
What I want to achieve from this costume is that it has the same functions as a select option
normal one... That is, that it hides when you click elsewhere, that if there are a large number of options, not everything is shown, that it shows a bar scrollbar
such like a select option
normal does.
https://jsfiddle.net/aht3nr4u/
For the disguise using other tags I have the following working:
$("body").on("click", ".selected", function() {
$(this).next(".options").toggleClass("open");
});
$("body").on("click", ".option", function() {
var value = $(this).find("span").html();
$(".selected").html(value);
$("#sel").val(value);
$(".options").toggleClass("open");
});
.container {
display: flex;
flex-wrap: wrap;
width: 25%;
}
.selected {
border: thin solid darkgray;
border-radius: 5px;
background: lightgray;
display: flex;
align-items: center;
cursor: pointer;
height: 1.5em;
margin-bottom: .2em;
padding-left: .5em;
min-width: 150px;
position: relative;
}
.selected:after {
font-family: FontAwesome;
content: "\f0d7";
margin-left: 1em;
position: absolute;
right: .5em;
}
.options {
display: none;
margin: 0;
padding: 0;
}
.options.open {
display: inline-block;
}
li {
display: flex;
justify-content: flex-start;
align-items: center;
cursor: pointer;
}
li.search {
padding: 0.5rem 0;
}
li>img {
margin-right: 1em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<form>
<input type="hidden" id="sel">
<div class="container">
<div class="selected">Select an option</div>
<ul class="options" id="fbody">
<input type="text" id="search">
<li class="option"><span>Option 1</span></li>
<li class="option"><span>Option 2</span></li>
<li class="option"><span>Option 3</span></li>
<li class="option"><span>Option 4</span></li>
<li class="option"><span>Option 5</span></li>
<li class="option"><span>Option 6</span></li>
<li class="option"><span>Option 7</span></li>
<li class="option"><span>Option 8</span></li>
<li class="option"><span>Option 9</span></li>
<li class="option"><span>Option 10</span></li>
<li class="option"><span>Option 11</span></li>
<li class="option"><span>Option 12</span></li>
<li class="option"><span>Option 13</span></li>
<li class="option"><span>Option 14</span></li>
<li class="option"><span>Option 15</span></li>
<li class="option"><span>Option 16</span></li>
<li class="option"><span>Option 17</span></li>
<li class="option"><span>Option 18</span></li>
<li class="option"><span>Option 19</span></li>
<li class="option"><span>Option 20</span></li>
<li class="option"><span>Option 21</span></li>
<li class="option"><span>Option 22</span></li>
<li class="option"><span>Option 23</span></li>
<li class="option"><span>Option 24</span></li>
<li class="option"><span>Option 25</span></li>
<li class="option"><span>Option 26</span></li>
<li class="option"><span>Option 27</span></li>
<li class="option"><span>Option 28</span></li>
<li class="option"><span>Option 29</span></li>
<li class="option"><span>Option 30</span></li>
<li class="option"><span>Option 31</span></li>
<li class="option"><span>Option 32</span></li>
<li class="option"><span>A</span></li>
<li class="option"><span>B</span></li>
<li class="option"><span>C</span></li>
<li class="option"><span>D</span></li>
<li class="option"><span>E</span></li>
<li class="option"><span>H</span></li>
<li class="option"><span>E</span></li>
<li class="option"><span>E</span></li>
<li class="option"><span>A</span></li>
<li class="option"><span>Other</span></li>
</ul>
</div>
</form>
Which meets the following conditions:
- When I click, it shows me the list of options.
- When selecting an option it is added in the same way as if it were a
select option
and at the same time the same value is added in ainput
hidden field.
My idea in the end is to achieve is to have the following design
The la bar scrollbar
could be edited by adding this jsfiddle demo style .
I have almost everything working, it would only be necessary to add those small details that can be achieved through CSS3 styles, but I have not been able to give it the best style :(
I have found these examples of css styles in codepen and in (jsfiddle)[ http://jsfiddle.net/Starx/a6NJk/2/]
But I haven't managed to do much despite the examples.