简体   繁体   English

屏幕不是很大时向下滚动时位置固定

[英]Position fixed when scroll down hopping when the screen is not very big

I'm trying to make my header fixed when the user scroll down. 当用户向下滚动时,我正在尝试修复标题。 The problem is if the content on the page is not big enough the screen hopping. 问题是页面上的内容是否不够大,导致屏幕跳动。 Also when I scroll to the top again it change from fixed to normal very hard. 同样,当我再次滚动到顶部时,它很难从固定变为正常。 Can someone help me with this? 有人可以帮我弄这个吗?

window.addEventListener('scroll', function() {
if($(window).scrollTop() == 0) {
    $('.sticky-wrapper').removeClass('stickynow');
} else {
    $('.sticky-wrapper').addClass('stickynow');
}
});

I don't really understand what 'screen hopping' means, but i guess what happens is that when your header becomes position:fixed , it doesn't occupy space anymore so the content moves up to fill that space an thus, 'hopping'. 我不太了解“屏幕跳变”的含义,但我想会发生什么情况,就是当您的标题变为position:fixed ,它不再占用空间,因此content会向上移动以填充该空间,从而“跳变” 。

You should add margin-top to your content ( or whichever element is right after the header in HTML structure ) equal to the header's height. 您应该在内容中添加margin-top(或HTML结构中标头之后的任何元素),使其等于标头的高度。

See below 见下文

 window.addEventListener('scroll', function() { let header = $('.sticky-wrapper') if ($(window).scrollTop() == 0) { $(header).removeClass('stickynow'); $('section').css({ 'margin-top': 0 }) } else { $(header).addClass('stickynow'); $('section').css({ 'margin-top': $(header).height() }) } }); 
 header { width: 100%; height: 100px; background: red; } .stickynow { position: fixed; background: blue; top: 0; left: 0; } section { height: 1000px; width: 100%; background: green; } body, html { margin: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <header class="sticky-wrapper"></header> <section>Not 'Hopping'</section> 

OPTION B. Without javascript/jQuery 选项B.不使用javascript / jQuery

If you don't need to add a class or something else that involves javascript/jQuery, you can have the same result as above, but only with css 如果您不需要添加类或涉及javascript / jQuery的其他内容,则可以得到与上述相同的结果,但只能使用CSS

 header { position: fixed; background: blue; top: 0; left: 0; height:100px; width:100%; } section { height: 1000px; width: 100%; background: green; margin-top:100px; /*add this*/ } body, html { margin: 0; } 
 <header class="sticky-wrapper"></header> <section>Not 'Hopping'</section> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向下滚动jQuery时如何获取浏览器屏幕窗口的位置 - How to get the position of screen window of browser when scroll down jquery 当您在滚动上到达其原始位置时,将位置从固定到最底部切换到div的初始位置 - Switch position from fixed to the very bottom to initial of a div when you reach its original position on scroll 向下滚动时,jQuery滚动到固定位置 - Jquery Scroll to Fixed positioning when scroll down 向下滚动时固定导航 - Fixed Nav when you scroll down 滚动位置在底部时更改为固定位置 - Changing to fixed positioning when scroll position is at bottom 固定div向下滚动并在向上滚动时设置绝对值 - Fixed div on scroll down & Set absolute when scroll up 位置:固定到位置:静态,当水平滚动出现时? - Position: fixed to position:static when horizontal scroll shows up? 搜索栏似乎表现得好像它的 position 是粘性的,但我将它设置为固定但当我滚动时它仍然向下移动 - Search bar seems to be acting as if its position is sticky but I have it set to fixed yet it still moves down when I scroll 返回屏幕顶部时隐藏和显示固定导航检测 - Hide & reveal fixed navigation detect when back at very top of screen 当滚动位置到达屏幕中的元素位置时如何显示和转换? - How to display and transition when scroll position reaches element position in the screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM