您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關Asp.net中怎么保持頁面滾動條位置,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
代碼如下:
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
代碼如下:
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:
復制代碼 代碼如下:
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];
}
}
}
在頁面后臺調用的時候,寫下如下的代碼:
復制代碼 代碼如下:
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
以上就是Asp.net中怎么保持頁面滾動條位置,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。