这是ASP.NET MVC项目,我在页面的特定位置放置了div
并添加了标识dealProductAnchor
。当这个页面正常加载或重新加载时,它表现正常,但是当点击一个特定按钮时我在页面加载完成后完成所有回发进度后想要那个页面,它会到达该div的位置。 / p>
使用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;
}
});
答案 0 :(得分:1)
如果您想通过BackEnd实现此功能,可以将值设置为ViewBag
并在.cshtml
页面上查看,如下所示:
控制器代码:
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>
}
});
希望它可以帮助你:)
答案 1 :(得分:0)
你能试试吗??
$(".button-1").click(function () {
$('html, body').animate({
scrollTop: $("#dealProductAnchor").offset().top
}, 2000);
});
答案 2 :(得分:0)
而不是设置:
Date,Time with Timezone
在window.location = window.location.href + "#dealProductAnchor";
回调中,使用Javascript设置scrolltop:
$(document).ready
如果这不起作用,您可能需要添加$(document).ready(function () {
if (localStorage.getItem('click') == true) {
$('body').scrollTop($("#dealProductAnchor").offset().top)
localStorage.removeItem('click');
}
});
包装器以允许浏览器完成呈现:
window.setTimeout()
答案 3 :(得分:0)
我尝试更改表单操作属性以包含哈希
$(".button-1").click(function () {
//Change $("form") to use the form ID if you have one
$("form").attr("action", $("form").attr("action") + "#dealProductAnchor" );
}
如果按钮正在提交表单,您可能需要将按钮从提交更改为按钮,然后在单击处理程序中提交表单。