Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 885579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:55:58+00:00 2026-05-15T12:55:58+00:00

Short version: I’m extending a jQuery object function that is intended to take 0-3

  • 0

Short version: I’m extending a jQuery object function that is intended to take 0-3 arguments and pass them off as the second to fourth arguments into jQuery.animate. Except, I want to isolate the callback and put in another function call at the end of it.


I’m extending the jQuery library the ordinary way, through jQuery.fn.newfunction = .... It’s a custom animation function, specifically one that expands an object so that its outerwidth including margins equals the inner width of its parent node.

jQuery.fn.expand_to_parent_container = function() {
  var args = Array.prototype.slice.call(arguments);

  return this.each(function(){
    var parent = $(this).parent();
    parent.inner_width = parent.innerWidth();

    var inner_outer_differential = $(this).outerWidth(true) - $(this).width();

    // store original width for reversion later
    $(this).data('original_width', $(this).width());

    args.unshift({ width: parent.inner_width - inner_outer_differential });

    $.fn.animate.apply($(this), args );
  });
}

Simple enough. I’m using the arguments local variable, “casting” it as an array, and unshifting the first argument, which will go in the properties slot of .animate (API here). The intention, therefore, is to allow .expand_to_parent_container to take the arguments duration, easing, callback, with the usual jQuery convention of making them optional in intuitive patterns (e.g., if only a function is passed, it becomes the callback). It appears as though jQuery.speed is responsible for figuring out this mess, but I’d rather not use it/tamper with it directly because it does not appear to be as public/deprecation-proof as the other jQuery functions, and because I’d rather just delegate the task to .animate.

Here’s my reversion function.

jQuery.fn.contract_to_original_size = function() {
  var args = Array.prototype.slice.call(arguments);

  var callback_with_width_reset = function(){
    callback();
    $(this).width('auto');
  }

  args.unshift({ width: $(this).data('original_width') });

  if($(this).data('original_width')) {
    $.fn.animate.apply($(this), args );
  } else {
    $(this).width('auto');
  }
}

I make my desired behaviour clear in var callback_with_width_reset ... assignment, in that I’d like for the width property to be reset to auto, both to restore it closer to its original state and to recover from any craziness that might happen over the course of lots of animations. However, I don’t know how to piece out the callback function from arguments or args.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:55:58+00:00Added an answer on May 15, 2026 at 12:55 pm

    There are a few things I’d like to point out real quick:

    • If you call expand_to_parent_container on multiple objects at once, you keep unshifting {width: ...} objects into args and never shift them back off.
    • contract_to_original_size doesn’t use this.each() nor does it return this, and it will suffer from the same problems with unshifting width objects over and over.
    • You can make sure your code only sets original-width if it isn’t already set, stopping one of your problems here (starting an expand while expanded / whatever wont mess with it! )
    • You can use $.map() on your arguments array to detect and replace your callback function pretty easily, if you still feel you need to.

    Here is the code adjusted a little:

    jQuery.fn.expand_to_parent_container = function() {
      // instead of Array.prototype, we can grab slice from an empty array []
      var args = [].slice.call(arguments);
    
      return this.each(function(){
        var parent = $(this).parent();
        parent.inner_width = parent.innerWidth();
    
        var inner_outer_differential = $(this).outerWidth(true) - $(this).width();
        // store original width for reversion later
        if (!$(this).data('original_width'))
        {
          $(this).data('original_width', $(this).width());      
        }
    
        args.unshift({ width: parent.inner_width - inner_outer_differential });
        $.fn.animate.apply($(this), args );
        args.shift(); // push the width object back off in case we have multiples
      });
    }
    
    jQuery.fn.contract_to_original_size = function() {
      var args = Array.prototype.slice.call(arguments);
      var callback;
    
      var callback_with_width_reset = function(){
        if (callback) callback.apply(this,arguments);
        // do something else like:
        // $(this).width('auto');
        // you may want to $(this).data('original-width', null); here as well?
      }
    
      args = $.map(args, function(param) {
        // if the parameter is a function, replace it with our callback
        if ($.isFunction(param)) {
          // and save the original callback to be called.
          callback = param;
          return callback_with_width_reset;
        }
        // don't change the other params
        return param;
      });
    
      // we didn't find a callback already in the arguments
      if (!callback) { 
        args.push(callback_with_width_reset);
      }
    
    
      return this.each(function() {
        // rather than shift() the new value off after each loop
        // this is another technique you could use:
    
        // create another copy so we can mess with it and keep our template
        var localArgs = [].slice.call(args); 
        // also - storing $(this) is going to save a few function calls here:
        var $this = $(this);
    
        if($this.data('original_width')) {
          localArgs.unshift({ width: $this.data('original_width') });
          $.fn.animate.apply($this, localArgs );
        } else {
          $this.width('auto');
        }    
      });
    }
    

    Check it out in action on jsFiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In phadej's answer the transformation between the old and new… May 16, 2026 at 11:51 am
  • Editorial Team
    Editorial Team added an answer Make your own database class that extends mysqli, and does… May 16, 2026 at 11:51 am
  • Editorial Team
    Editorial Team added an answer Can I install WebDav on Linux ? You can do… May 16, 2026 at 11:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Short Version I want an ADPlus script that will do a full memory dump
Short version: I want to trigger the Form_Load() event without making the form visible.
Short version: I'm wondering if it's possible, and how best, to utilise CPU specific
Short version: assuming I don't want to keep the data for long, how do
Short Version: When I've created a Channel using ChannelFactory on a client which uses
Short version: What is the cleanest and most maintainable technique for consistant presentation and
Short version: Is there a simple, built-in way to identify the calling view in
Short version : echo testing | vim - | grep good This doesn't work
Short version: I have a similar setup to StackOverflow. Users get Achievements. I have
Short version: Is it easy/feasible/possible to program modal window in Flash (AS3)? Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.