2010-08-27 122 views

回答

3

创建ASP隐藏的输入字段该字段的客户端ID存储在回发的位置,进入下面的代码:

// client id of the hidden input field 
var hiddenInputId = '<%= _myHiddenInputField.ClientID %>'; 

// store the current scroll position into the input 
function storeScrollPosition(){ 
    $('#'+hiddenInputId)[0].value = scrollPosition(); 
} 

// load the value out of the input and scroll the page 
function loadScrollPosition(){ 
    var curPosition = $('#'+hiddenInputId)[0].value; 
    if (curPosition > 0) 
     $(window).scroll(curPosition); 
} 

// determine the scroll position (cross browser code) 
function scrollPosition() { 
    var n_result = window.pageYOffset ? 
        window.pageYOffset : 0; 
    var n_docel = document.documentElement ? 
        document.documentElement.scrollTop : 0; 
    var n_body = document.body ? 
       document.body.scrollTop : 0; 
    if (n_docel && (!n_result || (n_result > n_docel))) 
     n_result = n_docel; 
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result; 
} 

// on load of the page, load the previous scroll position 
$(document).ready(function(){loadScrollPosition();}); 
// on scroll of the page, update the input field 
$(window).scroll(function(){storeScrollPosition();}); 

这只适用于回传。如果你需要总是有相同的屏幕位置,我们可以玩饼干:)

+0

我改变了这一行:** $(window).scroll(curPosition); ** 使用** scrollTop **函数,这工作很好。谢谢! – 2012-01-11 13:37:35

相关问题