使用CSS3和jQuery可伸縮的搜尋條

來源:互聯網
上載者:User

標籤:import   還需要   應用   one   state   mit   hold   idt   條件   

搜尋條在我們網站是必不可少的,尤其是在有限的頁面空間裡,放置一個重要的搜尋條是個難題,今天我將結合執行個體給大家介紹一下如何使用CSS3和jQuery來實現一個可伸縮功能的搜尋條。

HTML

在需要放置搜尋條的頁面中放置如下html代碼,搜尋條#search_bar包含一個form#myform表單,表單中放置一個搜尋輸入框#search,一個搜尋按鈕.search_btn以及搜尋按鈕表徵圖.search_ico。

<div id="search_bar" class="search_bar"> 
    <form id="myform"> 
        <input class="input" placeholder="想搜點什麼呢..." type="text" name="key" id="search"> 
        <input class="search_btn" type="submit" value=""> 
        <span class="search_ico"></span> 
    </form> 
</div> 
CSS

我們通過CSS來將整個搜尋條布局美化,其中我們使用了CSS3代碼。

.search_bar{position: relative;margin-top: 10px; 
    width: 0%;min-width: 60px;height: 60px; 
    float: right;overflow: hidden; 
    -webkit-transition: width 0.3s; 
    -moz-transition: width 0.3s; 
    transition: width 0.3s; 
    -webkit-backface-visibility: hidden; 
    background:#162934; 
} 
 
.input{ 
    position: absolute;top: 0;right: 0; 
    border: none;outline: none; 
    width: 98%;height: 60px; line-height:60px;z-index: 10; 
    font-size: 20px;color: #f9f9f9;background:transparent 
} 
 
.search_ico,.search_btn  { 
    width: 60px;height: 60px;display: block; 
    position: absolute;right: 0;top: 0; 
    padding: 0;margin: 0;line-height: 60px;cursor: pointer; 
} 
 
.search_ico{background: #e67e22 url(icon.png) no-repeat 18px 20px;z-index:90;} 
.search_open{width: 100% !important; z-index:1002} 
#show{position:absolute; padding:20px} 

上述代碼中關鍵的是transition: width 0.3s;可以實現CSS3的動畫效果,width由0變成100%,具體大家可以去看下CSS3手冊相關介紹,這裡不多描述,你可以直接複製和修改代碼應用到你的項目中去。

jQuery

當點擊搜尋按鈕時,搜尋條.search_bar通過toggleClass()轉場樣式.search_open,這就實現了搜尋條收縮和伸展功能。另外我們還需要判斷輸入情況,當輸入滿足條件時,提交搜尋表單實現搜尋功能,請看代碼:

$(function(){ 
    $(".search_ico").click(function(){ 
        $(".search_bar").toggleClass(‘search_open‘); 
        var keys = $("#search").val(); 
        if(keys.length>2){ 
            $("#search").val(‘‘); 
            $("#myform").submit(); 
        }else{ 
            return false; 
        } 
    }); 
}); 

該效果可以運用到移動端項目中,當然你也可以添加手動滑動效果。

使用CSS3和jQuery可伸縮的搜尋條

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.