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

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

在此处输入图像描述

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

有什么想法吗?

我不知道你想从哪里获取日期,所以,只是一个例子.. 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 }); ​ 

右侧的Div可以具有固定位置,或者您可以使用scrollresize事件更新其在块中的绝对位置。