简体   繁体   English

如何正确使导航栏固定在屏幕顶部?

[英]How to properly make navbar stay fixed at top of screen?

I am trying to make a navbar that stays at the top of the screen when you scroll down.我正在尝试制作一个导航栏,当您向下滚动时它会停留在屏幕顶部。

Here is how it currently looks:这是它目前的样子:

这是它目前的样子

Navbar.jsx导航栏.jsx

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import { BsFillPersonFill } from 'react-icons/bs';
import { FiMail } from 'react-icons/fi';
import { FaPlus, FaSearch } from 'react-icons/fa';
import { AiTwotoneBell } from 'react-icons/ai';
import './navbar.css';

function Navbar() {
  return (
    <section className="search-bar">
      <div className="row">
        <div className="col-lg mx-auto"> 
          <form>
            <div>
              <div className="input-group">
                <div className="homeBtn">
                <h3>VIZZEY</h3> 
                </div> 
              
                <input type="search" placeholder="Search" className="form-control" />
                <button className="searchBtn">
                  
                  <FaSearch />

                  </button>
                <div className="input-group-append buttons">
                
                  
                  <div className="icon">
                    <button className="icon-btn">
                      <h4><FiMail /></h4>
                    </button>
                  </div>
                  <div className="icon">
                    <button className="icon-btn">
                      <h4><FaPlus /></h4>
                    </button>
                  </div>
                  <div className="icon">
                    <button className="icon-btn">
                      <h4><AiTwotoneBell /></h4>
                    </button>
                  </div>
                  <div className="icon">
                    <button className="icon-btn">
                      <h4><BsFillPersonFill /></h4>
                    </button> 
                  </div>
                </div>
              </div>
            </div>
          </form>
        </div>
      </div>
    </section>  
  )
}
export default Navbar;

navbar.css导航栏.css

.searchBtn {
    color: #fff;
    background-color: #00ce7f;
    height: 38px; 
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px; 
    width: 40px;   
    border: none;  
}    

form {
    padding-top: 10px;
}

.icon {
    padding-right: 20px;
} 

.form-control {
background-color: rgb(255, 255, 255); 
border-color: rgb(133, 133, 133);  
border-top-left-radius: 5px !important;
border-bottom-left-radius: 5px !important; 
} 

.form-control:focus-within {
    background-color: rgb(255, 255, 255); 
    border-color: rgb(133, 133, 133); 
    outline: none;  
 
}
.profiledropdown-item {
width: 200px;
height: 100px;  
margin-left: 20px;
border: 3px solid rgb(255, 0, 0);
background-color: #ff0000; 
display: flex;
} 


.search-bar {
    width: auto; 
    border: solid  #333333; 
    background-color: #333333; 
    padding-bottom: 10px;   
    position: fixed;
}

[type="search"]::-webkit-search-cancel-button,
[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
  appearance: none;
}

.icon-btn {
    height: 40px; 
    width: 40px; 
    border-radius: 5px; 
    background-color: #00ce7f;
    color: white; 
    outline-color: chocolate; 
    border: none;
}

.homeBtn {
    padding-right: 60px; 
    padding-left: 50px; 
    color: #00ce7f;  
 
} 

.buttons {
    padding-left: 55px; 
    padding-right: 15px;
}
 
button:focus, button:active {
    outline: none !important;
    box-shadow: none !important; 
    background-color: rgb(111, 0, 255);
  }

I tried to simply add "position: fixed;"我试图简单地添加“位置:固定;” to the "search-bar" class, but when I do that, it looks like this:到“搜索栏”class,但是当我这样做时,它看起来像这样:

看起来像这样

Can somebody tell me how make it look how it does in the first picture, but just remain at the top of the screen without shrinking and without the boxes getting in the way?有人可以告诉我如何让它看起来像第一张图片中的样子,但只是保持在屏幕顶部而不缩小并且没有框妨碍?

In addition to position: fixed , add z-index: 9000 or similar to the bar so it stays on top of everything else.除了position: fixed之外,添加z-index: 9000或类似条形,使其位于其他所有内容之上。

You can add a CSS property to the main class/id of the Navbar position: fixed;您可以将 CSS 属性添加到导航栏 position 的主类/ID position: fixed;

Applying this property, your navbar will be fixed on its position it means if a user is scrolling still the navbar will be sticky at its position.应用此属性,您的导航栏将固定在其 position 上,这意味着如果用户仍在滚动,导航栏将在其 position 处保持粘性。

Do these step: Add position: fixed;执行以下步骤:添加position: fixed; and z-index: 1000; z-index: 1000;

by applying z-index: 1000;通过应用z-index: 1000; your navbar will be on top of all the elements.您的导航栏将位于所有元素之上。

Did you try adding a higher z-index value to your search-bar?您是否尝试向搜索栏添加更高的 z-index 值?

Eg z-index: 99;例如z-index: 99;

Read this: css-tricks.com/almanac/properties/z/z-index阅读: css-tricks.com/almanac/properties/z/z-index

Maybe you are looking for也许你正在寻找

position: sticky; //will remain in the flow, and stick while scrolling down.  
top:0; // will stick to top

or if you are using position fixed, also add width and z-index或者如果您使用 position 固定,还添加宽度和 z-index

position: fixed;
z-index:9000;
width: 100%;

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

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