当我单击按钮时,我需要自动滚动到页面底部。我在论坛上找到了很多解决方案,但对我来说不起作用。我在 Java 脚本中尝试过:
<asp:Button ID="btn_add_action1" runat="server" Text="Ajouter une action" onclick="btn_add_action1_Click" OnClientClick = "goToBottom()" />
用JS函数:
window.scrollTo(0,document.body.scrollHeight);
或
document.body.scrollTop = document.body.scrollHeight;
我在这里找到了这个:Scroll Automatically to the Bottom of the Page
我尝试了许多其他解决方案,但它不起作用
最佳答案
当您单击该按钮时,将执行回发。这意味着滚动位置将丢失。如果您想滚动到页面底部,则必须在回发完成后使用 ScriptManager
protected void Button1_Click(object sender, EventArgs e)
{
//your button code
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "scrollDown", "setTimeout(function () { window.scrollTo(0,document.body.scrollHeight); }, 25);", true);
}
还有一个名为 MaintainScrollPositionOnPostBack
的东西,它的作用类似,它会转到与 PostBack 之后单击按钮相同的位置。
关于javascript - postBack后滚动到页面底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45158736/