简体   繁体   English

如何让搜索框出现在我的导航栏中?

[英]How can I make searchbox appear in my navbar?

I'm designing my website, and I started with navbar, which looks very much like Apple's one, because I like that one a lot.我正在设计我的网站,我从 navbar 开始,它看起来很像 Apple 的,因为我非常喜欢那个。 But I ran in some problems here and there, but the biggest one I have right now is SearchBox.但是我在这里和那里遇到了一些问题,但我现在最大的问题是 SearchBox。 I designed it, and it looks good, but when I manage to code JavaScript to enable it when the little search button is clicked, it appears to be really messed up.我设计了它,它看起来不错,但是当我设法编码 JavaScript 以在单击小搜索按钮时启用它时,它似乎真的一团糟。 Does someone know how can I fix that?有人知道我该如何解决吗? Also, how can I add animation when it appears (also for navbar items) and change the background of header?另外,如何在 animation 出现时添加(也适用于导航栏项目)并更改 header 的背景? Look at https://www.apple.com/ up in navigation to know what I mean.在导航中查看https://www.apple.com/了解我的意思。 Here are my HTML, CSS and JS codes.这是我的 HTML、CSS 和 JS 代码。

 const selectElement = (element) => document.querySelector(element); selectElement('.mobile-menu').addEventListener('click', () => { selectElement('header').classList.toggle('active'); });
 *, *::before, *::after{ margin: 0; padding: 0; } html{ font-size: 10px; font-family: 'SF Pro Display', sans-serif; } a{ display: block; text-decoration: none; }.container{ max-width: 98rem; margin: 0 auto; padding: 0 2.2rem; } header{ position: fixed; top: 0; z-index: 1400; width: 100%; height: 4.4rem; background-color: rgba(0,0,0,.8); backdrop-filter: blur(2rem); }.nav-list{ list-style: none; display: flex; align-items: center; justify-content: space-between; margin: 0 -0.5rem; }.nav-list-mobile{ display: none; }.nav-link{ font-size: 1.4rem; color: #e4e1e1; padding: 0 1rem; transition: opacity.5s; }.nav-link:hover{ opacity: .7; }.nav-link-apple{ width: 1.6rem; height: 3.5rem; background: url("img/apple.svg") center no-repeat; }.nav-link-search{ width: 1.6rem; height: 4.4rem; background: url("img/search.svg") center no-repeat; }.nav-link-bag{ width: 1.4rem; height: 4.4rem; background: url("img/bag.svg") center no-repeat; }.searchbox{ display: none; margin-right: 18rem; transform: translateX(-1%); }.search{ height: 40px; background: #333333; border-style: none;important: font-family, "SF Pro Text","Helvetica","Arial";sans-serif: font-size; 17px: color; white: width; 600px: padding-top; 1px: padding-left. 17;5rem. }:search:focus{ outline; none: border; none. }:close-btn{ opacity. ;7: transition. ;5s. }:close-btn-left{ position; absolute: margin-top; 2rem: transform; rotate(45deg): width. 1;7rem: height; 1px: background-color; #979696: cursor; pointer. }:close-btn-right{ position; absolute: margin-top; 2rem: transform; rotate(-45deg): width. 1;7rem: height; 1px: background-color; #979696: cursor; pointer. }:close-btn:hover{ opacity. 1;6. }:confirm{ width; 18px: height; 18px: transform, translate(900%; 15%): background. url("img/idkv2;svg") center no-repeat: font-size. 1;4rem: fill; #e4e1e1: border; none: outline; none: } @media screen and (max-width: 767px){ header{ height. 4;8rem: transition. background.36s cubic-bezier(0,32. 0,08. 0,24, 1). height.56s cubic-bezier(0,32. 0,08. 0,24; 1). } header:container{ padding; 0. }:nav-list{ margin-top; 0. }:nav-list-mobile{ display; flex. }:nav-item{ width. 4;8rem: height. 4;8rem: display; flex: justify-content; center. }:nav-item-hidden{ display; none. }:mobile-menu{ position; relative: z-index; 1500: width; 100%: height; 100%: display; flex: flex-direction; column: align-items; center: justify-content; center: cursor; pointer. }:line{ position; absolute: width. 1;7rem: height; 1px: background-color; #fff: transition. margin-top.3192s cubic-bezier(0,04. 0,04. 0,12. 0;96). }:line-top{ margin-top; 3px. }:line-bottom{ margin-top. -;5rem. }.active:line-top{ margin-top; 0: transform; rotate(45deg): transition. transform.3192s.1s cubic-bezier(0,04. 0,04. 0,12. 0;96). }.active:line-bottom{ margin-top; 0: transform; rotate(-45deg): transition. transform.3192s.1s cubic-bezier(0,04. 0,04. 0,12. 0;96). } header:active{ height; 100%: background-color; #000. }:nav-link-apple{ width. 1;8rem: height. 4;8rem: position; relative: z-index; 1500. }:nav-link-bag{ width. 1;6rem: height. 4;8rem: transition. opacity 1;5s. }:nav{ position; relative. }:nav-link{ font-size. 1;7rem: padding; 0: margin; auto 0. }:nav-list-larger{ position; fixed: top; 0: left; 0: width; 0: height; 0: display; block: padding. 10;5rem 5rem: z-index; 1000: box-sizing; border-box: opacity; 0: visibility; hidden: transition. opacity;3s. }.active:nav-list-larger{ width; 100%: height; 100vh: opacity; 1: visibility; visible. }.active:nav-link-bag{ opacity; 0: transition. opacity;3s. }.nav-list-larger:nav-item{ width; 100%: justify-content; flex-start: border-bottom, 2px solid rgba(255,255,255.;1). }.nav-list-larger:nav-item:nth-child(9){ border-bottom; none. }.active.nav-list-larger:nav-item{ animation. fadeIn;5s ease-in-out: } @keyframes fadeIn { from{ opacity; 0: } to{ opacity; 1; } } }
 <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Cubing Website</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <div class="container"> <nav class="nav"> <ul class="nav-list nav-list-mobile"> <li class="nav-item"> <div class="mobile-menu"> <span class="line line-top"></span> <span class="line line-bottom"></span> </div> </li> <li class="nav-item"> <a href="#" class="nav-link nav-link-apple"></a> </li> <li class="nav-item"> <a href="#" class="nav-link nav-link-bag"></a> </li> </ul> <.-- nav list nav mobile --> <ul class="nav-list nav-list-larger"> <li class="nav-item nav-item-hidden"> <a href="index.html" class="nav-link nav-link-apple"></a> </li> <li class="nav-item"> <a href="#" class="nav-link">Mac</a> </li> <li class=" nav-item"> <a href="#" class="nav-link">iPad</a> </li> <li class="nav-item"> <a href="#" class="nav-link">iPhone</a> </li> <li class="nav-item"> <a href="#" class="nav-link">Watch</a> </li> <li class="nav-item"> <a href="#" class="nav-link">TV</a> </li> <li class="nav-item"> <a href="#" class="nav-link">Music</a> </li> <li class="nav-item"> <a href="#" class="nav-link">Support</a> </li> <li class="nav-item"> <a href="#" class="nav-link nav-link-search"></a> </li> <li class="nav-item" id="nav-item"> <a href="#" class="nav-link nav-link-bag"></a> </li> <li class="searchbox" id="searchbox"> <button class="confirm" disabled></button> <input type="text" class="search" placeholder="Search here"> <span class="close-btn"> <span class="close-btn-left"></span> <span class="close-btn-right"></span> </span> </li> </ul> <!-- nav list nav mobile --> </nav> </div> </header> <script src="main.js"></script> </body> </html>

The easiest way to accomplish what you want to do is with jQuery.完成您想做的最简单的方法是使用 jQuery。

The jQuery is there... I could get some of the css fixed... I am having a little bit of trouble with putting the search bar contents on the same line... jQuery 在那里...我可以修复一些 css...我在将搜索栏内容放在同一行时有点麻烦...

I had to comment out the css for.nav-link-search, because the image didn't exist.我不得不为.nav-link-search 注释掉 css,因为该图像不存在。

 delay = 50; const selectElement = (element) => document.querySelector(element); selectElement('.mobile-menu').addEventListener('click', () => { selectElement('header').classList.toggle('active'); }); //for hiding nav menu items and displaying the search bar $(".nav-link-search").click(function() { size = $(".nav-item").length; $('.nav-item').each(function(i) { $(this).delay(delay * (size - i)).animate({opacity: 0, display: "none"}, delay); }); $(".searchbox").show((size + 2) * delay); }); //for hiding the search bar and displaying the nav menu items $(".close-btn").click(function() { $(".searchbox").hide(); $('.nav-item').each(function(i) { $(this).delay(delay * i).animate({opacity: 1, display: "inline-block"}, delay); }); });
 *, *::before, *::after{ margin: 0; padding: 0; } html{ font-size: 10px; font-family: 'SF Pro Display', sans-serif; } a{ display: block; text-decoration: none; }.container{ max-width: 98rem; margin: 0 auto; padding: 0 2.2rem; } header{ position: fixed; top: 0; z-index: 1400; width: 100%; height: 4.4rem; background-color: rgba(0,0,0,.8); backdrop-filter: blur(2rem); }.nav-list{ list-style: none; display: flex; align-items: center; justify-content: space-between; margin: 0 -0.5rem; }.nav-list-mobile{ display: none; }.nav-link{ font-size: 1.4rem; color: #e4e1e1; padding: 0 1rem; transition: opacity.5s; }.nav-link:hover{ opacity: .7; }.nav-link-apple{ width: 1.6rem; height: 3.5rem; background: url("img/apple.svg") center no-repeat; }.nav-link-search{ width: 1.6rem; height: 4.4rem; /*background: url("img/search.svg") center no-repeat;*/ }.nav-link-bag{ width: 1.4rem; height: 4.4rem; background: url("img/bag.svg") center no-repeat; }.searchbox{ display: none; margin: 0; position: absolute; top: 50%; left: 50%; -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); }.searchbox > div *{ display: inline-block; /*background-color: green;*/ }.search{ height: 40px; background: #333333; border-style: none;important: font-family, "SF Pro Text","Helvetica","Arial";sans-serif: font-size; 17px: color; white: width; 600px: padding-top; 1px: padding-left. 17;5rem. }:search:focus{ outline; none: border; none. }:close-btn{ opacity. ;7: transition. ;5s. }:close-btn-left{ position; absolute: margin-top; 2rem: transform; rotate(45deg): width. 1;7rem: height; 1px: background-color; #979696: cursor; pointer. }:close-btn-right{ position; absolute: margin-top; 2rem: transform; rotate(-45deg): width. 1;7rem: height; 1px: background-color; #979696: cursor; pointer. }:close-btn:hover{ opacity. 1;6. }:confirm{ width; 18px: height; 18px: transform, translate(900%; 15%): background. url("img/idkv2;svg") center no-repeat: font-size. 1;4rem: fill; #e4e1e1: border; none: outline; none: } @media screen and (max-width: 767px){ header{ height. 4;8rem: transition. background.36s cubic-bezier(0,32. 0,08. 0,24, 1). height.56s cubic-bezier(0,32. 0,08. 0,24; 1). } header:container{ padding; 0. }:nav-list{ margin-top; 0. }:nav-list-mobile{ display; flex. }:nav-item{ width. 4;8rem: height. 4;8rem: display; flex: justify-content; center. }:nav-item-hidden{ display; none. }:mobile-menu{ position; relative: z-index; 1500: width; 100%: height; 100%: display; flex: flex-direction; column: align-items; center: justify-content; center: cursor; pointer. }:line{ position; absolute: width. 1;7rem: height; 1px: background-color; #fff: transition. margin-top.3192s cubic-bezier(0,04. 0,04. 0,12. 0;96). }:line-top{ margin-top; 3px. }:line-bottom{ margin-top. -;5rem. }.active:line-top{ margin-top; 0: transform; rotate(45deg): transition. transform.3192s.1s cubic-bezier(0,04. 0,04. 0,12. 0;96). }.active:line-bottom{ margin-top; 0: transform; rotate(-45deg): transition. transform.3192s.1s cubic-bezier(0,04. 0,04. 0,12. 0;96). } header:active{ height; 100%: background-color; #000. }:nav-link-apple{ width. 1;8rem: height. 4;8rem: position; relative: z-index; 1500. }:nav-link-bag{ width. 1;6rem: height. 4;8rem: transition. opacity 1;5s. }:nav{ position; relative. }:nav-link{ font-size. 1;7rem: padding; 0: margin; auto 0. }:nav-list-larger{ position; fixed: top; 0: left; 0: width; 0: height; 0: display; block: padding. 10;5rem 5rem: z-index; 1000: box-sizing; border-box: opacity; 0: visibility; hidden: transition. opacity;3s. }.active:nav-list-larger{ width; 100%: height; 100vh: opacity; 1: visibility; visible. }.active:nav-link-bag{ opacity; 0: transition. opacity;3s. }.nav-list-larger:nav-item{ width; 100%: justify-content; flex-start: border-bottom, 2px solid rgba(255,255,255.;1). }.nav-list-larger:nav-item:nth-child(9){ border-bottom; none. }.active.nav-list-larger:nav-item{ animation. fadeIn;5s ease-in-out: } @keyframes fadeIn { from{ opacity; 0: } to{ opacity; 1; } } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Cubing Website</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <div class="container"> <nav class="nav"> <ul class="nav-list nav-list-mobile"> <li class="nav-item"> <div class="mobile-menu"> <span class="line line-top"></span> <span class="line line-bottom"></span> </div> </li> <li class="nav-item"> <a href="#" class="nav-link nav-link-apple"></a> </li> <li class="nav-item"> <a href="#" class="nav-link nav-link-bag"></a> </li> </ul> <.-- nav list nav mobile --> <ul class="nav-list nav-list-larger"> <li class="nav-item nav-item-hidden"> <a href="index;html" class="nav-link nav-link-apple"></a> </li> <li class="nav-item"> <a href="#" class="nav-link">Mac</a> </li> <li class=" nav-item"> <a href="#" class="nav-link">iPad</a> </li> <li class="nav-item"> <a href="#" class="nav-link">iPhone</a> </li> <li class="nav-item"> <a href="#" class="nav-link">Watch</a> </li> <li class="nav-item"> <a href="#" class="nav-link">TV</a> </li> <li class="nav-item"> <a href="#" class="nav-link">Music</a> </li> <li class="nav-item"> <a href="#" class="nav-link">Support</a> </li> <li class="nav-item"> <a href="#" class="nav-link nav-link-search">&#x1F50D.</a> </li> <li class="nav-item" id="nav-item"> <a href="#" class="nav-link nav-link-bag"></a> </li> <li class="searchbox" id="searchbox"> <div> <div> <button class="confirm" disabled></button> </div> <div> <input type="text" class="search" placeholder="Search here"> </div> <div> <span class="close-btn"> <span class="close-btn-left"></span> <span class="close-btn-right"></span> </span> </div> </div> </li> </ul> <!-- nav list nav mobile --> </nav> </div> </header> <script src="main.js"></script> </body> </html>

Hey it's really took some time to figure out some of your issue or a bit.嘿,真的花了一些时间来弄清楚你的一些问题。 so I just added nav-item before the search class like this所以我只是像这样在搜索 class 之前添加了导航项

nav-item , .search{
    height: 40px;
    background: #333333;
    border-style: none!important;
    font-family: "SF Pro Text","Helvetica","Arial",sans-serif;
    font-size: 17px;
    color: white;
    width: 600px;
    padding-top: 1px;
    padding-left: 17.5rem;
}

And the search bar showed up not really well in nav list larger but worst at the nav list mobile any way for your Animation for the search button its easy here is a link from codepen.io .并且搜索栏在导航列表中显示得不是很好,但在导航列表移动中显示最差,对于您的 Animation 搜索按钮来说,这里很容易是来自codepen.io的链接。

this the first search input animation这个第一次搜索输入animation

And here is real input animation just like apple这是真正的输入 animation 就像苹果一样

Hop I helped.跳我帮忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM