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

发布于 2025-01-08 01:06:48 字数 261 浏览 2 评论 0原文

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

在此处输入图像描述

当您向下滚动页面时,日期(或其他信息)会发生变化基于你旁边的div。它还必须在博客设置中工作,这意味着每个 div 可能有不同的高度。

有什么想法吗?

I am building a blog page in Wordpress and adding a sidebar that points to the current post. I'd like to fill that sidebar with the date of the current post using jQuery. It's only an idea so I don't have any code. But it would function like this:

enter image description here

As you scroll down the page the date (or other info) would change based on which div you were next to. It also has to work in a blog setting meaning each div can potentially be a different height.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2025-01-15 01:06:48

我不知道你想从哪里获取日期,所以,只是一个例子.. http://jsfiddle.net/ Nsubt/ Div 可以有固定位置,或者您可以使用 scrollresize 事件更新其在块中的绝对位置。

$(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
});
​

右侧的

I do not know where you want to get the date from, so, just an example.. 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 on the right could have a fixed position or you can update its absolute position in the block working with scroll and resize events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文