admin管理员组文章数量:1387285
I've been referencing this post regarding Bootstrap's scrollspy ponent. In my code, I instantiate a new scrollspy to spy on the body
element using:
$("body").scrollspy({ offset: 25 });
Later on in my code, I make an AJAX call and add/remove elements from the page. This causes the scrollspy to be misaligned, so I need to refresh the 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. 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. Does anyone know how I might refresh my scrollspy?
I've been referencing this post regarding Bootstrap's scrollspy ponent. In my code, I instantiate a new scrollspy to spy on the body
element using:
$("body").scrollspy({ offset: 25 });
Later on in my code, I make an AJAX call and add/remove elements from the page. This causes the scrollspy to be misaligned, so I need to refresh the 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. 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. Does anyone know how I might refresh my scrollspy?
1 Answer
Reset to default 6It 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()
. 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):
<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:
function refreshScrollSpy() {
$('[data-spy="scroll"]').each(function () {
$(this).scrollspy('refresh');
});
};
本文标签: javascriptCannot properly refresh Bootstrap scrollspy spying on ltbodygt elementStack Overflow
版权声明:本文标题:javascript - Cannot properly refresh Bootstrap scrollspy spying on <body> element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744496637a2609068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论