• 用JS实现页面滚动位置保持的方法


    c#

    step 1
    replace the tag of <body> with the follewing codes:

    <%
      if (Request["__SCROLLPOS"] != null &&
          Request["__SCROLLPOS"] != String.Empty) {
          int pos = Convert.ToInt32 (Request["__SCROLLPOS"]);
          Response.Write ("<body id=\"theBody\" " +
              "onscroll=\"javascript:document.forms[0].__SCROLLPOS.value = " +
              "theBody.scrollTop;\" " +
              "onload=\"javascript:theBody.scrollTop=" + pos + ";\">");
      }
      else {
          Response.Write ("<body id=\"theBody\" " +
              "onscroll=\"javascript:document.forms[0].__SCROLLPOS.value =" +
              "theBody.scrollTop;\">");
      }
    %>

    step 2
    add the follewing codes between <form> and </form>

    <input type="hidden" name="__SCROLLPOS" value="" />



    vb

    step 1
    replace the tag of <body> with the follewing codes:

    <%
             If Not Request("__SCROLLPOS") Is Nothing then
             if Request("__SCROLLPOS") <> String.Empty Then
                Dim pos As Integer = CType(Request("__SCROLLPOS"), Integer)
                Response.Write("<body id=""theBody"" onscroll=""javascript:document.forms[0].__SCROLLPOS.value = theBody.scrollTop;"" onload=""javascript:theBody.scrollTop=" + pos.tostring + ";"">")
           end if
           else
                response.Write("<body id=""theBody"" onscroll=""javascript:document.forms[0].__SCROLLPOS.value =theBody.scrollTop;"">")
         
            end if
    %>

    step 2
    add the follewing codes between <form> and </form>

    <input type="hidden" name="__SCROLLPOS" value="" />




    js版的:)

    <!--
    Created by 宝玉 , 2004-2-21
    http://www.webuc.net

    Description: 记录页面上次的滚动条位置
    -->
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> 记录滚动条位置 </TITLE>

    <script language="javascript">
        <!--
        //  获取当前文件名
        function getFileName()
        {
            var url = this.location.href
            var pos = url.lastIndexOf("/");
            if(pos == -1)
                pos = url.lastIndexOf("\\")
            var filename = url.substr(pos +1)
            return filename;
        }

        function fnLoad()
        {
            with(window.document.body)
            {
                addBehavior ("#default#userData");    // 使得body元素可以支持userdate
                load("scrollState" + getFileName());    // 获取以前保存在userdate中的状态
                scrollLeft = getAttribute("scrollLeft");    // 滚动条左位置
                scrollTop = getAttribute("scrollTop");
            }
        }

        function fnUnload()
        {
            with(window.document.body)
            {
                setAttribute("scrollLeft",scrollLeft);
                setAttribute("scrollTop",scrollTop);
                save("scrollState" + getFileName());   
                // 防止受其他文件的userdate数据影响,所以将文件名加上了
                // userdate里的数据是不能跨目录访问的
            }
        }
        window.onload = fnLoad;
        window.onunload = fnUnload;

        // -->
    </script>
    </HEAD>

  • 相关阅读:
    知识全聚集 .Net Core 技术突破 | 如何实现一个模块化方案一
    企业项目实战 .Net Core + Vue/Angular 分库分表日志系统六 | 最终篇-通过AOP自动连接数据库-完成日志业务
    企业项目实战 .Net Core + Vue/Angular 分库分表日志系统五 | 完善业务自动创建数据库
    企业项目实战 .Net Core + Vue/Angular 分库分表日志系统四 | 强化设计方案
    企业项目实战 .Net Core + Vue/Angular 分库分表日志系统三 | 控制反转搭配简单业务
    企业项目实战 .Net Core + Vue/Angular 分库分表日志系统二 | 简单的分库分表设计
    SDN+DPI文献阅读(2)
    SDN中的Heavy-Hitter测量文献阅读
    DPI技术简介
    SDN+DPI文献阅读
  • 原文地址:https://www.cnblogs.com/goody9807/p/411904.html
Copyright © 2020-2023  润新知