放置粘性奇怪的渲染

时间:2019-02-16 07:28:48

标签: css sticky

我有一个带有position:sticky的简单导航栏,滚动时滚动条显示不清晰,一切似乎都正常,那么它位于顶部。 When not scrolled When scrolled Close view of not scrolled Close of view of the same nav bar scrolled

从上图可以看出,似乎存在一些渲染问题。这是CSS:

body{
/*For demo purpose*/
height:300vh;
}
* {
    font-family: RobotoThin;
    margin: 0;
    padding: 0;
    overflow-x: hidden;
    outline: none;
    -webkit-transition: 1s;
    transition: 1s;
    box-sizing: border-box;
    background-repeat: no-repeat;
    transition: 0.5s !important;
   scroll-behavior: smooth;
}
div.window {

    display: -webkit-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    -webkit-justify-content: center;
    justify-content: center;
    box-sizing: border-box;
    padding: calc(2vh + 2vw);
    height: 100vh;
    width: 95vw;
    position: absolute;
    right: 0;
    top: 0;
    display: none;
    white-space: nowrap;
    display: flex;
    -ms-flex-wrap: wrap;
    -webkit-flex-wrap: wrap;
    flex-wrap: wrap;
    background-color: #ecf0f1;
    margin-left: 5vw;
}
#help{
    display:block;
    transition:0s !important;
}
div#help{
height:200vh;
    white-space: normal;
}
div#help  a{
    color:red;
    text-decoration: none;
}
div#nav {
  top: 0;
  position: sticky;
  display: flex;
  justify-content: space-between;
  color: black;
  background-color: white;
  padding-top: 1vmax;
  padding-bottom: 1vmax;
  font-size: 1.2vmax;
  flex-wrap: wrap;
  border-radius: 10em;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
}

div#nav>a {
  color: black;
}

div#nav>*:hover {
  color: gold;
  letter-spacing: 0.3vw;
}

div#nav> :first-child {
  margin-left: 1vw;
}

div#nav> :last-child {
  margin-right: 1vw;
}
<body>
<div class='windows' id='help'>
<div id='nav'> <a>General important info</a> <a>Desktop</a> <a>Math calculators</a> <a>Notes</a> <a>Battery</a> <a>Quotes</a> <a>Settings</a> <a>Passwords and Data</a> </div>
</div>
</body>
不要介意颜色。我使用一个插件,因此您将看到所有白色,而不是黑色。 奇怪的是,我无法使示例正常工作,因此我无法代表问题。希望您可以看一下我的代码,并找出可能导致此问题的原因。

1 个答案:

答案 0 :(得分:2)

反复摆弄之后,罪魁祸首是:

  overflow-x: hidden;

body {
  /*For demo purpose*/
  height: 300vh;
}

* {
  font-family: RobotoThin;
  margin: 0;
  padding: 0;
  /* overflow-x: hidden;     <-- The problem */
  outline: none;
  -webkit-transition: 1s;
  transition: 1s;
  box-sizing: border-box;
  background-repeat: no-repeat;
  transition: 0.5s !important;
  scroll-behavior: smooth;
}

div.window {
  display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  box-sizing: border-box;
  padding: calc(2vh + 2vw);
  height: 100vh;
  width: 95vw;
  position: absolute;
  right: 0;
  top: 0;
  display: none;
  white-space: nowrap;
  display: flex;
  -ms-flex-wrap: wrap;
  -webkit-flex-wrap: wrap;
  flex-wrap: wrap;
  background-color: #ecf0f1;
  margin-left: 5vw;
}

#help {
  display: block;
  transition: 0s !important;
}

div#help {
  height: 200vh;
  white-space: normal;
}

div#help a {
  color: red;
  text-decoration: none;
}

div#nav {
  top: 0;
  position: sticky;
  display: flex;
  justify-content: space-between;
  color: black;
  background-color: white;
  padding-top: 1vmax;
  padding-bottom: 1vmax;
  font-size: 1.2vmax;
  flex-wrap: wrap;
  border-radius: 10em;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
}

div#nav>a {
  color: black;
}

div#nav>*:hover {
  color: gold;
  letter-spacing: 0.3vw;
}

div#nav> :first-child {
  margin-left: 1vw;
}

div#nav> :last-child {
  margin-right: 1vw;
}
<body>
  <div class='windows' id='help'>
    <div id='nav'> <a>General important info</a> <a>Desktop</a> <a>Math calculators</a> <a>Notes</a> <a>Battery</a> <a>Quotes</a> <a>Settings</a> <a>Passwords and Data</a> </div>
  </div>
</body>