简体   繁体   English

固定在副标题上后,如何防止内容跳跃?

[英]How to prevent content jump after adding position:fixed to subhead?

I'm currently working on my new portfolio you can see here: http://katharinakoeth.de/neu/ And there's already my problem. 我目前正在开发我的新作品集,您可以在这里查看: http : //katharinakoeth.de/neu/这已经是我的问题。 As you can see, I added some jquery action to my subheads (i'm really a beginner when it comes to javascript) to change its position from inherit to fixed/sticky .. but when the change happens, my content jumps up because of the sudden space. 如您所见,我在子标题中添加了一些jquery动作(当涉及javascript时,我真的是一个初学者),将其位置从继承更改为固定/粘滞..但是,当更改发生时,由于突然的空间。 » it's most obvious with the "people I enjoy working with" ... the first person suddenly disappears as soon as the subhead becomes sticky. »“与我一起工作的人”最明显……当副标题变粘时,第一个人突然消失。

Is there any way to either add extra space or prevent the jump in another way? 有什么方法可以增加额外的空间或以其他方式阻止跳转?

When you change those subheaders to position:fixed, they're removed from the flow of the document. 当您将这些子标题更改为position:fixed时,它们将从文档流中删除。 They have a margin-bottom: 75px which is also removed from the flow when that happens. 它们的底边距为:75px,这种情况也会从流中删除。

Try changing that to a margin-top:75px to the start of the blocks below each subheader; 尝试将其更改为margin-top:75px到每个子标题下面的块的开头; that won't "disappear" when the subheads change position values, so your spacing should be preserved. 当副标题更改位置值时不会“消失”,因此应保留您的间距。

FYI, your fix.js file could probably be refactored down to something like this: 仅供参考,您的fix.js文件可能可以重构为以下形式:

var $titles = $("header h2");
$(window).scroll(function(){

    var win_top = $(this).scrollTop();
    $titles.each(function(){
        var div_top1 = $(this).offset().top;
        if (win_top > div_top) $(this).addClass('stick')
        else $(this).removeClass('stick');
    });

});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM