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
保存divId的位置
#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
}
查看全文
相关阅读:
几个函数小练习
结构体和枚举类型
结构体
几个函数小练习
函数简介
网页端滚轮滑动事件
碰撞检测
Ajax前后台交互函数
事件绑定与解除js
移动端的屏幕适配问题
原文地址:https://www.cnblogs.com/ghd258/p/270441.html
最新文章
6.18 函数查缺,补漏
6.17 类思想的简单学习, 以及 封装
6.17 复习 根据学生分数进行学生信息的 冒泡排序
6.17 面向对象思想及类的提纲
6.15 SQL 结业测试习题
JS两种事件的触发方式
HTML布局思路
Response对象
Repeater控件
选择控件
热门文章
按钮控件
文本控件
数据库的SQL语句创建和主外键删除操作
ASP.NET常见面试题及答案(130题)
asp.net c# 网上搜集面试题目大全(附答案)
简单邮箱界面
插入账号密码框和选框
验证码和简历表格
网页基础操作
函数调用自己递归小练习
Copyright © 2011-2022 走看看