当向下滚动时,如何设置标题以固定在屏幕上
How to set the header to fix on screen when scroll down?
这是演示站点:
http://www.kraphicstudio.com/LLR/index.html如果你向下滚动网站直到横幅消失,你会发现菜单栏
"关于赞助商的活动查询登记捐款"
会粘在屏幕上。
我试过
#menu {
position: fixed;
}
,但它只能固定元素,而不是固定特定条件下的元素。如何在演示站点中做效果?非常感谢。
现在习惯了现在如果你向下滚动,那么你的菜单就会像这样固定
var oritop = -100;
$(window).scroll(function() {
var scrollt = $(this).scrollTop();
var elm = $(".scrollTopWindow");
if(oritop < 0) {
oritop= elm.offset().top;
}
if(scrollt >= oritop) {
elm.css({"position": "fixed", "top": 0, "left": 0,"right":0});
}
else {
elm.css("position", "static");
}
});
body{height:2000px}
.scrollTopWindow {
background-color: red;
height: 40px;
color: white;
padding:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>Some Content here <br>
<div class = "scrollTopWindow">
Scroll top window and fix me !
</div>
源代码为
With Scroll你可以检查,当窗口达到一个特定的高度后,将fixed添加到你的header
$(window).scroll(function () {
var height = 30;
var selector = $('header');
if($(this).scrollTop() >= height){
selector .addClass('fixedHead');
}else{
selector .removeClass('fixedHead');
}
});
.fixedHead{
position: fixed;
width: 100%;
top: -30px;
left: 0;
}
我想这可能行得通
您可以查看下面的链接。
http://jsfiddle.net/elcreador419/xJXZr/ var navPos = $('#nav').offset().top;
$(window).scroll(function () {
var fixIT = $(this).scrollTop() >= navPos;
var setPos = fixIT ? 'fixed' : 'relative';
var logoSH = fixIT ? 'show' : 'hide';
$('#nav').css({
position: setPos
});
$('#mini-logo')[logoSH](300);
});
相关文章:
- CSS-如何定位内容数据标题
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 在PHP中使用javascript更改页面标题'if'
- 在不打开聊天屏幕的情况下制作Zopim-ding代理
- Brightcove获取/显示HTML中的当前视频标题和描述
- 使用图像数组列表的simplegallary.js显示适合桌面屏幕的图像
- React组件-设置页面标题
- jQuery动态更改标题
- HighCharts长标题文本在某些元素上重叠
- 导航栏没有调整到浏览器屏幕的大小
- 调整屏幕大小后不显示子菜单
- 将行添加到具有固定标题的HTML表中
- 如何在p5.js中管理多智力竞赛游戏的多个屏幕
- CKEditor v4:自制插件中对话框的动态标题
- jQuery:当屏幕大小改变时,如何更改默认图像和悬停图像
- 标题在滚动100%屏幕高度调整大小
- 当向下滚动时,如何设置标题以固定在屏幕上
- 计算粘性标题中的屏幕大小
- 参考资料 修改立体主义.js石墨标题文字,看不到屏幕上的数据
- 在小屏幕上隐藏/显示标题,但在大屏幕上保持Jquery/CSS