SharePoint 2016 on premise:如何滚动到页面顶部?
SharePoint 2016 on premise : how to scroll to top of the page?
我的用例非常简单:我在自定义列表表单中(修改 NewForm.aspx,使用 SharePoint Designer 添加)。我添加了一个按钮,我希望当用户点击它时,页面会滚动回到最顶部。
我试过以下方法:
在aspx
<button onclick="return MyScrollTop()">SCROLL TOP</button>
在javascript
function MyScrollTop() {
//All my attemps go here
return false;
}
我没有详细说明我是如何确保函数被调用的(有时在 SharePoint 中使用 MDS 和 _spBodyOnLoadFunctionNames.push 可能会很棘手,但我 100% 确定我的函数正如我在浏览器的调试器中看到的那样被调用。
我在“10”和 "Edge" 模式下使用 IE11。
以下是我的尝试(我一一尝试过,不是在同一个函数中)
//attempt #1 (as seen on W3C)
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
//attempt #2 (as seen on Stack Overflow for regular javascript)
window.scrollTo(0,0);
//attempt #3 (as seen on Stack Overflow for some corner case - desperate attempt)
window.scroll(0,0);
//attempt #4 (as seen on Stack Overflow to fight SharePoint's '_maintainWorkspaceScrollPosition' hidden control on a page reload or unload)
var scrollX = document.getElementById('__SCROLLPOSITIONX');
var scrollY = document.getElementById('__SCROLLPOSITIONY');
if (scrollX && scrollY) {
scrollX.value = 0;
scrollY.value = 0;
}
var workspaceY = document.getElementById('_maintainWorkspaceScrollPosition');
if (workspaceY) {
workspaceY.value = 0;
}
None 这些作品。当我点击按钮时,断点显示我的脚本被执行了,但是像window.scrollTo和其他的完全没有效果。
我已经在 init.js 的这个 SharePoint 函数中设置了一个断点,看看我是否可以在某个地方挂钩,但我不确定我应该做什么:
if (!g_setScrollPos) {
if (browseris.firefox && browseris.firefox36up)
window.scrollTo(0, 0);
if (Boolean((ajaxNavigate.get_search()).match(RegExp("[?&]IsDlg=1")))) {
if (!isIE7 || elmWorkspace.scrollHeight < elmWorkspace.clientHeight)
elmWorkspace.style.overflowY = "auto";
}
var scrollElem = document.getElementById("_maintainWorkspaceScrollPosition");
if (scrollElem != null && scrollElem.value != null) {
elmWorkspace.scrollTop = Number(scrollElem.value);
}
g_setScrollPos = true;
}
CallWorkspaceResizedEventHandlers();
g_frl = false;
我终于做到了:
var w = document.getElementById("s4-workspace");
w.scrollTop = 0;
我的用例非常简单:我在自定义列表表单中(修改 NewForm.aspx,使用 SharePoint Designer 添加)。我添加了一个按钮,我希望当用户点击它时,页面会滚动回到最顶部。
我试过以下方法:
在aspx
<button onclick="return MyScrollTop()">SCROLL TOP</button>
在javascript
function MyScrollTop() {
//All my attemps go here
return false;
}
我没有详细说明我是如何确保函数被调用的(有时在 SharePoint 中使用 MDS 和 _spBodyOnLoadFunctionNames.push 可能会很棘手,但我 100% 确定我的函数正如我在浏览器的调试器中看到的那样被调用。
我在“10”和 "Edge" 模式下使用 IE11。
以下是我的尝试(我一一尝试过,不是在同一个函数中)
//attempt #1 (as seen on W3C)
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
//attempt #2 (as seen on Stack Overflow for regular javascript)
window.scrollTo(0,0);
//attempt #3 (as seen on Stack Overflow for some corner case - desperate attempt)
window.scroll(0,0);
//attempt #4 (as seen on Stack Overflow to fight SharePoint's '_maintainWorkspaceScrollPosition' hidden control on a page reload or unload)
var scrollX = document.getElementById('__SCROLLPOSITIONX');
var scrollY = document.getElementById('__SCROLLPOSITIONY');
if (scrollX && scrollY) {
scrollX.value = 0;
scrollY.value = 0;
}
var workspaceY = document.getElementById('_maintainWorkspaceScrollPosition');
if (workspaceY) {
workspaceY.value = 0;
}
None 这些作品。当我点击按钮时,断点显示我的脚本被执行了,但是像window.scrollTo和其他的完全没有效果。
我已经在 init.js 的这个 SharePoint 函数中设置了一个断点,看看我是否可以在某个地方挂钩,但我不确定我应该做什么:
if (!g_setScrollPos) {
if (browseris.firefox && browseris.firefox36up)
window.scrollTo(0, 0);
if (Boolean((ajaxNavigate.get_search()).match(RegExp("[?&]IsDlg=1")))) {
if (!isIE7 || elmWorkspace.scrollHeight < elmWorkspace.clientHeight)
elmWorkspace.style.overflowY = "auto";
}
var scrollElem = document.getElementById("_maintainWorkspaceScrollPosition");
if (scrollElem != null && scrollElem.value != null) {
elmWorkspace.scrollTop = Number(scrollElem.value);
}
g_setScrollPos = true;
}
CallWorkspaceResizedEventHandlers();
g_frl = false;
我终于做到了:
var w = document.getElementById("s4-workspace");
w.scrollTop = 0;