异步回发后如何保持整个页面滚动位置

发布于 2024-09-28 09:34:59 字数 897 浏览 4 评论 0原文

我正在使用 asp.net 4.0 iis 7.5 microsoft Visual Studio 2010

我想要的是当异步回发发生时(更新面板)保持整个页面(浏览器)滚动位置(不是 div 或面板)

我该怎么做

实际上我有一个函数可以像这样在回发后保持 div 滚动条位置

       <script type="text/javascript">
        var xPos, yPos;
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_beginRequest(BeginRequestHandler);
        prm.add_endRequest(EndRequestHandler);
        function BeginRequestHandler(sender, args) {
            xPos = document.getElementById('Main').scrollLeft;
            yPos = document.getElementById('Main').scrollTop;
        }
        function EndRequestHandler(sender, args) {
            document.getElementById('Main').scrollLeft = xPos;
            document.getElementById('Main').scrollTop = yPos;
        }
    </script>

,但我找不到浏览器滚动条 id 来获取其值以使用 document.getElementById 获取,

谢谢您的答案

i am using asp.net 4.0 iis 7.5 microsoft visual studio 2010

what i want is keep whole page (browser) scroll position (not a div or panel) when asynchronous postback happened (update panel)

how can i do this

actually i had a function which can keep div scroll bar position after postback like this

       <script type="text/javascript">
        var xPos, yPos;
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_beginRequest(BeginRequestHandler);
        prm.add_endRequest(EndRequestHandler);
        function BeginRequestHandler(sender, args) {
            xPos = document.getElementById('Main').scrollLeft;
            yPos = document.getElementById('Main').scrollTop;
        }
        function EndRequestHandler(sender, args) {
            document.getElementById('Main').scrollLeft = xPos;
            document.getElementById('Main').scrollTop = yPos;
        }
    </script>

bu i could not find browser scroll bar id to get its values to get with document.getElementById

thanks for answers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叶落知秋 2024-10-05 09:34:59

asp.net 有一个名为 MaintainScrollPositionOnPostBack

希望这会有所帮助

asp.net has @page directive property called MaintainScrollPositionOnPostBack

hope this will help

身边 2024-10-05 09:34:59

我在这里找到它:http://forums.asp.net/t/1300961.aspx

只需将其添加到脚本管理器之后即可。它在所有浏览器中都适用于我

<script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(beginRequest);
    function beginRequest()
    {
        prm._scrollPosition = null;
    }
</script>

I found it here :http://forums.asp.net/t/1300961.aspx

just add it right after script manager. it works for me in all browsers

<script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(beginRequest);
    function beginRequest()
    {
        prm._scrollPosition = null;
    }
</script>
悲歌长辞 2024-10-05 09:34:59

你可以用客户端的方式做到这一点:

$(document).ready(function () {
    $(window).on('beforeunload', function () {
        document.cookie = "keepscroll=" + $(window).scrollTop();
    });
    var cs = document.cookie ? document.cookie.split(';') : [];
    var i = 0, cslen = cs.length;
    for (; i < cs.length; i++) {
        var c = cs[i].split('=');
        if (c[0].trim() == "keepscroll") {
            $(window).scrollTop(parseInt(c[1]));
            break;
        }
    }
});

如果你不是 jQuery 的朋友那么你可以尝试类似的方法:

window.onbeforeunload = function () {
    document.cookie = "keepscroll=" + document.body.scrollTop;
};
var keepscroll = window.setTimeout(function () {
    var cs = document.cookie ? document.cookie.split(';') : [];
    var i = 0, cslen = cs.length;
    for (; i < cs.length; i++) {
        var c = cs[i].split('=');
        if (c[0].trim() == "keepscroll") {
            window.scrollTo(0, parseInt(c[1]));
            break;
        }
    }
    window.clearTimeout(keepscroll);
    keepscroll = null;
}, 100);

You can do it the client way :

$(document).ready(function () {
    $(window).on('beforeunload', function () {
        document.cookie = "keepscroll=" + $(window).scrollTop();
    });
    var cs = document.cookie ? document.cookie.split(';') : [];
    var i = 0, cslen = cs.length;
    for (; i < cs.length; i++) {
        var c = cs[i].split('=');
        if (c[0].trim() == "keepscroll") {
            $(window).scrollTop(parseInt(c[1]));
            break;
        }
    }
});

If you are not jQuery's friend then you could try something like :

window.onbeforeunload = function () {
    document.cookie = "keepscroll=" + document.body.scrollTop;
};
var keepscroll = window.setTimeout(function () {
    var cs = document.cookie ? document.cookie.split(';') : [];
    var i = 0, cslen = cs.length;
    for (; i < cs.length; i++) {
        var c = cs[i].split('=');
        if (c[0].trim() == "keepscroll") {
            window.scrollTo(0, parseInt(c[1]));
            break;
        }
    }
    window.clearTimeout(keepscroll);
    keepscroll = null;
}, 100);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文