gpt4 book ai didi

javascript - 根据滚动位置更改 div 内容

转载 作者:太空狗 更新时间:2023-10-29 16:01:42 26 4
gpt4 key购买 nike

我希望使用 this 得到一些帮助来自另一个 Stack Exchange 帖子的代码。以下是 JavaScript:

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

$(document).ready(function(){
$(window).trigger('scroll'); // init the value
});

我已经在我自己的网站上实现了它:http://peterwoyzbun.com/newscroll/scroll.html .当滚动位置到达某个点时,框中的信息发生变化。目前,更改的内容直接来自 div“.post”。也就是说,当用户滚动到给定的“.post”时,固定的灰色框会加载“.post”中的内容。

我想要做的是让灰色框显示用户当前看到的内容的描述。因此,当用户到达 div“content1”时,灰色框会显示“content1”的文本描述。也许当达到“content1”时,div“description1”会在灰色框中取消隐藏?

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

在包含描述的每个部分中添加一个隐藏元素,例如:

<div id="content1">
<p class="description" style="display: none;">content1 description</p>
....
</div>

然后在 javascript 中获取相关部分的描述,如下所示:

if(pos.top >= $(this).offset().top && pos.top <= $(this).next().offset().top)
{
$('#date').html($(this).find('.description').text());
return;
}

Jsfiddle

关于javascript - 根据滚动位置更改 div 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16204318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com