我一直试图在页面加载时自动转到网页的底部。事实上,在get和post上,无论视图如何被调用,它都必须自动进入页面底部。
我想用javascript做。
看起来很简单,我可以在这里轻松找到。这看起来像是:
Set page scroll position on page load of MVC app
只有一个问题......答案并没有将javascript解决方案放在上下文中。没有上下文...我不知道在哪里放这些线,无论我尝试什么......
我不会整天玩,知道如何实现这一目标,所以
@model WhateverModelYouWant
@{
ViewBag.Title = "Formulaire de reprise";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript">
document.getElementById("ImportantStuff").scrollIntoView();
</script>
<h2> TITLE OF THE VIEW <h2>
<div> Lots of content here </div>
<div> Even more content here </div>
<div id="ImportantStuff"> Important stuff here </div>
<input type="submit" value="ImportantButton" >
毋庸置疑,这并不会使页面在任何地方滚动......提前致谢。
错误:
答案 0 :(得分:3)
在您的示例中,无论如何都无法滚动任何内容,因为所有内容都在可见区域中。除此之外,mituw16已经为您提供了正确的解决方案。以下是如何使用scrollIntoView函数的示例。
<script type="text/javascript">
function scrollToImportantStuff() {
document.getElementById('ImportantStuff').scrollIntoView()
}
window.onload = scrollToImportantStuff;
</script>
<h2> TITLE OF THE VIEW <h2>
<input type="button" onclick="document.getElementById('ImportantStuff').scrollIntoView()" value="Scroll ImportantStuff into View" />
<div style="height:500px;"> Lots of content here </div>
<div style="height:500px;"> Even more content here </div>
<div id="ImportantStuff"> Important stuff here </div>
答案 1 :(得分:1)
这与MVC没有任何关系。它是用javascript完成的。
document.getElementById("ImportantStuff").scrollIntoView();
https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView