zoukankan      html  css  js  c++  java
  • 保存div与页面滚动条的位置

    1/// <summary>
     2        /// 保存div与页面滚动条的位置
     3        /// </summary>
     9        /// <param name="divId">DIV的ID数组</param>

    10        public static void RetainScrollPosition(string[] divId)
    11        {
    12            
    13            System.Web.UI.Page page=(Page)HttpContext.Current.Handler;
    14
    15            string saveScrollPosition = "";
    16            string setScrollPosition = "";
    17
    18            string saveScrollPositionDiv = "";
    19            string setScrollPositionDiv = "";
    20
    21            #region 保存页面滚动条的状态
    22            page.RegisterHiddenField("__SCROLLPOS_TOP","0");
    23            page.RegisterHiddenField("__SCROLLPOS_LEFT","0");
    24                
    25            saveScrollPosition = "document.forms[0].__SCROLLPOS_TOP.value = {0}.scrollTop;document.forms[0].__SCROLLPOS_LEFT.value = {0}.scrollLeft;";//}{0}.onscroll=saveScrollPosition;</script>";
    26            setScrollPosition = "{0}.scrollTop ='{1}';{0}.scrollLeft ='{2}';";//}{0}.onload=setScrollPosition;</script>";
    27            
    28            saveScrollPosition = string.Format(saveScrollPosition,"window.document.body");
    29            setScrollPosition = string.Format(setScrollPosition,"window.document.body", page.Request.Form["__SCROLLPOS_TOP"],page.Request.Form["__SCROLLPOS_LEFT"]);
    30            #endregion

    31
    32            #region 保存divId的位置
    33            for (int i = 0; i < divId.Length; i++)
    34            {
    35                page.RegisterHiddenField(divId[i].ToString() + "_TOP","0");
    36                page.RegisterHiddenField(divId[i].ToString() + "_LEFT","0");
    37                
    38                saveScrollPositionDiv = "<script language='javascript'>function saveScrollPosition" + divId[i].ToString() + "() {{document.all('" + divId[i].ToString() + "_TOP').value = {0}.scrollTop;document.all('" + divId[i].ToString() + "_LEFT').value = {0}.scrollLeft;}}{0}.onscroll=saveScrollPosition" + divId[i].ToString() + ";</script>";
    39                setScrollPositionDiv += "{0}.scrollTop = {1};{0}.scrollLeft = {2};";
    40
    41                saveScrollPositionDiv = string.Format(saveScrollPositionDiv,"window.document.all('" + divId[i].ToString() + "')");
    42                page.RegisterStartupScript("saveScroll" + divId[i].ToString(), saveScrollPositionDiv);
    43
    44                setScrollPositionDiv = string.Format(setScrollPositionDiv,"window.document.all('" + divId[i].ToString() + "')", page.Request.Form[divId[i].ToString() + "_TOP"],page.Request.Form[divId[i].ToString() + "_LEFT"]);
    45            }

    46            #endregion

    47                
    48            #region 注册客户端脚本
    49            saveScrollPosition = "<script language='javascript'>function saveScrollPosition() {{" + saveScrollPosition + "}}{0}.onscroll=saveScrollPosition;</script>";
    50            page.RegisterStartupScript("saveScroll"string.Format(saveScrollPosition,"window.document.body"));
    51
    52            if(page.IsPostBack)
    53            {
    54                setScrollPosition = "<script language='javascript'>function setScrollPosition() {{" + setScrollPosition + setScrollPositionDiv + "}}{0}.onload=setScrollPosition;</script>";
    55                page.RegisterStartupScript("setScroll"string.Format(setScrollPosition,"window.document.body"));
    56                //page.RegisterStartupScript("setScrollDocument", string.Format(setScrollPosition,"window.document.body", page.Request.Form["__SCROLLPOS_TOP"],page.Request.Form["__SCROLLPOS_LEFT"]));
    57            }

    58            #endregion

    59            
    60        }
  • 相关阅读:
    c# Exception 异常信息归整
    解决PKIX(PKIX path building failed) 问题 unable to find valid certification path to requested target
    java Thumbnails 加载网络图片,处理返回base64
    C# 解析 Json
    mysql 中文乱码解决方法
    .Net WinForm下配置Log4Net(总结不输出原因)
    uploadify 后台动态传参数
    C# rename方法重命名文件
    将Qt5.5 动态链接生成的exe及依赖dll打包方法
    远程桌面Default.rdp 中各个参数的含义(转)
  • 原文地址:https://www.cnblogs.com/ghd258/p/270441.html
Copyright © 2011-2022 走看看