按屏幕位置动态居中Div内容

时间:2014-04-04 18:32:36

标签: dynamic coffeescript

当用户向下滚动时,我试图将Div的内容居中,通过始终位于顶部并始终为120px高的标题。我想使用vanilla javascript并没有找到办法做到这一点?

我还需要能够决定什么是居中,什么不是。

提前致谢。

1 个答案:

答案 0 :(得分:1)

你可以使用这样的东西。

var sections = $('.section');
$(window).scroll(function() {
    var currentPosition = $(this).scrollTop();
    sections.removeClass('selected').each(function() {
        var top = $(this).offset().top,
            bottom = top + $(this).height();
        if (currentPosition >= top && currentPosition <= bottom) {
            $(this).addClass('selected');
        }
    });
});

选择可以进行文本对齐中心等。

相关问题