【问题标题】:SharePoint 2016 on premise : how to scroll to top of the page?本地 SharePoint 2016:如何滚动到页面顶部?
【发布时间】:2018-07-24 23:54:51
【问题描述】:

我的用例非常简单:我在自定义列表表单中(修改后的 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% 确定我的函数在在浏览器的调试器中查看。

我使用的是 IE11,同时处于“10”和“Edge”模式。

这是我的尝试(我一一尝试,不在同一个函数中)

//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;
}

这些都不起作用。当我点击按钮时,断点显示我的脚本已执行,但它就像 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;

【问题讨论】:

    标签: javascript sharepoint sharepoint-2016


    【解决方案1】:

    我终于做到了:

    var w = document.getElementById("s4-workspace");
    w.scrollTop = 0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 2023-02-25
      • 1970-01-01
      • 2014-11-05
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多