如何为快捷方式创建自定义搜索功能

时间:2015-05-12 09:37:21

标签: javascript jquery html css twitter-bootstrap

目标:
单击带有图标阅读玻璃a的按钮时,文本字段应从右向左进入。

按下按钮后,光标应位于文本字段中,并准备好检索输入数据,并且应显示按钮周围的颜色框,例如黄色。

当您在文本字段外使用光标时,突然按下鼠标左键,文本字段和按钮周围的黄色颜色将消失。

问题:
我现在不知道如何创建它。

的信息:
*我使用bootstrap和jQuery *今天,我没有可用的源代码。

enter image description here

2 个答案:

答案 0 :(得分:0)

您需要的是按钮上的两个操作:

  1. 显示输入字段
  2. 触发表格
  3. 以下代码就是这样做的。页面加载时输入的宽度为零。 jQuery函数绑定按钮上的click事件。

    单击它时,将查看输入字段是否具有宽度。如果没有,它将阻止默认操作(提交表单),而是将输入设置为200px的宽度。之后,它专注于该领域。

    第二次单击按钮时,输入的宽度不会为零,因此按钮动作正常并将提交表单。

    希望它适合你。

    
    
    $(function() {
        $('#search-form button').on('click', function(e) {
            var btn = $(this);
            var inp = $(this).parent().find("input");
            
            if( inp.width() == 0 ) {
                e.preventDefault();
                inp.animate({width: "200px"}, 500).focus();        
                btn.addClass('active-btn');
            }
        });
    });
    
    * {
        box-sizing: border-box;
    }
    body {
        background: black;
    }
    #search-form input {
        color: #fff;
        height: 50px;
        background: #484848;
        border: 0;
        float: right;
        width: 0;
    }
    #search-form button {
        background: #484848;
        color: #fff;
        width: 50px;
        height: 50px;
        border: 1px solid #484848;
        float: right;
    }
    #search-form .active-btn {
        border: 1px solid #57ABD9;
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form id="search-form">
        <button type="submit">&#128270;</button><input type="text" name="search" />
    </form>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

我希望这样的事情:

注意 - 它也是响应

DEMO FULL SCREEN DEMO

<强> HTML

<div id="sb-search" class="sb-search " >
    <form>
        <input class="sb-search-input " onkeyup="buttonUp();" placeholder="Enter your search term..." type="search" value="" name="search" id="search"/>
            <input class="sb-search-submit" type="submit"  value=""/>
        <span class="sb-icon-search"><i class="glyphicon glyphicon-search"></i></span>
    </form>
</div>

CSS [如果需要]

body{

    margin: 40px 60px;
    background:#555;
}

.sb-search {
    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;
}

.sb-search-input {
    position: absolute;
    top: 0;
    right: 0px;
    border: none;
    outline: none;
    background: #fff;
    width: 100%;
    height: 60px;
    margin: 0;
    z-index: 10;
    padding: 20px 65px 20px 20px;
    font-family: inherit;
    font-size: 20px;
    color: #2c3e50;
}

input[type="search"].sb-search-input {
    -webkit-appearance: none;
    -webkit-border-radius: 0px;
}
.sb-search-input::-webkit-input-placeholder {
    color: #efb480;
}

.sb-search-input:-moz-placeholder {
    color: #efb480;
}

.sb-search-input::-moz-placeholder {
    color: #efb480;
}

.sb-search-input:-ms-input-placeholder {
    color: #efb480;
}

.sb-icon-search,
.sb-search-submit  {
    width: 60px;
    height: 60px;
    display: block;
    position: absolute;
    right: 0;
    top: 0;
    padding: 0;
    margin: 0;
    line-height: 60px;
    text-align: center;
    cursor: pointer;
}

.sb-search-submit {
    background: #fff; /* IE needs this */
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; /* IE 8 */
    filter: alpha(opacity=0); /* IE 5-7 */
    opacity: 0;
    color: transparent;
    color:red;
    border: none;
    outline: none;
    z-index: -1;
}

.sb-icon-search {
    color: #fff;
    background: #e67e22;
    z-index: 90;
    font-size: 22px;
    font-family: 'icomoon';
    speak: none;
    font-style: normal;
    font-weight: normal;
    font-variant: normal;
    text-transform: none;
    -webkit-font-smoothing: antialiased;
}

.sb-icon-search:before {
    content: "";
}

.sb-search.sb-search-open,
.no-js .sb-search {
    width: 100%;
}

.sb-search.sb-search-open .sb-icon-search,
.no-js .sb-search .sb-icon-search {
    background: #da6d0d;
    color: #fff;
    z-index: 11;
}

.sb-search.sb-search-open .sb-search-submit,
.no-js .sb-search .sb-search-submit {
/*    z-index: 90;*/
}

<强> JS

function buttonUp(){
     var valux = $('.sb-search-input').val(); 
        valux = $.trim(valux).length;
        if(valux !== 0){
            $('.sb-search-submit').css('z-index','99');
        } else{
            $('.sb-search-input').val(''); 
            $('.sb-search-submit').css('z-index','-999');
        }
}

$(document).ready(function(){
    var submitIcon = $('.sb-icon-search');
    var submitInput = $('.sb-search-input');
    var searchBox = $('.sb-search');
    var isOpen = false;

    $(document).mouseup(function(e){

        if(isOpen == true){
        submitInput.val('');
        $('.sb-search-submit').css('z-index','-999');
        submitIcon.click();
        }
    });

    submitIcon.mouseup(function(){
        return false;
    });

    searchBox.mouseup(function(){
        return false;
    });

    submitIcon.click(function(){
        if(isOpen == false){
            searchBox.addClass('sb-search-open');
            $('.sb-search-input').focus();
            isOpen = true;
        } else {
            searchBox.removeClass('sb-search-open');
            $('.sb-search-input').blur();
            isOpen = false;
        }
});
});