简体   繁体   English

使用 setIntervel() 偏移具有动态高度的固定页眉的页面上的内容

[英]Using setIntervel() to offset content on page with fixed header with dynamic height

My problem is very similar to this one except the thing that fixed element may change his height dynamically during application lifecycle (other data, viewport change, etc... ).我的问题与这个问题非常相似,除了固定元素可能会在应用程序生命周期(其他数据、视口更改等)期间动态改变其高度。

I'm using setInterval() function with 100ms interval to update offset of content element depending on header height.我正在使用setInterval()函数以 100ms 的间隔更新内容元素的偏移量,具体取决于标题高度。

jQuery(function($){
    setInterval(function(){
        $('article').css('padding-top', $('header').outerHeight());
    }, 100)
});

Here is jsfiddle for it ( change the width of the resulted page to see how it works ).这是它的jsfiddle更改结果页面的宽度以查看它是如何工作的)。

For user experience it looks just great, but I'm curious is there a better way?对于用户体验,它看起来很棒,但我很好奇有没有更好的方法?

What are the disadvantages of this approach?这种方法的缺点是什么?

The major disadvantage is that you consume CPU every 100ms.主要缺点是您每 100 毫秒消耗一次 CPU。 And it doesn't do anything most of the time.而且大多数时候它什么都不做。

There is a better way.有个更好的方法。 Just emit an event after the fixed element changes height and bind your css adjusment to it.只需在固定元素更改高度后发出一个事件并将您的 css 调整绑定到它。 Something like:就像是:

$(document).trigger('my_element_changed_height');

wherever the height changes and无论高度变化和

$(document).on('my_element_changed_height', function() {
    $('article').css('padding-top', $('header').outerHeight());
});

I suppose you can use jquery.ba-resize.js library.我想你可以使用 jquery.ba-resize.js 库。 Here is a link: http://benalman.com/projects/jquery-resize-plugin It allows you to use resize event on any DOM element.这是一个链接: http : //benalman.com/projects/jquery-resize-plugin它允许您在任何 DOM 元素上使用 resize 事件。 But if I'm not mistaken this library uses setTimeout functionality and I'm not sure that's better in performance.但如果我没记错的话,这个库使用了 setTimeout 功能,我不确定它的性能是否更好。

UPDATE: time goes and web evolve, position: sticky更新:随着时间的推移和网络的发展, 位置:粘性

header{
    position: sticky;
}

Old Answer:旧答案:

Here is another solution that comes to my head.这是我想到的另一个解决方案。 I was thinking how would be great have such position : fixed-relative :) (That fixed on viewport but doesn't desapear from normal flow) And here is an idea how to emulate this behaviour.我在想有这样的position : fixed-relative会有多好position : fixed-relative :) (固定在视口上,但不会从正常流中消失)这里有一个如何模拟这种行为的想法 Set header element position as relative.将标题元素位置设置为相对位置。

header{
    position: relative;
}

And add some listner to scroll event.并添加一些监听器来滚动事件。

jQuery(function($){
    $(window).scroll(function(){
        $('header').css('top',$(this).scrollTop() );
    });
});

It's much pretty than have infinity loop with setInterval or trigger some event across your application.它比具有 setInterval 的无限循环或在您的应用程序中触发某些事件要漂亮得多。

Unfortunately it will not work on most touch devices不幸的是它不适用于大多数触摸设备

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

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