Javascript新手,有人可以逐行解释这个代码吗?

时间:2009-02-12 21:08:10

标签: javascript

我使用此代码来保持滚动位置,并且不知道它的含义。如果有人有时间,你能否为我提供一步一步解释它在做什么。这是:

<script language="javascript"  type="text/javascript">
    var xPos, yPos;
    var prm = Sys.WebForms.PageRequestManager.getInstance();

        function BeginRequestHandler(sender, args) {

        if ($get('<%=lstAuctions.ClientID %>') != null) {

            xPos = $get('<%=lstAuctions.ClientID %>').scrollLeft;
            yPos = $get('<%=lstAuctions.ClientID %>').scrollTop;
        }
    }

    function EndRequestHandler(sender, args) {

        if ($get('<%=lstAuctions.ClientID %>') != null) {

            $get('<%=lstAuctions.ClientID %>').scrollLeft = xPos;
            $get('<%=lstAuctions.ClientID %>').scrollTop = yPos;
        }
    }

    prm.add_beginRequest(BeginRequestHandler);
    prm.add_endRequest(EndRequestHandler);

</script>

5 个答案:

答案 0 :(得分:7)

var xPos, yPos; // global variable declaration
var prm = Sys.WebForms.PageRequestManager.getInstance(); // Some webforms javascript manager

/*
* Begin function with 2 arguments
*/
function BeginRequestHandler(sender, args) {

    // check if the element generated by .net with id 'lstAuctions.ClientID' exists
    if ($get('<%=lstAuctions.ClientID %>') != null) {

        // get its scroll left and top position and
        // assign it to the global variables
        xPos = $get('<%=lstAuctions.ClientID %>').scrollLeft;
        yPos = $get('<%=lstAuctions.ClientID %>').scrollTop;
    }
}

/*
* this method gets executed last, it uses the 
* already set global variables to assign the old scrollpositions again
*/
function EndRequestHandler(sender, args) {

    if ($get('<%=lstAuctions.ClientID %>') != null) {
        // assign the previous scroll positions
        $get('<%=lstAuctions.ClientID %>').scrollLeft = xPos;
        $get('<%=lstAuctions.ClientID %>').scrollTop = yPos;
    }
}

// first function gets executed on the beginning of a request
prm.add_beginRequest(BeginRequestHandler);
// second function gets executed on the end of the request
prm.add_endRequest(EndRequestHandler);

答案 1 :(得分:4)

Sys.WebForms.PageRequestManager是一个ASP.Net AJAX构造。

特别是在您的代码中,有一些分配的变量(xPos,yPos,prm)和两个定义的函数(BeginRequestHandler,EndRequestHandler)。在代码的末尾有两个函数调用(prm.add_beginRequest,prm.add_endRequest),它们将这些函数分配为事件处理程序。

$ get调用是库的一部分,作为从客户端获取数据的快捷方式。这是非常多的javascript,但它只是通过ASP.Net AJAX客户端库的语法实现。

答案 2 :(得分:2)

你问过......

// declare 2 variables
var xPos, yPos;
// get an instance of the PageRequestManager - this looks like an MS ajax helper class
var prm = Sys.WebForms.PageRequestManager.getInstance();

// declare a function
function BeginRequestHandler(sender, args) {

    // get the ClientSide HTML DOM element which corresponds to the lstAuctions asp control on the serverside
    if ($get('<%=lstAuctions.ClientID %>') != null) {
        // if the element is not null (eg: page is not broken)

        // get the x Position of the object relative to what is displayed by the scrolled window (if you scroll sideways this value changes)
        xPos = $get('<%=lstAuctions.ClientID %>').scrollLeft;
        // get the y Position of the object relative to what is displayed by the scrolled window (if you scroll up/down this value changes)
        yPos = $get('<%=lstAuctions.ClientID %>').scrollTop;
    }
}

// declare a function
function EndRequestHandler(sender, args) {

    // get the ClientSide HTML DOM element which corresponds to the lstAuctions asp control on the serverside
    if ($get('<%=lstAuctions.ClientID %>') != null) {
        // if the element is not null (eg: page is not broken)

        // set the x position of the object to what we got last time (horizontal scroll the page)
        $get('<%=lstAuctions.ClientID %>').scrollLeft = xPos;

        // set the y position of the object to what we got last time (vertical scroll the page)
        $get('<%=lstAuctions.ClientID %>').scrollTop = yPos;
    }
}

// tell the page request manager to call our BeginRequestHandler method when it begins it's request
prm.add_beginRequest(BeginRequestHandler);

// tell the page request manager to call our EndRequestHandler method when it ends it's request
prm.add_endRequest(EndRequestHandler);

基本上,看起来页面使用MS ajax库来显示一些动态内容(可能用另一个列表替换列表),但是保留用户滚动到的位置,这样页面就不会“跳转”当新内容替换旧内容时。

答案 3 :(得分:1)

答案 4 :(得分:1)

var xPos,yPos;

**声明两个全局变量。


函数BeginRequestHandler(sender,args){

**声明一个新功能。此函数可能用于事件处理程序

if($ get('&lt;%= lstAuctions.ClientID%&gt;')!= null){

**这是&lt; %%&gt;中定义的内联ASP / ASP.NET代码的组合。配对。

xPos = $ get('&lt;%= lstAuctions.ClientID%&gt;')。scrollLeft;

**将页面的当前滚动位置捕获到局部变量中。

yPos = $ get('&lt;%= lstAuctions.ClientID%&gt;')。scrollTop;

**将页面的当前滚动位置捕获到局部变量中。


function EndRequestHandler(sender,args){

**声明一个新功能。此函数可能用于事件处理程序

if($ get('&lt;%= lstAuctions.ClientID%&gt;')!= null){

**这是&lt; %%&gt;中定义的内联ASP / ASP.NET代码的组合。配对。

$ get('&lt;%= lstAuctions.ClientID%&gt;')。scrollLeft = xPos;

**将页面的滚动位置设置为xPos的值。

$ get('&lt;%= lstAuctions.ClientID%&gt;')。scrollTop = yPos;

**将页面的滚动位置设置为xPos的值。


var prm = Sys.WebForms.PageRequestManager.getInstance();

**声明并初始化一个新的变量到PageRequestManager。

prm.add_beginRequest(BeginRequestHandler);

**将上面定义的事件处理程序添加到当前页面的beginRequest。

prm.add_endRequest(EndRequestHandler);

**将上面定义的事件处理程序添加到当前页面的endRequest。