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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:41:28+00:00 2026-06-04T12:41:28+00:00

First post. I’ve searched high and low for similar questions and have come up

  • 0

First post. I’ve searched high and low for similar questions and have come up empty handed, so here goes.

I am currently using small bit of jQuery to proportionally scale a collection of images for display in an image carousel (thanks to SO users for much of this code). The carousel is powered by the jCarousel Lite plugin (here). I have 4 different image sets, which are selected using a navigation menu. When a new image set is selected, the following jQuery resizing code executes on each image in the set, and then the image carousel is initialized using the freshly resized images within the #slider div.

The problem: this code only works sometimes, seemingly randomly. When the slider initializes, if the images are not scaled successfully, all widths are increased to maximum width, thus cropping the bottom of the image. This problem is especially apparent for portrait-oriented images, as the slider is shaped for landscape-oriented images (900px x 600px). I cannot pinpoint what causes the code to work successfully or not, regardless of order or clicks, window size, or browser.

Any ideas why this code would randomly execute successfully?

//The following scales images proportionally to fit within the #slider div
$('#slider ul li img').each(function() {
    var maxWidth = $('#slider').outerWidth(false); // Max width for the image
    var maxHeight = $('#slider').outerHeight(false);    // Max height for the image
    var ratio = 0;  // Used for aspect ratio
    var width = $(this).width();    // Current image width
    var height = $(this).height();  // Current image height

// Check if the current width is larger than the max
if(width > maxWidth){
    ratio = maxWidth / width;   // Ratio for scaling image
    $(this).css("width", maxWidth); // Set new width
    $(this).css("height", height * ratio);  // Scale height based on ratio
    height = height * ratio;    // Reset height to match scaled image
}

var width = $(this).width();    // Current image width
var height = $(this).height();  // Current image height

// Check if current height is larger than max
if(height > maxHeight){
    ratio = maxHeight / height; // Ratio for scaling image
    $(this).css("height", maxHeight);   // Set new height
    $(this).css("width", width * ratio);    // Scale width based on ratio
    width = width * ratio;    // Reset width to match scaled image
}
});
initialize(); //initializes the jCarousel Lite carousel
  • 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-04T12:41:29+00:00Added an answer on June 4, 2026 at 12:41 pm

    Have you tried running it on window load jQuery(window).load(function() {

    This will help ensure that all of the images are available before checking their dimensions

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

Sidebar

Related Questions

first post here, I come in peace :) I've searched but can't quite find
First post, have found many answers here, so hopes are high. The problem: Google
My first post here :) I have a summer job doing a bit of
First post, but long time browser :) So here's my problem: Basically I have
First post here, sorry for starting with asking questions. In my Windows Phone 7
First post here, I have tried searching but couldn't find what I'm looking for
First post here! Ok.. I have a contact XML as follows: <contact> <item> <ContactData
First post, so here goes. I'm writing a script that does intelligent search and
First post here and it's and awk question. I have a file that looks
first post here as I'm stuck with my wonderful C++ function. The error I'm

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.