Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Understand the code snippet in jQuery offset method

I am trying to studying the jQuery method of offset, as below:

    // offset() relates an element's border box to the document origin
    offset: function( options ) {

        // Preserve chaining for setter
        if ( arguments.length ) {
            return options === undefined ?
                this :
                this.each( function( i ) {
                    jQuery.offset.setOffset( this, options, i );
                } );
        }
...

I cannot understand the first paragraph very well. It seems that the method is to check if there are any arguments, and if yes, it will check options, if no options provided, just return the current object, if any options is defined, it will set offset(that is rather strange since this function is to get offset, not to set offset).

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

.offset can be used in two ways: to get the offset, or to set the offset. See the docs.

When called with no arguments, it will:

Description: Get the current coordinates of the first element in the set of matched elements, relative to the document.

Hence the check for arguments.length.

When called with an argument, it will:

.offset( coordinates )

Description: Set the current coordinates of every element in the set of matched elements, relative to the document.

Which is why the code does

if (arguments.length) {
  // set offset of matching elements and return
}
// get and return offset of first matching element
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!