首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不破坏其元素的情况下创建固定的顶部部分?

如何在不破坏其元素的情况下创建固定的顶部部分?
EN

Stack Overflow用户
提问于 2020-09-10 12:52:50
回答 3查看 63关注 0票数 3

我已经将position: fixed;添加到放置菜单和徽标的顶部部分。它确实在向下滚动的同时继续显示菜单,但它也打乱了徽标和菜单的顺序和空间。有没有办法克服这个问题?或者是在向下滚动时保留菜单的另一种方法?

The section that I want to keep while scrolling down

The unwanted result

代码语言:javascript
复制
html {
  scroll-behavior: smooth;
}

body {
  background: #fafafa;
}

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 3fr 3fr 3fr 2fr;
  grid-template-areas: "logo . menu menu" "hi hi hi hi" "what what what what" "projects projects projects projects""contact contact contact contact";
}

.logo {
  background: #fafafa;
  grid-area: logo;
  display: flex;
  justify-content: center;
  justify-items: center;
  position: fixed;
}

.logo img {
  object-fit: contain;
}

.menu {
  display: flex;
  grid-area: menu;
  background-color: #fafafa;
  text-align: center;
  align-items: center;
  justify-content: center;
  position: fixed;
}

.menu a {
  position: relative;
  cursor: pointer;
  font-size: 20px;
  font-family: 'Roboto', sans-serif;
  text-decoration: none;
  color: black;
}

.menu a+a {
  margin-left: 1rem;
}

.menu a::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  transform: scaleX(0);
  transition: transform 0.4s ease;
  transform-origin: bottom right;
  background-color: #ff9c00;
}

.menu a:hover::after {
  transform-origin: bottom left;
  transform: scaleX(1);
}

.hi {
  grid-area: hi;
  background-color: #c2edda;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.hi img {
  max-width: 100%;
  width: 37em;
  animation: fadein 2s;
  padding: 150px 150px 150px 50px;
}

@keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@media screen and (max-width: 1080px) {
  .hi {
    justify-content: flex-end;
  }
  .hi p {
    font-size: 20px !important;
  }
}

@media screen and (max-width: 870px) {
  .hi img {
    display: none;
  }
  .hi {
    justify-content: center;
  }
}

.hi p {
  font-size: 35px;
  text-align: center;
  font-family: 'Roboto';
}

.what {
  padding: 50px 50px 50px 50px;
  font-family: 'Roboto';
  grid-area: what;
  background-color: #fafafa;
  display: flex;
  justify-items: center;
  justify-content: center;
}

.what img {
  object-fit: contain;
  width: 15%;
  height: auto;
  position: absolute;
  margin-top: 100px;
}

.projects {
  font-family: 'Roboto';
  display: flex;
  justify-content: center;
  padding: 50px 50px;
  grid-area: projects;
  background-color: khaki;
}

.pContainer {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: 1px 1px;
  grid-template-areas: ". headline headline ." "p1 p2 p3 p4" ". . . .";
  justify-content: space-between;
}

.headline {
  grid-area: headline;
  display: flex;
  justify-content: center;
}

.p1 {
  grid-area: p1;
}

.p2 {
  grid-area: p2;
  display: flex;
  justify-content: center;
}

.p3 {
  grid-area: p3;
  display: flex;
  justify-content: center;
}

.p4 {
  grid-area: p4;
}

.contact {
  grid-area: contact;
  background-color: steelblue;
}
代码语言:javascript
复制
<div class="grid-container">
  <div class="logo"><img src="logo.png"></div>
  <div class="menu">
    <ul>
      <a href="#who" class="list-inline-item">Who</a>
      <a href="#skills" class="list-inline-item">What</a>
      <a href="#proj" class="list-inline-item">Projects</a>
      <a class="list-inline-item">Contact</a>
    </ul>
  </div>
  <div id="who" class="hi">
    <img src="photo.png" alt="photo">
    <p id="para">
      Hi! I'm Avichai, <br> I'm a 22 y/o junior web developer and designer. <br> My knowledge is based mainly <br> on my high school computer science extension, <br> my continuous self-learning and curisity.
    </p>
  </div>
  <div id="skills" class="what">
    <h1>Knowledge and Skills</h1>
    <img src="3logo.png">
  </div>

  <div id="proj" class="projects">


    <div class="pContainer">
      <div class="headline">
        <h1>Projects</h1>
      </div>
      <div class="p1"></div>
      <div class="p2">Lone Soldier Realty</div>
      <div class="p3">Freelance Graphic Design</div>
      <div class="p4"></div>
    </div>
    <div id="contact" class="contact">


    </div>

  </div>

GitHub Link to Website Code

非常感谢。:)

EN

回答 3

Stack Overflow用户

发布于 2020-09-10 14:40:28

对于这一点,position: sticky是很好的。

我所做的就是用display:flex将你的徽标和链接打包成1个div

然后我将它移出顶部的网格区域,并将其设置为position: sticky: top: 0;

这意味着你的元素将是第一个相对的。如果元素到达top: 0,它将坚持使用它

这里需要注意的是:通常我们在顶部设置重置页边距、填充和框大小。

像这样:

代码语言:javascript
复制
html {
   padding: 0;
   margin: 0;
   box-sizing: border-box;
}

代码语言:javascript
复制
html {
  scroll-behavior: smooth;
}

body {
  background: #fafafa;
  margin: 0;
  padding: 0;
  box-sizing:border-box;
}

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 0fr 3fr 3fr 3fr 2fr;
  grid-template-areas: "logo . menu menu" "hi hi hi hi" "what what what what" "projects projects projects projects""contact contact contact contact";
}

.logo {
  background: #fafafa;
  grid-area: logo;
  display: flex;
  justify-content: center;
  justify-items: center;
}

.logo img {
  object-fit: contain;
}

.menu {
  display: flex;
  grid-area: menu;
  background-color: #fafafa;
  text-align: center;
  align-items: center;
  justify-content: center;
}

.menu a {
  position: relative;
  cursor: pointer;
  font-size: 20px;
  font-family: 'Roboto', sans-serif;
  text-decoration: none;
  color: black;
}

.menu a+a {
  margin-left: 1rem;
}

.menu a::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  transform: scaleX(0);
  transition: transform 0.4s ease;
  transform-origin: bottom right;
  background-color: #ff9c00;
}

.menu a:hover::after {
  transform-origin: bottom left;
  transform: scaleX(1);
}

.hi {
  grid-area: hi;
  background-color: #c2edda;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.hi img {
  max-width: 100%;
  width: 37em;
  animation: fadein 2s;
  padding: 150px 150px 150px 50px;
}

@keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@media screen and (max-width: 1080px) {
  .hi {
    justify-content: flex-end;
  }
  .hi p {
    font-size: 20px !important;
  }
}

@media screen and (max-width: 870px) {
  .hi img {
    display: none;
  }
  .hi {
    justify-content: center;
  }
}

.hi p {
  font-size: 35px;
  text-align: center;
  font-family: 'Roboto';
}

.what {
  padding: 50px 50px 50px 50px;
  font-family: 'Roboto';
  grid-area: what;
  background-color: #fafafa;
  display: flex;
  justify-items: center;
  justify-content: center;
}

.what img {
  object-fit: contain;
  width: 15%;
  height: auto;
  position: absolute;
  margin-top: 100px;
}

.projects {
  font-family: 'Roboto';
  display: flex;
  justify-content: center;
  padding: 50px 50px;
  grid-area: projects;
  background-color: khaki;
}

.pContainer {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: 1px 1px;
  grid-template-areas: ". headline headline ." "p1 p2 p3 p4" ". . . .";
  justify-content: space-between;
}

.headline {
  grid-area: headline;
  display: flex;
  justify-content: center;
}

.p1 {
  grid-area: p1;
}

.p2 {
  grid-area: p2;
  display: flex;
  justify-content: center;
}

.p3 {
  grid-area: p3;
  display: flex;
  justify-content: center;
}

.p4 {
  grid-area: p4;
}

.contact {
  grid-area: contact;
  background-color: steelblue;
}

.stickybar {
   display: flex;
   align-items: center;
   justify-content: space-between;
   position: sticky;
   top: 0;
   background: #fafafa;
   padding: 0 10px;
   z-index: 1;
}
代码语言:javascript
复制
<div class="stickybar">
  <div class="logo"><img src="https://raw.githubusercontent.com/avihyb/avihyb.github.io/master/LOGO.png"></div>
     <div class="menu">
       <ul>
         <a href="#who" class="list-inline-item">Who</a>
         <a href="#skills" class="list-inline-item">What</a>
         <a href="#proj" class="list-inline-item">Projects</a>
         <a class="list-inline-item">Contact</a>
       </ul>
     </div>
  </div>
<div class="grid-container">

  <div id="who" class="hi">
    <img src="photo.png" alt="photo">
    <p id="para">
      Hi! I'm Avichai, <br> I'm a 22 y/o junior web developer and designer. <br> My knowledge is based mainly <br> on my high school computer science extension, <br> my continuous self-learning and curisity.
    </p>
  </div>
  <div id="skills" class="what">
    <h1>Knowledge and Skills</h1>
    <img src="3logo.png">
  </div>

  <div id="proj" class="projects">


    <div class="pContainer">
      <div class="headline">
        <h1>Projects</h1>
      </div>
      <div class="p1"></div>
      <div class="p2">Lone Soldier Realty</div>
      <div class="p3">Freelance Graphic Design</div>
      <div class="p4"></div>
    </div>
    <div id="contact" class="contact">


    </div>

  </div>

票数 2
EN

Stack Overflow用户

发布于 2020-09-10 13:20:04

希望这能对你有所帮助。

您必须将徽标和菜单都放在一个包装器中,因此在我们的情况下,标题是包装器的最佳匹配,然后添加固定在该包装器上的位置。

将固定位置放在标题包装上,而不是菜单和徽标上。

另外,我在标题上添加了显示伸缩,并使其之间的空间和对齐中心。

代码语言:javascript
复制
 html{
      scroll-behavior: smooth;
}
body{
      background: #fafafa;  
}
.grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr;
    grid-template-rows: 1fr 3fr 3fr 3fr 2fr;
        
    grid-template-areas: "logo . menu menu" "hi hi hi hi" "what what what what" "projects projects projects projects""contact contact contact contact" ;
  }
  
  .logo { 
        background: #fafafa;
        grid-area: logo; 

        }
  
        .logo img{

            object-fit: contain;
        }
header.header{
          position: fixed;
          left:0;
          right:0; 
              display: flex;
    justify-content: space-between;
    align-items: center;
    padding:10px;
       background:#fafafa;
    }
  .menu { 
          
          grid-area: menu; 
          background-color: #fafafa;
        

          
          
        }
        .menu a{
              position: relative;
              cursor: pointer;
              font-size:20px;
              font-family: 'Roboto', sans-serif;
              text-decoration: none;
              color: black;
            }

      .menu a + a{
            margin-left: 1rem;
      }
      .menu a::after{
            content:"";
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 2px;
            transform: scaleX(0);
            transition: transform 0.4s ease;
            transform-origin: bottom right;
            background-color: #ff9c00;

      }
  
      .menu a:hover::after{
            transform-origin: bottom left;
            transform: scaleX(1);
      }
  .hi { 
        grid-area: hi;
        background-color: #c2edda;
        height:100%;
        display: flex;
        justify-content: center;
        align-items: center;
        
      }

      .hi img{
            max-width: 100%;
            width: 37em;
            animation: fadein 2s;
            padding:150px 150px 150px 50px ;
      }
      @keyframes fadein {
            from { opacity: 0; }
            to   { opacity: 1; }
        }

      @media screen and (max-width: 1080px)
      {
            .hi{justify-content: flex-end;}
            .hi p{font-size: 20px !important;}
            
      }
      @media screen and (max-width: 870px)
      {
            .hi img{display: none;}
            .hi{justify-content: center;}
      }
  
      .hi p{
            font-size: 35px;
            text-align: center;
            font-family: 'Roboto';
      }
  .what {
      padding:50px 50px 50px 50px ;

         font-family: 'Roboto';
         grid-area: what;
         background-color: #fafafa;
         display: flex;
         justify-items: center;
         justify-content: center;
         
        }   
        .what img{
              object-fit: contain;
              width: 15%;
              height: auto;
              position: absolute;
              margin-top: 100px;
        }
  
        .projects{
            font-family: 'Roboto';
              display: flex;
              justify-content: center;
              padding: 50px 50px;
              grid-area: projects;
              background-color: khaki;
        }

        .pContainer{

            display: grid;
            grid-template-columns: 1fr 1fr 1fr 1fr;
            grid-template-rows: 1fr 1fr 1fr;
            gap: 1px 1px;
            grid-template-areas:
              ". headline headline ."
              "p1 p2 p3 p4"
              ". . . .";
              justify-content: space-between;
        }

      .headline { grid-area: headline;
      display: flex;
justify-content: center; }

      .p1 { grid-area: p1; }

      .p2 { grid-area: p2;
            display: flex;
            justify-content: center;  }

      .p3 { grid-area: p3;
            display: flex;
            justify-content: center;
             }

      .p4 { grid-area: p4; }


  .contact { 
         grid-area: contact;
         background-color: steelblue;
        }
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
  
  <script src="script.js"></script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <title>Avichai</title>
    <style>
      @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@100&display=swap');
      </style> 
      <link rel="icon" href="logo.png" >
         <link rel="stylesheet" href="/style.css">
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    
</head>
<body>
   <div class="grid-container">
   <header class="header">
  <div class="logo"><img src="https://raw.githubusercontent.com/avihyb/avihyb.github.io/master/LOGO.png" class="img-fluid" width="180" height="82"></div>
  <div class="menu">
    <ul>
      <a href="#who" class="list-inline-item">Who</a>
      <a href="#skills" class="list-inline-item">What</a>
      <a href="#proj" class="list-inline-item">Projects</a>
      <a class="list-inline-item">Contact</a>
    </ul>
  </div>
  </header>
  <div id="who" class="hi">
    <img src="photo.png" alt="photo" />
    <p id="para">
      Hi! I'm Avichai, <br>
      I'm a 22 y/o junior web developer and designer. <br>
      My knowledge is based mainly <br> on my high school computer science extension, <br>
      my continuous self-learning and curisity.
    </p>
  </div>
    <div id="skills" class="what"><h1>Knowledge and Skills</h1>
      <img src="3logo.png">
  </div>

  <div id="proj" class="projects">
    
    
    <div class="pContainer">
      <div class="headline"><h1>Projects</h1></div>
      <div class="p1"></div>
      <div class="p2">Lone Soldier Realty</div>
      <div class="p3">Freelance Graphic Design</div>
      <div class="p4"></div>
    </div>
  <div id="contact"class="contact">


  </div>

</div>

</body>
</html>

票数 1
EN

Stack Overflow用户

发布于 2020-09-10 13:52:09

首先,我在徽标和菜单中添加了一个div作为导航栏。

代码语言:javascript
复制
  <div class="navbar">
    <div class="logo"><img src="https://raw.githubusercontent.com/avihyb/avihyb.github.io/master/LOGO.png"></div>
    <div class="menu">
      <ul>
        <a href="#who" class="list-inline-item">Who</a>
        <a href="#skills" class="list-inline-item">What</a>
        <a href="#proj" class="list-inline-item">Projects</a>
        <a class="list-inline-item">Contact</a>
      </ul>
    </div>
  </div>

由于您修复了徽标和菜单,因此它只会更改为它们内部的固定导航栏。然后给导航栏添加背景色;

代码语言:javascript
复制
.navbar{
  display:flex;
  flex-wrap:wrap;
  background-color: #fafafa;
  position: fixed;
  top:0;
  left:0;
  right:0;
  width:100%;
  z-index:9999; // for images overlaying the navbar.
}

然后将width:20%;添加到徽标也是一个宽度

代码语言:javascript
复制
.logo {
  display: flex;
  justify-content: center;
  width:20%;
}

width:75%;margin-right:5%;到菜单

代码语言:javascript
复制
.menu {
  width:75%;
  margin-right:5%;
}

尝试演示

代码语言:javascript
复制
html {
  scroll-behavior: smooth;
}

body {
  background: #fafafa;
}

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 0fr 3fr 3fr 3fr 2fr;
  grid-template-areas: "logo . menu menu" "hi hi hi hi" "what what what what" "projects projects projects projects""contact contact contact contact";
}

.logo {
  background: #fafafa;
  grid-area: logo;
  display: flex;
  justify-content: center;
  align-items:center;
  width:25%;
}

.logo img {
  object-fit: contain;
}

.menu {
  display: flex;
  grid-area: menu;
  background-color: #fafafa;
  text-align: center;
  align-items: center;
  justify-content: center;
  height:104px;
  width:75%;

}
.navbar{
  display:flex;
  flex-wrap:wrap;
  background-color: #fafafa;
  position: fixed;
  top:0;
  left:0;
  right:0;
  width:100%;
  z-index:9999;
}
.menu a {
  position: relative;
  cursor: pointer;
  font-size: 20px;
  font-family: 'Roboto', sans-serif;
  text-decoration: none;
  color: black;
}

.menu a+a {
  margin-left: 1rem;
}

.menu a::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  transform: scaleX(0);
  transition: transform 0.4s ease;
  transform-origin: bottom right;
  background-color: #ff9c00;
}

.menu a:hover::after {
  transform-origin: bottom left;
  transform: scaleX(1);
}

.hi {
  grid-area: hi;
  background-color: #c2edda;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.hi img {
  max-width: 100%;
  width: 37em;
  animation: fadein 2s;
  padding: 150px 150px 150px 50px;
}

@keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@media screen and (max-width: 1080px) {
  .hi {
    justify-content: flex-end;
  }
  .hi p {
    font-size: 20px !important;
  }
}

@media screen and (max-width: 870px) {
  .hi img {
    display: none;
  }
  .hi {
    justify-content: center;
  }
}

.hi p {
  font-size: 35px;
  text-align: center;
  font-family: 'Roboto';
}

.what {
  padding: 50px 50px 50px 50px;
  font-family: 'Roboto';
  grid-area: what;
  background-color: #fafafa;
  display: flex;
  justify-items: center;
  justify-content: center;
}

.what img {
  object-fit: contain;
  width: 15%;
  height: auto;
  position: absolute;
  margin-top: 100px;
}

.projects {
  font-family: 'Roboto';
  display: flex;
  justify-content: center;
  padding: 50px 50px;
  grid-area: projects;
  background-color: khaki;
}

.pContainer {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: 1px 1px;
  grid-template-areas: ". headline headline ." "p1 p2 p3 p4" ". . . .";
  justify-content: space-between;
}

.headline {
  grid-area: headline;
  display: flex;
  justify-content: center;
}

.p1 {
  grid-area: p1;
}

.p2 {
  grid-area: p2;
  display: flex;
  justify-content: center;
}

.p3 {
  grid-area: p3;
  display: flex;
  justify-content: center;
}

.p4 {
  grid-area: p4;
}

.contact {
  grid-area: contact;
  background-color: steelblue;
}
代码语言:javascript
复制
<div class="grid-container">
  <div class="navbar">
    <div class="logo"><img src="https://raw.githubusercontent.com/avihyb/avihyb.github.io/master/LOGO.png"></div>
    <div class="menu">
      <ul>
        <a href="#who" class="list-inline-item">Who</a>
        <a href="#skills" class="list-inline-item">What</a>
        <a href="#proj" class="list-inline-item">Projects</a>
        <a class="list-inline-item">Contact</a>
      </ul>
    </div>
  </div>
  <div id="who" class="hi">
    <img src="https://raw.githubusercontent.com/avihyb/avihyb.github.io/master/photo.png" alt="photo">
    <p id="para">
      Hi! I'm Avichai, <br> I'm a 22 y/o junior web developer and designer. <br> My knowledge is based mainly <br> on my high school computer science extension, <br> my continuous self-learning and curisity.
    </p>
  </div>
  <div id="skills" class="what">
    <h1>Knowledge and Skills</h1>
    <img src="https://raw.githubusercontent.com/avihyb/avihyb.github.io/master/3logo.png">
  </div>

  <div id="proj" class="projects">


    <div class="pContainer">
      <div class="headline">
        <h1>Projects</h1>
      </div>
      <div class="p1"></div>
      <div class="p2">Lone Soldier Realty</div>
      <div class="p3">Freelance Graphic Design</div>
      <div class="p4"></div>
    </div>
    <div id="contact" class="contact">


    </div>

  </div>

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63823033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档