[英]Having trouble with a fixed element on scroll
I'm attempting to create a layout where there is a horizontal menu under the logo, and when the user scrolls past the logo the menu becomes fixed at the top. 我试图创建一种布局,其中徽标下方有一个水平菜单,并且当用户滚动经过徽标时,菜单固定在顶部。
$(window).scroll(function(e) {
$el = $('#menu');
if ($(this).scrollTop() > 100 && $el.css('position') != 'fixed') {
$('#menu').css({'position': 'fixed', 'top': '0px'});
$('#content-text').css({'margin-top': '50px'});
}
if ($(this).scrollTop() < 100 && $el.css('position') == 'fixed') {
$('#menu').css({'position': 'static', 'top': '100px'});
$('#content-text').css({'margin-top': '0px'});
}
});
In doing so I also have to change the margin-top
of the main content, otherwise it jumps. 这样做时,我还必须更改主要内容的页
margin-top
,否则它会跳。
Example: 例:
http://jsfiddle.net/7R89x/ http://jsfiddle.net/7R89x/
While the fixed menu appears to be working, when I change the margin on the content section it now overlaps the footer at the bottom of the page. 尽管固定菜单似乎可以正常工作,但是当我更改内容部分的边距时,它现在与页面底部的页脚重叠。 How can I fix this?
我怎样才能解决这个问题?
What is the reason you add that margin-top: 50px to content-text? 向内容文本添加margin-top:50px的原因是什么?
removing that just works fine. 删除就可以了。
$(window).scroll(function(e) {
$el = $('#menu');
if ($(this).scrollTop() > 100 && $el.css('position') != 'fixed') {
$('#menu').css({'position': 'fixed', 'top': '0px'});
}
if ($(this).scrollTop() < 100 && $el.css('position') == 'fixed') {
$('#menu').css({'position': 'static', 'top': '100px'});
}
});
I would do it this way. 我会这样做。 OR - one of the 2 ways below.
或 -以下2种方式之一。 If you know the heights for sure, you can just add and remove class.
如果您确实知道这些高度,则可以添加和删除类。 If you don't, you can just query them.
如果没有,您可以查询它们。 Here is a jsFiddle.
这是一个jsFiddle。
I think the real issue is that FF is strict, and your major blocks aren't really organized box-model wise. 我认为真正的问题是FF是严格的,并且您的主要模块并不是真正有条理的箱式模型。 If you float them all left and make them width 100% etc, they will stack nicely.
如果将它们全部浮动,并使其宽度为100%等,它们将很好地堆叠。 The whole table-cell thing is whack.
整个表格单元的内容都很糟糕。 You are better off with the consistency of floats.
浮点数的一致性会更好。
<header>
header
</header>
<nav>
<ul class="menu">
<li><a href="#">item</a></li>
<li><a href="#">item</a></li>
<li><a href="#">item</a></li>
</ul>
</nav>
<section>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi id dicta asperiores placeat tempore doloribus eius consequuntur vero libero fugiat quidem officia. Quae est temporibus non ad reiciendis excepturi doloribus!</p>
<ul>
<li>to show scroll</li>
<li>etc.</li>
</ul>
</section>
* {
box-sizing: border-box;
}
header, nav, section {
width: 100%;
float: left;
}
body {
margin: 0;
}
header {
background: red;
min-height: 100px;
}
nav {
background: lightblue;
}
.menu {
list-style: none;
margin: 0;
padding: 0;
}
section {
background: orange;
min-height: 1200px
}
.fixed-header nav {
position: fixed;
top: 0;
left: 0;
}
.fixed-header section {
background: lime;
/* you could set margin here, if you know the nav height for sure */
}
$(window).on('scroll', function() {
var navHeight = $('nav').outerHeight();
var headerHeight = $('header').outerHeight();
if ( $(this).scrollTop() > headerHeight ) {
$('body').addClass('fixed-header');
$('section').css({
'margin-top' : navHeight
});
} else if ( $(this).scrollTop() <= headerHeight ) {
$('body').removeClass('fixed-header');
$('section').css({
'margin-top' : 0
});
}
});
Everything you are doing is good, and the problem you are facing is because the height of the content-text
is 100
of the outer-div( content
) which is 100%. 你正在做的一切都是美好的,而你面临的问题是,因为高度
content-text
为100
外的div(的content
),这是100%。 But you are specifying a margin of let's say 5%
. 但是您指定的边距为
5%
。 Then it will cause an overflow 5%.. As you are getting. 然后,它将导致5%的溢出。
So my advise change the height as well.. Updated link: http://jsfiddle.net/7R89x/1/ 因此,我的建议也更改高度。。更新的链接: http : //jsfiddle.net/7R89x/1/
code: 码:
if ($(this).scrollTop() > 100 && $el.css('position') != 'fixed') {
$('#menu').css({'position': 'fixed', 'top': '0px'});
$('#content-text').css({'margin-top': '5%','height':'95%'});
}
if ($(this).scrollTop() < 100 && $el.css('position') == 'fixed') {
$('#menu').css({'position': 'static', 'top': '100px'});
$('#content-text').css({'margin-top': '0px','height':'100%'});
}
OR 要么
Just 只是
if ($(this).scrollTop() > 100 && $el.css('position') != 'fixed') {
$('#menu').css({'position': 'fixed', 'top': '0px'});
$('#content').css({'margin-top': '50px'});
}
if ($(this).scrollTop() < 100 && $el.css('position') == 'fixed') {
$('#menu').css({'position': 'static', 'top': '100px'});
$('#content').css({'margin-top': '0px'});
}
works flawlessly.. 完美地工作..
Updated: http://jsfiddle.net/7R89x/3/ 更新: http : //jsfiddle.net/7R89x/3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.