一般來(lái)說(shuō)很多的人在控制瀏覽器滾動(dòng)條都是用Page.SmartNavigation = False,對(duì)于頁(yè)面中DIV等等的滾動(dòng)條控制,我在看了別人代碼后,寫(xiě)了一個(gè),僅供參考:
復(fù)制代碼 代碼如下:
Public Shared Sub SaveDivScrollPosition(ByVal divIDArray As String, ByVal objPage As Page)
Dim saveScrollPosition As String
Dim i As Integer
Dim divID() As String
divID = divIDArray.Split(",")
For i = 0 To divID.Length - 1
objPage.RegisterHiddenField(divID(i) "__SCROLLPOS", objPage.Request.Form(divID(i) "__SCROLLPOS"))
saveScrollPosition = "script language='javascript'>" _
"function saveScrollPosition() {" _
" if (document.all['" divID(i) "'] != undefined) {" _
"document.forms[0]." divID(i) "__SCROLLPOS.value = " _
"document.all['" divID(i) "'].scrollTop + ',' " _
" + document.all['" divID(i) "'].scrollLeft;}}" _
"if (document.all['" divID(i) "'] != undefined) {document.all['" divID(i) "'].onscroll=saveScrollPosition;}" _
"/script>"
objPage.RegisterStartupScript("saveScroll" divID(i), saveScrollPosition)
Next
End Sub
復(fù)制代碼 代碼如下:
Public Shared Sub RestoreDivScrollPosition(ByVal divIDArray As String, ByVal objPage As Page)
Dim restoreScrollPosition As String
Dim setPositionFunction As String
Dim i As Integer
Dim divID() As String
divID = divIDArray.Split(",")
For i = 0 To divID.Length - 1
setPositionFunction = setPositionFunction "SetScrollPosition('" divID(i) "');"
Next
restoreScrollPosition = "script language='javascript'>" _
setPositionFunction _
"/script>"
objPage.RegisterStartupScript("restoreScroll", restoreScrollPosition)
End Sub
JAVASCRIPT:
復(fù)制代碼 代碼如下:
function SetScrollPosition(divID){
var e;
var a;
var obj;
if (document.getElementById(divID)){
obj = eval('document.forms[0].' + divID + '__SCROLLPOS');
if (obj) {
e=eval('document.forms[0].' + divID + '__SCROLLPOS').value;
a=e.split(',');
document.getElementById(divID).scrollTop= a[0];
document.getElementById(divID).scrollLeft= a[1];
}
}
}
在頁(yè)面后臺(tái)調(diào)用的時(shí)候,寫(xiě)下如下的代碼:
復(fù)制代碼 代碼如下:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
call SaveDivScrollPosition("div1,div2,div3",me)
call RestoreDivScrollPosition("div1,div2,div3",me)
End Sub
不好意思,這兩個(gè)SUB可以寫(xiě)成一個(gè),僅供參考。
您可能感興趣的文章:- asp.net通過(guò)Ajax UpdatePanel回傳后滾動(dòng)條位置變更解決方法
- 解決ASP.NET回傳后div滾動(dòng)條位置復(fù)位的問(wèn)題(利用隱藏控件原理)
- ASP.Net刷新頁(yè)面后自動(dòng)滾動(dòng)到原來(lái)位置方法匯總