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 8219081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:02:05+00:00 2026-06-07T13:02:05+00:00

To better learn jquery I decided to write a plugin that creates a gallery

  • 0

To better learn jquery I decided to write a plugin that creates a gallery collage effect like google+. Here’s an example.

I want to trigger it again on resizing the html element that contains the images. Part of the problem I’m having is that I need to store the original image size in order to recalculate the image sizes to make them fit.

I don’t know where to store and how to retrieve those original image sizes. Full plugin is linked above, but I’ll put a summary here.

;(function( $ ) {
    $.fn.collagePlus = function( options ) {

        var settings = $.extend( 
            //... 
            'images'          : $('img', $(this))
            //... 
        );

        return this.each(function() {
            settings.images.each(function(index){
                //... 

                /*
                * get the current image size
                */
                var w = (typeof $(this).data().width != 'undefined') ? $(this).data().width : $(this).width();
                var h = (typeof $(this).data().height != 'undefined') ? $(this).data().height : $(this).height();

                /*
                * store the original size for resize events
                */
                $(this).attr( "data-width" , w  );
                $(this).attr( "data-height" , h  ); 
                //... Do some other stuff
                }
            );
        });
    }
})( jQuery );
  • 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-06-07T13:02:09+00:00Added an answer on June 7, 2026 at 1:02 pm

    You’re using .data() wrong. When you pass 1 parameter to the .data function, it returns the value for the given key. When you pass 2 parameters, .data will set the value for that key.

    This block:

    //get the current image size
    var w = (typeof $(this).data().width != 'undefined') ? $(this).data().width : $(this).width();
    var h = (typeof $(this).data().height != 'undefined') ? $(this).data().height : $(this).height();
    

    Should be:

    var $this = $(this); //caching your selector
    if (!$this.data('width')) //if this element doesn't have a width stored
        $this.data('width', $this.width()); //stores currently computed width
    if (!$this.data('height')) //repeat
        $this.data('height', $this.height());
    

    And of course, to retrieve the data later:

    alert($this.data('width')) //alerts currently stored width
    

    Fiddle Demo

    You can also store an object in the .data passing a map of properties:

    if (!$(this).data('size'))
        $(this).data('size', { width: $(this).width(), height: $(this).height() });
    

    Now width and height are properties of the object stored in the .data('size'), which can retrieved with:

    alert($(this).data('size').width);
    

    Fiddle

    For the sake of simplicity, I’d primarily go with the first option. However the second one looks tidier. Choose whichever you find more readable and maintainable.

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

Sidebar

Related Questions

I'm trying to learn Rails better by looking at example applications, and while looking
I've decided to learn prolog. What's a good interpreter to use? Even better if
In order to better learn both Cocoa and Core Data, I decided to build
I have some javascript that I'm trying to retool using jQuery to learn the
Which is better, learn the basics of JavaScript, and then jQuery Or master JavaScript
I live and breathe jQuery, but I also love finding an excellent plugin that
I am working on a very simple project to learn Rails better, coming from
I am a newbie in Obj-C. I need to learn this better so please
I am trying to learn MySQL select statements a little better and I have
I am trying to learn Subtyping in Java and I am not an better

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.