【问题标题】:Adding a slide down search box to navigation menu向导航菜单添加向下滑动搜索框
【发布时间】:2026-02-10 17:35:02
【问题描述】:

我正在尝试构建一个向下滑动的搜索框。一旦您按下搜索图标,我希望搜索框向下滑动。 (我希望它完全像这样 - http://www.marieclaire.co.uk/)。当它向下滑动时,我希望它是全宽的。我已经开始构建实际的搜索框,但我不知道如何让它从搜索图标上滑下来。有人有解决办法吗?

jsfiddle - https://jsfiddle.net/zx06d7vz/(屏幕底部的搜索框)

/*--------------------------------------------------------------
## Search
--------------------------------------------------------------*/
.search-site {
    float: right;
    font-size: 1.2em;
    height: 50px;
    line-height: 50px;
    padding: 0 15px
}

.search-form {
    -webkit-transition: all .3s ease-in-out;
    -o-transition: all .3s ease-in-out;
    transition: all .3s ease-in-out;
    -webkit-transition: all .3s ease-in-out 250ms ease;
    -moz-transition: all .3s ease-in-out 250ms ease;
    -ms-transition: all .3s ease-in-out 250ms ease;
    -o-transition: all .3s ease-in-out 250ms ease;
    transition: all .3s ease-in-out 250ms ease;
    background: #fff;
    height: 0;
    left: 50%;
    opacity: 0;
    overflow: hidden;
    padding-left: calc((100vw - 1200px) / 2);
    padding-right: calc((100vw - 1200px) / 2);
    position: absolute;
    top: calc(100% + 1px);
    transform: translateX(-50%);
    width: 100vw;
    z-index: 999999
}

.search-form.search-visible {
    opacity: 1;
    height: 200px
}

.search-form.search-form-permanent {
    border-bottom: none;
    height: 100px !important;
    left: 0;
    opacity: 1 !important;
    padding: 0;
    position: relative;
    transform: none;
    width: 100%;
    z-index: 5
}

.search-form.search-form-permanent .input-group {
    padding: 0;
    top: 0
}

.search-form.search-form-permanent .button-search {
    color: #33f;
    outline: none
}

.search-form.search-form-permanent .button-search:hover {
    color: #b4c5cd
}

.search-form .input-group {
    padding: 0 10px;
    position: relative;
    top: 72px;
    width: 100%
}

.search-form .form-control {
    background: #fff;
    border: none;
    border-bottom: 1px #b4c5cd solid;
    border-radius: 0;
    box-shadow: none;
    float: left;
    height: auto;
    padding: 0;
    outline: none;
    width: calc(100% - 36px) !important
}

.search-form .form-control:focus {
    background: #fff !important
}

.search-form .button-search {
    background: transparent;
    border: none;
    float: right;
    font-size: 1.4em;
    padding: 6px 0 0 0;
    width: 36px
     background: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-search-strong-128.png') 0 0 no-repeat;
}
<form role="search" method="get" class="search-form form-inline search-visible" action="">
            <div class="input-group">
                <input type="search" value="" name="s" class="input-sm search-field form-control" placeholder="Search">
                <button type="submit" class="button-search icon-search"></button>
            </div>
        </form>

【问题讨论】:

  • 这个 sn-p 的 JSFiddle 代码太多,很难在可怕的分屏查看器中使用。我建议立即解决您的问题,并删除这些无用的导航菜单。专注于构建搜索功能,然后正确添加菜单。否则,我无能为力。
  • 我编辑了它

标签: javascript jquery html css search


【解决方案1】:

测试那个。

HTML:

<h1 class="search">S</h1>
<form role="search" method="get" class="search-form form-inline" action="">
            <div class="input-group">
                <input type="search" value="" name="s" class="input-sm search-field form-control" placeholder="Search">
                <button type="submit" class="button-search icon-search"></button>
            </div>
        </form>

CSS

/*--------------------------------------------------------------
## Search
--------------------------------------------------------------*/
.search-site {
    float: right;
    font-size: 1.2em;
    height: 50px;
    line-height: 50px;
    padding: 0 15px
}

.search-form {
    -webkit-transition: all .3s ease-in-out;
    -o-transition: all .3s ease-in-out;
    transition: all .3s ease-in-out;
    -webkit-transition: all .3s ease-in-out 250ms ease;
    -moz-transition: all .3s ease-in-out 250ms ease;
    -ms-transition: all .3s ease-in-out 250ms ease;
    -o-transition: all .3s ease-in-out 250ms ease;
    transition: all .3s ease-in-out 250ms ease;
    background: #fff;
    left: 50%;
    opacity: 0;
    overflow: hidden;
    padding-left: calc((100vw - 1200px) / 2);
    padding-right: calc((100vw - 1200px) / 2);
    position: absolute;
    top: -100px;
    transform: translateX(-50%);
    width: 100vw;
    z-index: 999999
}

.search-form.search-visible {
    opacity: 1;
    height: 200px;
    top: 0;
}

.search-form.search-form-permanent {
    border-bottom: none;
    height: 100px !important;
    left: 0;
    opacity: 1 !important;
    padding: 0;
    position: relative;
    transform: none;
    width: 100%;
    z-index: 5
}

.search-form.search-form-permanent .input-group {
    padding: 0;
    top: 0
}

.search-form.search-form-permanent .button-search {
    color: #33f;
    outline: none
}

.search-form.search-form-permanent .button-search:hover {
    color: #b4c5cd
}

.search-form .input-group {
    padding: 0 10px;
    position: relative;
    top: 72px;
    width: 100%
}

.search-form .form-control {
    background: #fff;
    border: none;
    border-bottom: 1px #b4c5cd solid;
    border-radius: 0;
    box-shadow: none;
    float: left;
    height: auto;
    padding: 0;
    outline: none;
    width: calc(100% - 36px) !important
}

.search-form .form-control:focus {
    background: #fff !important
}

.search-form .button-search {
    background: transparent;
    border: none;
    float: right;
    font-size: 1.4em;
    padding: 6px 0 0 0;
    width: 36px
     background: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-search-strong-128.png') 0 0 no-repeat;
}

jQuery:

$('.search').on('click', function () {
 $('.search-form').addClass('search-visible');
});

【讨论】:

  • 这只是你的例子。因此,请检查您和我的​​代码之间的差异。在我的示例中,您必须单击“S”才能向下滑动搜索栏。如果有效,请接受答案;)
  • 您可以将此 addClass 更改为 toggleClass。然后您可以在点击时隐藏和显示此栏。
  • 谢谢它有效。如果您单击搜索栏上的其他任何位置,我是否也可以隐藏搜索栏?
  • 是的,可以这样做。我很快就会改变这个 jQuery 代码。
  • 你编辑过 jQuery 代码吗?我仍然无法通过单击搜索栏以外的任何位置来隐藏搜索栏。