[英]Cannot properly refresh Bootstrap scrollspy spying on <body> element
I've been referencing this post regarding Bootstrap's scrollspy component. 我一直在引用关于Bootstrap的scrollspy组件的这篇文章。 In my code, I instantiate a new scrollspy to spy on the
body
element using: 在我的代码中,我使用以下方法实例化一个新的scrollspy来监视
body
元素:
$("body").scrollspy({ offset: 25 });
Later on in my code, I make an AJAX call and add/remove elements from the page. 稍后在我的代码中,我进行了一个AJAX调用并在页面中添加/删除元素。 This causes the scrollspy to be misaligned, so I need to refresh the scrollspy.
这会导致scrollspy错位,所以我需要刷新scrollspy。 I have tried performing this refresh operation many ways, such as:
我尝试过多种方式执行此刷新操作,例如:
$("body").scrollspy("refresh");
and 和
$('[data-spy="scroll"]').each(function () {
$(this).scrollspy('refresh');
});
However, neither of these code snippets bring about any change in the behavior of the scrollspy. 但是,这些代码片段都不会导致scrollspy的行为发生任何变化。 I believe this is because the scrollspy is spying directly on the
body
element, but I am unsure of how to have the scrollspy refresh following my AJAX calls. 我相信这是因为scrollspy直接在
body
元素上进行间谍活动,但我不确定如何在我的AJAX调用之后刷新scrolllspy。 Does anyone know how I might refresh my scrollspy? 有谁知道我怎么可能刷新我的卷轴?
It appears that it is not possible to refresh a scrollspy that is set to spy on the body
element via a call to $("body").scrollspy()
. 似乎无法通过调用
$("body").scrollspy()
来刷新设置为监视body
元素的$("body").scrollspy()
。 In order to use the refresh functionality documented on the Bootstrap website, I had to explicitly declare the data-spy
and data-target
attributes of the body
tag (where scrollingNav
is the id
of the nav
bar in which to visualize the scrolling): 为了使用Bootstrap网站上记录的刷新功能,我必须显式声明
body
标签的data-spy
和data-target
属性(其中scrollingNav
是可视化滚动的nav
栏的id
):
<body data-spy="scroll" data-target="#scrollingNav">
Then, to refresh this scrollspy when elements were dynamically added/removed from the page, I used the following method: 然后,为了在从页面动态添加/删除元素时刷新此scrollspy,我使用以下方法:
function refreshScrollSpy() {
$('[data-spy="scroll"]').each(function () {
$(this).scrollspy('refresh');
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.