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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:59:37+00:00 2026-05-25T15:59:37+00:00

I am trying to make a JQuery script in order to change the size

  • 0

I am trying to make a JQuery script in order to change the size of some images in a portfolio. Some images are larger horizontally and some others vertically, so my idea is just to display the center part as a thumbnail. And if you click on it, you could see the real image (with an external slider ok?)

So the little script I’ve done is the following:

$(document).ready(function(){

    var w_div = $('#portfolio div').width();
    var h_div = $('#portfolio div').height();

    $('#portfolio div a img').each(function() {

        /* Correction estimate: div.width - photo.width / 2 */
        var posLeft = -(w_div - $(this).width())  / 2;
        var posTop  = -(h_div - $(this).height()) / 2;

        /* Assignment of new measures to the current image */
        $(this).css({left: posLeft+'px', top: posTop+'px'});

    });

});

With the loop I try to take the img group and iterate over it to calculate the correct center position. But I don’t know why it only works for the first image. I mean, this script change the css of all the images with the correct measures for the first image only. Why the assignments o posLeft and posTop doesn’t change? I don’t understand it…

(I forget to say that every image has the ‘position: relative’ sentence on the css file ok?

Thanks!

edit: Could be easier and better idea to use PHP and executing this task on the server side?

  • 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-25T15:59:38+00:00Added an answer on May 25, 2026 at 3:59 pm

    I read this too quickly. So I’m heavily editing my answer.

    1. $(document).ready() runs as soon as the DOM is rendered. The images aren’t loaded yet and the only information the DOM has about them so far is what is in the img attributes. This is also true of the container div, which hasn’t stretched its height yet to accomodate the images. That’s probably why you get the same dimensions every time. Ideally you should manually set width and height of the images as well as the container div. If you don’t want to do that, you can use $(window).load() which will wait for all of the images to load and the DOM to be re-rendered, but this is very obvious to see.

    2. ‘left’ and ‘top’ only work on elements that are ‘position: absolute;’ and within a container with ‘position: relative;’. So the images need to be absolute and the div above them relative for left and top to work.

    3. I’m not sure why you’re using left and top instead of width and height, since you’re trying to make a thumbnail. Another way is to calculate the maximum of the width and height and scale it down to a desired width or height.

      var max_width = 20; var max_height = 20;
      $('#portfolio div a img').each(function() {
          var w = $(this).width();
          var h = $(this).height();
          var scale = null;
          if (w >= h) { if (w > max_width) { scale = 1 / (w / max_width); } } 
          else { if (h > max_height) { scale = 1 / (h / max_height); } }
          if (scale) {
              $(this).width(w * scale);
              $(this).height(h * scale);
          }
      });
      

    I hope some of this clarifies enough to let you work a proper solution out.

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

Sidebar

Related Questions

I am trying to use this jQuery script to make an image caption from
I'm trying to make a simple JQuery button from a div which will change
I am new to programming especially jQuery. I am trying to sort (order) images
I'm trying to make a scrolling calendar script using jquery (similar to the one
i'm trying to make some change in the following code which updates the number
I'm trying to make a JQuery accordion which will change it's style to .active{}
I am trying to use jQuery in order to make a pager for tumbs
I am trying to generate a jquery script to make all tr elements from
I am trying to make a jquery menu that when I click on one
I'm trying to figure out how to make jQuery slide #content2 down and replace

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.