我有在页面的顶部的固定报头,和向下滚动,并示出了滑块之后,我有一个次级锚菜单。我怎样才能使这个锚菜单粘到头上。 二级菜单粘到标题
Q
二级菜单粘到标题
0
A
回答
2
我敢肯定这是你在找什么!
https://codepen.io/will0220/pen/QqreKY
基本上,你需要从屏幕上,这是通过检查元素上偏移减去窗口的滚动做的顶部检查中间内容导航栏的位置。然后,如果您向上或向下滚动,则系统会自动关闭 - 因此,如果您向下滚动,并且中间页面导航小于屏幕顶部固定标题的高度,请将其克隆并添加固定版本。克隆是要走的路,所以内容的长度不会改变。然后当我们向相反的方向滚动时,我们可以使用与之前相同的计算来检查原始页内导航(仍然存在)的位置。
这会处理在固定的导航栏切换:
var thisScroll = 0, lastScroll = 0;
$(window).scroll(function(){
thisScroll = $(window).scrollTop();
if($('nav').offset().top - thisScroll <= 50 && !$('#stuckNav').length && thisScroll > lastScroll){
var newNav = $('nav').clone();
newNav.attr('id', 'stuckNav');
$('#wrapper').append(newNav);
}
else if($('nav').offset().top - $(window).scrollTop() > 50 && thisScroll < lastScroll){
$('#stuckNav').remove();
}
lastScroll = thisScroll;
});
而CSS需要的东西是这样的:
#stuckNav{
position: fixed;
top: 50px;
z-index: 2;
}
nav{
position: relative;
width: 100%;
background: #FFF;
text-align: center;
padding: 10px 0;
}
+0
这是完美的 – rinkaslb
0
只需使用JavaScript的:https://codepen.io/claytron5000/pen/OxZKxB?editors=1010
var secondary = document.querySelector(".secondary");
var fromTop = secondary.offsetTop;
document.onscroll = function() {
if (!secondary.classList.contains("stuck") && window.pageYOffset >= fromTop) {
secondary.classList.add("stuck");
}
else if (secondary.classList.contains("stuck") && window.pageYOffset < fromTop){
secondary.classList.remove("stuck");
}
};
.container p {
max-width: 768px;
margin: auto;
}
.header, .secondary {
background: beige;
position: relative;
}
ul li {
display: inline-block;
background: lightgrey;
}
.stuck {
position: fixed;
top:0;
width: 100%;
}
<div id="container">
<div class="header">
<ul>
<li>here's</li>
<li>some</li>
<li>items</li>
</ul>
</div>
<p>Some content that will scroll away.</p>
<div class="secondary">
<ul>
<li>here's</li>
<li>different</li>
<li>items</li>
</ul>
</div>
<p>And a whole lotta ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
</div>
相关问题
- 1. 基于节点标题生成二级菜单
- 2. Javascript粘性菜单问题
- 3. 粘性菜单问题
- 4. 在asp.net中将菜单从二级扩展到三级MVC
- 5. 多级菜单的问题
- 6. 粘性菜单更改标志
- 7. 用mmenu创建一个二级菜单
- 8. Wordpress中可交换的二级菜单
- 9. css下拉菜单第二级消失
- 10. 作为二级菜单的主详细信息菜单
- 11. 垂直菜单子菜单飞出并粘到页首
- 12. 添加登录/注销到drupal菜单,这不是主菜单或二级菜单
- 13. 多级下拉导航 - 保持二级下拉菜单关闭
- 14. Sencha GXT网格 - 列标题菜单和第二列标题中的空白
- 15. 多级菜单
- 16. wordpress:粘性菜单没有粘住滚动,直到后来
- 17. responsive/bootstrap.css菜单标题
- 18. IE7:菜单上方标题
- 19. jquery下拉菜单粘贴
- 20. webkit粘页脚菜单
- 21. 第二个菜单中的typo3不同的标题
- 22. 更改扩展名顶级上下文菜单标题
- 23. 如何更改主菜单第一级标题?
- 24. WordPress的获取顶级菜单项目标题
- 25. 超级鱼菜单z-index问题
- 26. Drupal的层级菜单主题化
- 27. 第三级CSS多层菜单问题
- 28. 多级下拉菜单,下拉问题
- 29. IE7 CSS超级菜单问题
- 30. 多级下拉菜单问题与jQuery
你可以使用jQuery? – will
您是否尝试过使用[header {position:sticky}](https://developer.mozilla.org/en-US/docs/Web/CSS/position#CSS_4)? – styfle
是的,我可以使用jQuery。我从来没有使用位置粘 – rinkaslb