简体   繁体   English

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

[英]New to Javascript, can someone explain this code line by line?

I used this code to maintain scroll position and don't have a clue of what it means. 我使用此代码来保持滚动位置,并且不知道它的含义。 If someone has the time, can you provide me with an step by step explanation of what it is doing. 如果有人有时间,你能否为我提供一步一步解释它在做什么。 Here it is: 这里是:

<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>
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);

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

Specifically in your code, there are some allocated variables (xPos,yPos,prm) and two defined functions (BeginRequestHandler,EndRequestHandler). 特别是在您的代码中,有一些已分配的变量(xPos,yPos,prm)和两个已定义的函数(BeginRequestHandler,EndRequestHandler)。 At the end of the code are two function calls (prm.add_beginRequest,prm.add_endRequest) that are assigning those functions as event handlers. 在代码的末尾是两个函数调用(prm.add_beginRequest,prm.add_endRequest),它们将这些函数分配为事件处理程序。

The $get calls are part of the library as a shortcut for getting data from the client-side. $ get调用是库的一部分,作为从客户端获取数据的快捷方式。 It's very much javascript under the covers, but it's just a syntactical implementation through the ASP.Net AJAX client-side library. 这是非常多的javascript,但它只是通过ASP.Net AJAX客户端库的语法实现。

You did ask... 你确实问过......

// 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);

Basically, it looks like the page is using the MS ajax library to display some dynamic content (probably replacing a list with another list), but preserving the place that the user has scrolled to so the page doesn't "jump" when the new content replaces the old content. 基本上,看起来页面使用MS ajax库来显示一些动态内容(可能用另一个列表替换列表),但保留用户滚动到的位置,以便页面在新的时候不会“跳转”内容替换旧内容。

var xPos, yPos; var xPos,yPos;

**declares two global variables. **声明两个全局变量。


function BeginRequestHandler(sender, args) { function BeginRequestHandler(sender,args){

**declares a new function. **声明一个新功能。 This function is probably used for an event handler 此函数可能用于事件处理程序

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

**this is a combination of inline ASP/ASP.NET code as defined in the <% %> pairing. **这是<%%>配对中定义的内联ASP / ASP.NET代码的组合。

xPos = $get('<%=lstAuctions.ClientID %>').scrollLeft; xPos = $ get('<%= lstAuctions.ClientID%>')。scrollLeft;

**captures the current scrolling position of the page into the local variable. **将页面的当前滚动位置捕获到局部变量中。

yPos = $get('<%=lstAuctions.ClientID %>').scrollTop; yPos = $ get('<%= lstAuctions.ClientID%>')。scrollTop;

**captures the current scrolling position of the page into the local variable. **将页面的当前滚动位置捕获到局部变量中。


function EndRequestHandler(sender, args) { function EndRequestHandler(sender,args){

**declares a new function. **声明一个新功能。 This function is probably used for an event handler 此函数可能用于事件处理程序

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

**this is a combination of inline ASP/ASP.NET code as defined in the <% %> pairing. **这是<%%>配对中定义的内联ASP / ASP.NET代码的组合。

$get('<%=lstAuctions.ClientID %>').scrollLeft = xPos; $ get('<%= lstAuctions.ClientID%>')。scrollLeft = xPos;

**sets the scrolling position of the page to the value of xPos. **将页面的滚动位置设置为xPos的值。

$get('<%=lstAuctions.ClientID %>').scrollTop = yPos; $ get('<%= lstAuctions.ClientID%>')。scrollTop = yPos;

**sets the scrolling position of the page to the value of xPos. **将页面的滚动位置设置为xPos的值。


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

**declares and initializes a new variable to the PageRequestManager. **向PageRequestManager声明并初始化一个新变量。

prm.add_beginRequest(BeginRequestHandler); prm.add_beginRequest(BeginRequestHandler);

**adds the event handler defined above to the beginRequest of the current page. **将上面定义的事件处理程序添加到当前页面的beginRequest。

prm.add_endRequest(EndRequestHandler); prm.add_endRequest(EndRequestHandler);

**adds the event handler defined above to the endRequest of the current page. **将上面定义的事件处理程序添加到当前页面的endRequest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM