gpt4 book ai didi

javascript - 如何在页面提交时自动滚动到页面顶部

转载 作者:行者123 更新时间:2023-11-28 05:51:08 24 4
gpt4 key购买 nike

我正在使用 thymeleaf 在 MVC 架构中使用 angularjs。我想在提交页面或自动刷新页面时将我的页面自动推送到顶部。

我知道使用下面的函数是可以实现的。但我到底要在哪里编写它(必须在脚本文件或 thymeleaf HTML 页面的每个页面中编写?)以及如何在 thymeleaf 页面中使用。

如果我在脚本文件中写入,那么如何以及在何处在 thymeleaf HTML 页面中使用该 id?

$window.scrollTo(0, angular.element('put here your element').offsetTop);   

最佳答案

查看$anchorScroll我认为它会提供您正在寻找的东西。

它可以滚动到命名输入或 $location.hash() 的最新值

作为示例,以下是提交页面的方法:

submitForm() {
doStuff(); // Shortening what you would do for submitting;

this._$anchorScroll('#named-element-id');
}

另一个例子是使用 $location.hash() 如果您已将其设置在 Controller 中的其他位置。然后你可以直接调用 $anchorScroll() 而不带参数。

关于javascript - 如何在页面提交时自动滚动到页面顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38070521/

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