JQUERY:为什么不;向下滚动时,我的导航固定在顶部
JQUERY: why isn't my nav fixed to the top when scrolling down?
当你在页面上向下滚动100px时,我的导航栏会固定在顶部。然后当向上滚动时,它返回到其正常状态。
我已经设法使导航栏在滚动时变得固定和不固定,但它不在正确的位置。它将页面向下固定到左侧,而不是顶部并向右浮动。
这是我在jsfiddle上做的例子,供你看看。
http://jsfiddle.net/edL5F/20/
<div id="wrapper">
<div id="codeback">
</div>
<div id="container">
<div class="nav">
</div>
<div id="wrap">
</div>
</div><!-- END OF CONTAINER -->
</div>
body{
background-color:black;
}
#wrapper{
width:100%;
height:inherit;
}
#codeback{
width:100%;
height:100%;
background-image:url('capture.jpg');
background-repeat: no-repeat;
position:fixed;
left:0px;
top:0px;
z-index:-1;
}
#container{
width:100%;
float:right;
}
.nav{
margin-top:100px;
width:80%;
height:50px;
float:right;
background-color:blue;
position:relative;
}
#wrap{
float:right;
width:80%;
height:1500px;
background-color:white;
}
$(window).scroll(function (e) {
$el = $('.nav');
if ($(this).scrollTop() > 100 && $el.css('position') != 'fixed') {
$('.nav').css({ 'position': 'fixed', 'top': '0'});
}
if ($(this).scrollTop() < 100 && $el.css('position') == 'fixed') {
$('.nav').css({ 'position': 'relative'});
}
});
你可以用class。。。
$(window).scroll(function (e) {
$el = $('.nav');
if ($(this).scrollTop() > 100) {
$('.nav').addClass("fixedNav");
}else {
$('.nav').removeClass("fixedNav");
}
});
.fixedNav {
position:fixed;
margin:0;
width:100%;
}
小提琴在这里:http://jsfiddle.net/yss9hz0v/
float:right
在位置为fixed
时没有任何影响。因此,当您将元素设置为位置fixed
时,必须设置right:0px
。
最后,每次将元素位置从fixed
更改为relative
,再更改为0px
和100px
时,都必须更改margin-top
分别
尝试:
$(window).scroll(function (e) {
$el = $('.nav');
if ($(this).scrollTop() > 100 && $el.css('position') != 'fixed') {
$('.nav').css({ 'position': 'fixed', 'top': '0','right':'0','margin-top':'0px'});
}
if ($(this).scrollTop() < 100 && $el.css('position') == 'fixed') {
$('.nav').css({ 'position': 'relative','margin-top':'100px'});
}
});
演示
这是我的解决方案:
- 从
.nav
中删除margin-top
- 将
padding-top:100px
添加到#container
- 编辑jquery
Jquery:
$(window).scroll(function (e) {
$el = $('.nav');
if ($(this).scrollTop() > 100 && $el.css('position') != 'fixed') {
$('.nav').css({ 'position': 'fixed', 'top': '0','right':'0'});
}
if ($(this).scrollTop() < 100 && $el.css('position') == 'fixed') {
$('.nav').css({ 'position': 'relative','margin-top':'100px'});
}
});
这是一把小提琴。
说明:CCD_ 14是在CCD_。
边距到填充的变化是在不编辑任何css的情况下,当你向上滚动时,迫使条回到原来的位置。
因为.nav元素的css属性中有一个margin-top: 100px;
。。。
Fiddle:http://jsfiddle.net/edL5F/24/
相关文章:
- 在主页上时,我的整个导航都会突出显示
- 在我的图库(jQuery)中使用箭头导航时,更改URL中的哈希
- 使用 CSS 或 javascript/jQuery,我会使用哪种方法来使我的网站的导航栏看起来更 3d-ish
- 为什么我的导航按钮或按钮文本不会移动到中心
- 我将如何使我的引导导航栏“;崩溃”;
- 我可以'我似乎没有得到我的网站'移动导航开始工作
- 如何让我的导航栏位于每个HTML页面的顶部(避免复制相同的代码)
- 在我的玉石模板中添加导航栏
- 我的导航栏不粘了
- 我的顶级导航是'使用滑块后无法工作
- 我的导航选项卡正在删除元素
- 单击我的导航按钮时切换我的菜单
- 我的导航栏搞砸了
- 如果我滚动并刷新我的网页,我的导航标题会失去其属性.JQuery
- 为什么我的 2 个具有相同导航栏的网站页面显示不同
- 如何让我的移动菜单按钮显示和隐藏导航栏,以及在负空间中单击时隐藏
- 如何使用引导导航栏构建我的流星烈焰 UI
- 在我导航到的大多数页面上,这个脚本是什么
- iPhone设备黑色屏幕在我的jQuery手机Gap应用程序导航中闪烁
- 使用 WayPoints js 从我的导航栏打开外部 html 页面