Vertically expanding search box HTML CSS.

huangapple go评论68阅读模式
英文:

Vertically expanding search box html css

问题

我正在尝试为导航栏创建一个可扩展的搜索框。当用户点击搜索框时,它应该展开,同时显示另一个具有不同名称输入的输入框。
我想要有两个这样的搜索框,一个在另一个下面。

这是我当前的实现:

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>

<nav class="navbar navbar-expand-lg py-2 fixed-top">
    <div class="container-fluid">
        <a class="navbar-brand" href="">
            <h7 id="nick"><strong class="d-none d-lg-inline">myLogo</strong></h7>
        </a>
        <form class="my-auto w-75" method="get" action="">
            <div class="input-group">
                <input type="text" class="form-control border border-right-0 ui-autocomplete-input" id="search"
                       name="search" placeholder="Cerca..." autocomplete="off">
                <span class="input-group-append">
                    <button class="btn btn-outline-light border border-left-0 p-3"
                            style="border-bottom-left-radius: 0%; border-top-left-radius: 0%; background-color: #f18918;"
                            type="submit">
                        Cerca
                    </button>
                </span>
            </div>
        </form>
        <a class="navbar-icon" data-bs-toggle="offcanvas" href="#sidebar" role="button" aria-controls="sidebar">
            <i class="bi bi-person-circle"></i>
        </a>
    </div>
</nav>

有任何想法吗?

英文:

I am trying to make an expandible search box for a navbar. When the user clicks on the searchbox this should expand showing also another input box with a different name input.
I would like to have such two searchbox one under the other.

This is my current implementation:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;/&gt;

&lt;nav class=&quot;navbar navbar-expand-lg py-2 fixed-top&quot;&gt;
      &lt;div class=&quot;container-fluid&quot;&gt;
          &lt;a class=&quot;navbar-brand&quot; href=&quot;&quot;&gt;
              &lt;h7 id=&quot;nick&quot;&gt;&lt;strong class=&quot;d-none d-lg-inline&quot;&gt;myLogo&lt;/strong&gt;&lt;/h7&gt;&lt;/a&gt;
              &lt;form class=&quot;my-auto w-75&quot; method=&quot;get&quot; action=&quot;&quot;&gt;
                  &lt;div class=&quot;input-group&quot;&gt;
                      &lt;input type=&quot;text&quot; class=&quot;form-control border border-right-0 ui-autocomplete-input&quot; id=&quot;search&quot;
                          name=&quot;search&quot; placeholder=&quot;Cerca...&quot; autocomplete=&quot;off&quot;&gt;
                      &lt;span class=&quot;input-group-append&quot;&gt;
                          &lt;button class=&quot;btn btn-outline-light border border-left-0 p-3&quot; style=&quot;border-bottom-left-radius: 0%; border-top-left-radius: 0%; background-color: #f18918;&quot; type=&quot;submit&quot;&gt;
                              Cerca
                          &lt;/button&gt;
                      &lt;/span&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
              &lt;a class=&quot;navbar-icon&quot; data-bs-toggle=&quot;offcanvas&quot; href=&quot;#sidebar&quot; role=&quot;button&quot; aria-controls=&quot;sidebar&quot;&gt;&lt;i class=&quot;bi bi-person-circle&quot;&gt;&lt;/i&gt;&lt;/a&gt;
      &lt;/div&gt;
  &lt;/nav&gt;

<!-- end snippet -->

Any idea?

答案1

得分: 1

我创建了另一个搜索字段,复制了第一个,并在第一个搜索字段上添加了JavaScript的点击事件,以显示第二个输入字段。这些代码非常基础且自解释。如果需要,我认为您可以自行进行其他调整。如果您仍有任何问题,请随时提问。

英文:

I created another search field copying the first one and added a click event on the first search field with JavaScript to display the 2nd input field. The codes are very basic and self explanatory. I think you can do the other adjustment by yourself if necessary. If you still have any question feel free to ask.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

window.addEventListener(&#39;click&#39;, function() {
  document.querySelector(&#39;#search&#39;).style.height = &quot;100%&quot;;
  document.querySelector(&#39;#search2&#39;).style.display = &quot;none&quot;;
})

document.querySelector(&#39;#search&#39;).addEventListener(&#39;click&#39;, function(event) {
  event.stopPropagation();
  this.style.height = &quot;50%&quot;; // Decreasing the height of the input for showing 2 one below another
  document.querySelector(&#39;#search2&#39;).style.display = &quot;block&quot;; // Showing the 2nd input field on click
})

document.querySelector(&#39;#search2&#39;).addEventListener(&#39;click&#39;, function(event) {
  event.stopPropagation();
})

document.querySelector(&#39;.input-group-append&#39;).addEventListener(&#39;click&#39;, function(event) {
  event.stopPropagation();
})

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;

&lt;nav class=&quot;navbar navbar-expand-lg py-2 fixed-top&quot;&gt;
  &lt;div class=&quot;container-fluid&quot;&gt;
    &lt;a class=&quot;navbar-brand&quot; href=&quot;&quot;&gt;
      &lt;h7 id=&quot;nick&quot;&gt;&lt;strong class=&quot;d-none d-lg-inline&quot;&gt;myLogo&lt;/strong&gt;&lt;/h7&gt;
    &lt;/a&gt;

    &lt;form class=&quot;my-auto w-75&quot; method=&quot;get&quot; action=&quot;&quot;&gt;
      &lt;div class=&quot;input-group&quot;&gt;
        &lt;div style=&quot;flex: 1;&quot;&gt;
          &lt;input type=&quot;text&quot; class=&quot;form-control border border-right-0 ui-autocomplete-input&quot; id=&quot;search&quot; name=&quot;search&quot; placeholder=&quot;Cerca...&quot; autocomplete=&quot;off&quot; style=&quot;height: 100%;&quot;&gt;

          &lt;input type=&quot;text&quot; class=&quot;form-control border border-right-0 ui-autocomplete-input&quot; id=&quot;search2&quot; name=&quot;search2&quot; placeholder=&quot;Cerca2...&quot; autocomplete=&quot;off&quot; style=&quot;height: 50%; display: none;&quot;&gt;
        &lt;/div&gt;

        &lt;span class=&quot;input-group-append&quot;&gt;
            &lt;button class=&quot;btn btn-outline-light border border-left-0 p-3&quot; style=&quot;border-bottom-left-radius: 0%; border-top-left-radius: 0%; background-color: #f18918; height: 100%;&quot; type=&quot;submit&quot;&gt;
                Cerca
            &lt;/button&gt;
        &lt;/span&gt;
      &lt;/div&gt;
    &lt;/form&gt;

    &lt;a class=&quot;navbar-icon&quot; data-bs-toggle=&quot;offcanvas&quot; href=&quot;#sidebar&quot; role=&quot;button&quot; aria-controls=&quot;sidebar&quot;&gt;&lt;i class=&quot;bi bi-person-circle&quot;&gt;&lt;/i&gt;&lt;/a&gt;
  &lt;/div&gt;
&lt;/nav&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月3日 02:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定