【问题标题】:Search bar below search button - on click搜索按钮下方的搜索栏 - 点击
【发布时间】:2022-03-15 17:03:10
【问题描述】:

我正在使用 bootstrap 3 为 angular 4 应用程序开发网页。

尝试添加一个搜索按钮(右上角/左上角),单击该按钮将在下一行展开。 Refer top right corner search button here.

<div class="row">
 <div class="col-xs-3">
  <form id="demo-2" style="margin-left: 10px">
    <input type="search" placeholder="Search..">
  </form>
 </div>
</div>

css

input {
  outline: none;
}
input[type=search] {
  -webkit-appearance: textfield;
  -webkit-box-sizing: content-box;
  font-family: inherit;
  font-size: 100%;
}
input::-webkit-search-decoration,

input[type=search] {
  background: #ededed url(https://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png) no-repeat 9px center;
  border: solid 1px #ccc;
  padding: 7px 9px 7px 17px;
  width: 55px;

  border-radius: 1em;

  -webkit-transition: all .5s;
  -moz-transition: all .5s;
  transition: all .5s;
}
input[type=search]:focus {
  width: 170px;
  background-color: #fff;

  -webkit-box-shadow: 0 0 5px rgba(109,207,246,.5);
  -moz-box-shadow: 0 0 5px rgba(109,207,246,.5);
  box-shadow: 0 0 5px rgba(109,207,246,.5);
}

#demo-2 input[type=search] {
  width: 15px;
  padding-left: 10px;
  color: transparent;
  cursor: pointer;
}
#demo-2 input[type=search]:hover {
  background-color: #fff;
}
#demo-2 input[type=search]:focus {
  width: 200px;
  padding-left: 32px;
  color: #000;
  background-color: #fff;
  cursor: auto;
  color: black;
}
#demo-2 input:-moz-placeholder {
  color: transparent;
}
#demo-2 input::-webkit-input-placeholder {
  color: transparent;
}

这让我搜索栏在同一行展开。我试图获得相同的例子。但他们都在谈论扩大同一行。

注意:对于桌面视图,搜索栏和搜索按钮将位于同一行。 Similar to this。将根据移动视图进行更改。

提前致谢。欢迎提出建议。

【问题讨论】:

    标签: html css twitter-bootstrap twitter-bootstrap-3 responsive-design


    【解决方案1】:

    搜索按钮并没有真正在下一行展开,它会切换位于其下方的新元素。查看下面的示例以了解我的意思。

    选项 1(使用简单的 JQuery 代码)

    $(document).ready(function(){
        $(".toggle-search").click(function(){
            $(".search-input").toggle();
        });
    });
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
    
    
    <button class="toggle-search"> Search </button>
    <div class="container">
      <form class="row search-input" id="demo-2" style="margin-left: 10px">
        <input class="col-lg-12" type="search" placeholder="Search..">
      </form>
    </div>

    选项 2(不包括额外的 JQuery 代码,但仍需要 JQuery 库)

    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
    
    
    <button type="button" class="btn btn-info" data-toggle="collapse" data-target=".search-input"> Search </button>
    <div class="container collapse search-input">
      <form class="row" id="demo-2" style="margin-left: 10px">
        <input class="col-lg-12" type="search" placeholder="Search..">
      </form>
    </div>

    【讨论】:

    • 谢谢.. 但我想在没有 JQuery/Javascript 的情况下实现它。是否可以使用 Bootstrap/ng-module & angular4 来做到这一点!!
    • 这也可以使用Bootstrap(bootstrap.min.js)自带的javascript来实现,可以用吗?否则,搜索字段将根据屏幕大小进行切换,而不是点击。
    • 我在我的 Angular 应用程序中尝试了您的示例(在 HTML 中,脚本在 中)。不知道我错过了哪里,它只是在我点击搜索按钮之前向我显示 .. 是的 typescript/javascript 与 Angular 应用程序一起使用会更好。由于每个组件都有 .css、.html、.ts,我们不需要编辑 bootstrap.min.js 我相信..
    • 我知道我们不需要编辑 bootstrap.min.js。我的意思是,您是否可以添加使用此文件的属性,或者您希望它纯粹使用 CSS。
    • 哦酷。是的,使用“../node_modules/bootstrap/dist/css/bootstrap.min.css”会很棒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多