当前位置: 代码迷 >> Web前端 >> 页面刷新后,自动滚动到早先位置
  详细解决方案

页面刷新后,自动滚动到早先位置

热度:108   发布时间:2013-03-14 10:33:15.0
页面刷新后,自动滚动到原先位置

 在ASP.NET页面中,每次页面回传,页面都会回到页面顶端,有时候,这是个麻烦,那么怎么避免这种情况呢。我总结了三种方式

1.页面里有MaintainScrollPositionOnPostback,默认是false,设为true即可

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"  MaintainScrollPositionOnPostback="true"
    Inherits="Default.Default" %>

2.可以使用Jquery,通过获取当前位置的高度,具体实现如下

function position()
{
     var top=$("#元素id").offset().top();
     $("html,body").animate({scrollTop:top},1000);
}

3.可以使用锚点,但这里可使用灵活处理

首先获取需要滚动到的位置的id,如,可以设置一个元素(<span name="postion" id="postion"></span>,注:要在form里),另外在form的任意位置设置<a href="#postion" id="click"></a>注:a标签里不要有内容,在回传的地方调用

Page.ClientScript.RegisterStartupScript(this.GetType(), "scroll", "document.getElementById('position').click();", true);
这种方法其实就是触发某个元素的事件

  相关解决方案