typeahead模糊搜尋查詢-結合bootstrap和jquery
阿新 • • 發佈:2019-01-31
本文利用twitter開源元件typeahead來實現一個模糊匹配效果,包含了,動態搜尋,迷糊匹配等功能。非常強大。專案地址
專案結合了bootstrap,搜素更加美觀
專案下載下來,然後引入
<script src="/assets/plugins/bootstrap/js/typeahead.bundle.min.js" type="text/javascript"></script>
路徑自己看著辦,別照抄我的
使用
- html
<div class="input-group" id="scrollable-dropdown-menu"> <input
- js
<script type="text/javascript"
- css
上面的可以成功,但是樣式非常醜非常醜,下面我給出樣式,直接複製就可以用
大家可以自由調整
span.twitter-typeahead .tt-menu, span.twitter-typeahead .tt-dropdown-menu { position: absolute; top: 100%; width:100%; left: 0; z-index: 1000; display: none; float: left; min-width: 160px; padding: 5px 0; margin: 2px 0 0; list-style: none; font-size: 14px; text-align: left; background-color: #ffffff; border: 1px solid #cccccc; border: 1px solid rgba(0, 0, 0, 0.15); border-radius: 4px; -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175); box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175); background-clip: padding-box; max-height: 200px; //這個地方有改動,變成一個滾動的 overflow-y: auto; //這個地方有改動,變成一個滾動的 } span.twitter-typeahead .tt-suggestion { display: block; padding: 3px 20px; clear: both; font-weight: normal; line-height: 1.42857143; color: #333333; white-space: nowrap; } span.twitter-typeahead .tt-suggestion.tt-cursor, span.twitter-typeahead .tt-suggestion:hover, span.twitter-typeahead .tt-suggestion:focus { color: #ffffff; text-decoration: none; outline: 0; background-color: #337ab7; } .input-group.input-group-lg span.twitter-typeahead .form-control { height: 46px; padding: 10px 16px; font-size: 18px; line-height: 1.3333333; border-radius: 6px; } .input-group.input-group-sm span.twitter-typeahead .form-control { height: 30px; padding: 5px 10px; font-size: 12px; line-height: 1.5; border-radius: 3px; } span.twitter-typeahead { width: 100%; } .input-group span.twitter-typeahead { display: block !important; height: 34px; } .input-group span.twitter-typeahead .tt-menu, .input-group span.twitter-typeahead .tt-dropdown-menu { top: 32px !important; } .input-group span.twitter-typeahead:not(:first-child):not(:last-child) .form-control { border-radius: 0; } .input-group span.twitter-typeahead:first-child .form-control { border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 0; border-bottom-right-radius: 0; } .input-group span.twitter-typeahead:last-child .form-control { border-top-left-radius: 0; border-bottom-left-radius: 0; border-top-right-radius: 4px; border-bottom-right-radius: 4px; } .input-group.input-group-sm span.twitter-typeahead { height: 30px; } .input-group.input-group-sm span.twitter-typeahead .tt-menu, .input-group.input-group-sm span.twitter-typeahead .tt-dropdown-menu { top: 30px !important; } .input-group.input-group-lg span.twitter-typeahead { height: 46px; } .input-group.input-group-lg span.twitter-typeahead .tt-menu, .input-group.input-group-lg span.twitter-typeahead .tt-dropdown-menu { top: 46px !important; } .tt-query .tt-hint{ width: 396px; height: 30px; padding: 8px 12px; font-size: 24px; line-height: 30px; border: 2px solid #ccc; -webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px; outline: none; } .tt-hint{ color: #999; }