jQuery根据滚动位置更改div中的内容

use*_*032 5 jquery blogs

我正在Wordpress中构建一个博客页面并添加一个指向当前帖子的侧边栏.我想用jQuery填充当前帖子的日期.这只是一个想法,所以我没有任何代码.但它的功能如下:

在此输入图像描述

当您向下滚动页面时,日期(或其他信息)将根据您所在的div而改变.它还必须在博客环境中工作,这意味着每个div可能具有不同的高度.

有什么想法吗?

Che*_*ery 14

我不知道你想从哪里得到日期,所以,只是一个例子.. http://jsfiddle.net/Nsubt/

$(window).on("scroll resize", function(){
    var pos=$('#date').offset();
    $('.post').each(function(){
        if(pos.top >= $(this).offset().top && 
           pos.top < $(this).next().offset().top)
        {
            // any way you want to get the date
            $('#date').html($(this).html()); 
            return; //break the loop
        }
    });
});

$(document).ready(function(){
  $(window).trigger('scroll'); // init the value
});
?
Run Code Online (Sandbox Code Playgroud)

右边的Div可以有一个固定的位置,或者你可以在使用scrollresize事件的块中更新它的绝对位置.