这是 ASP.NET MVC 项目,我在页面的特定位置放置了一个 div
并添加了 id dealProductAnchor
。当此页面正常加载或重新加载时,它的行为正常,但是当单击特定按钮时我希望在页面加载完成后完成所有回发进度后该页面,它会到达该 div 的位置。
使用java脚本我尝试了这个,但没有做任何来自服务器端或客户端的解决方案将是可观的。
$(".button-1").click(function () {
if (localStorage && !localStorage.getItem('click')) {
localStorage.setItem('click', true);
}
});
$(document).ready(function () {
if (localStorage.getItem('click') == true) {
window.location = window.location.href + "#dealProductAnchor";
//click = false;
}
});
最佳答案
如果您想通过后端实现此目的,您可以在 ViewBag
中设置一个值,并在 .cshtml
页面上检查它,如下所示:
Controller 代码:
public ActionResult TextAction() {
// Do your Logic here
ViewBag.testValue = "test";
return View();
}
JavaScript 代码:
$(document).ready(function () {
@if (ViewBag.testValue == "test") {
<text>$('html,body').animate({
scrollTop: ($("#dealProductAnchor").offset().top) - 200
}, 'slow');</text>
}
});
希望对你有帮助:)
关于javascript - 在 MVC ASP.NET 中重新加载页面时如何在特定位置滚动页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39138554/