gpt4 book ai didi

javascript - 使用 AngularJS $anchorScroll 滚动到另一个页面的 anchor 标记

转载 作者:搜寻专家 更新时间:2023-10-31 08:44:14 24 4
gpt4 key购买 nike

我需要从我的 AngularJS 主页滚动到另一个页面中的 anchor 标记。这两个页面都基于 url 作为部分 html 进入 ng-view 组件。在启动服务器时,主页被加载,需要从那里转到常见问题解答页面。我使用带# 的普通href,但没有指向目标页面中的正确div。这是我用 $anchorScroll

尝试的

这是 Controller 方法

	
$scope.scrollToFaq = function(id) {
$location.path("/faq")
$location.hash(id);
$anchorScroll();
}

这是我在 home.html 中的使用方式

<a ng-click="scrollToFaq('HERE')" href="" class="link">Here</a>

但这不能正常工作。如果我直接加载常见问题解答页面然后返回并单击主页上的 anchor 链接,它就可以工作。是否可以使用 `$anchorScroll

来做到这一点

最佳答案

尝试使用 Angular 的 $timeout 服务。

$scope.scrollToFaq = function(id) {
$location.path("/faq");
$timeout(function(){
$location.hash(id);
$anchorScroll();
}, delay);

此处 delay 是您希望等待 anchor 滚动发生的时间。我读过大约 300 毫秒/400 毫秒为人们工作。当我遇到这个问题时,我只需要调用 $timeout 中的 $anchorscroll。因此,请使用最适合您的情况。

注意:我正在寻找不使用 $timeout 的解决方案。当我找到一个时会更新。

关于javascript - 使用 AngularJS $anchorScroll 滚动到另一个页面的 anchor 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29034349/

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