滚动页面时修复标题转换

Gue*_*dio 3 html javascript css jquery

我不能为了这个人的生活而这样做.有谁知道这个滚动效果是如何在这个网站上创建的?- http://blindbarber.com/news

我正在开发一个项目,这个效果非常有用,这样滚动时我的固定导航就不会太大.

提前致谢.

Viv*_*dra 12

标题在css上保持在顶部position:fixed..要么你可以设置标题css - position:fixed从开始直接或者在position:fixed开始滚动页面时将其更改为...并且在标题滚动时将标题更新为要保留的内容. .

// css
.container {
  height: 2000px;
  width: 100%;
  background-color: yellow;
}

.header {
  text-align: center;
  background-color: red;
  height: 100px;
  min-height: 50px;
  width: 100%;
}

// js

window.onscroll= function () {
  var top = window.pageXOffset ? window.pageXOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
  var header = document.getElementById("header");
  if (top > 50){
    header.style.position = "fixed";
    header.style.height = "50px";
  } else {
    header.style.position = "relative";
    header.style.height = "100px";
  }
}

//html
<div class="container">
  <div id="header" class="header">
    Hello World
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

在这里演示