所有页面内容(红色"测试" 在我的图片中)应该置于最高100%(一个应该开始滚动查看)
顶部标题(在我的图像中标记为黑色)应固定在页面顶部 一旦内容点击标题,就应该开始滚动其余的内容。
(我不知道从哪里开始解决这个问题......可以 没有JavaScript 吗?)
提前感谢大家的帮助。
答案 0 :(得分:2)
我提出了一个很好的技巧几条JS线:
<强> jsBin live demo 强>
var heaF = document.getElementById("headerFixed");
var heaC = document.getElementById("headerClone");
heaC.innerHTML = heaF.innerHTML; // Clone header content (SEO wise)
function hero(){
var t = heaC.getBoundingClientRect().top; // Keep track of the clone top pos.
heaF.style.opacity = t<0 ? 0 : 1; // Toggle Org header opacity
heaC.style.opacity = t<0 ? 1 : 0; // Toggle Clone header opacity
}
hero(); // Do it when DOM is read
window.onscroll = hero; // Do it on scroll
window.onresize = hero; // But also on rresize
逻辑:
+-- +----------------+-- (Viewport Top)
| | headerFixed | opacity:1; 0 when the Clone hits Viewp.Top
h +----------------+
e | |
r | |
o +----------------+
| # headerClone | opacity:0; 1 when it hits Viewp.Top
+-- +----------------+-- (Viewport Bottom)
| Content... |
⋮ ⋮
HTML:
<div id="hero">
<div id="headerFixed"><h1>Header</h1></div>
<div id="headerClone"></div>
</div>
<div id="content">Website template and content here</div>
CSS:
html, body { height:100%; }
#hero{ // This guy contains header and the JS cloned header
height:100%; // In order to cover the whole viewport height
}
#headerFixed {
position: fixed; // Will be always fixed!
width: 100%;
}
#headerClone{
position:absolute;
bottom:0; // Place it at the bottom
opacity:0; // (Uncomment this line to see it in action)
width:100%;
}
提示:如果上述内容不清楚,请为我们的所有元素添加不同的背景颜色。你应该更好地看看会发生什么。