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

  • Home
  • SEARCH
  • 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 6811687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:21:28+00:00 2026-05-26T20:21:28+00:00

I recently started a project were I wanted the background image to scale to

  • 0

I recently started a project were I wanted the background image to scale to the size of the window. I was able to make it work with the following code:

<script>
$(window).load(function() {    

        var theWindow        = $(window),
            $bg              = $("#bg"),
            aspectRatio      = $bg.width() / $bg.height();

        function resizeBg() {

                if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
                    $bg
                        .removeClass()
                        .addClass('bgheight');
                } else {
                    $bg
                        .removeClass()
                        .addClass('bgwidth');
                }

        }

        theWindow.resize(function() {
                resizeBg();
        }).trigger("resize");

});

</script>
</head>
<body>
    <img src="images/ij_background.jpg" id="bg" alt="">
</body>
</html>

The problem is that the background image will load the full size image, and then scale to the window size, which is a little ugly. I want the image to load, scale, and then be displayed with a fade. I became a little stuck since I’m a little new to jQuery. Here is what I have so far.

  • 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-26T20:21:29+00:00Added an answer on May 26, 2026 at 8:21 pm

    You can load the image hidden, resize it, and then fade it in. This approach requires that JavaScript be enabled on the client (about 98% of people have it enabled, but a noscript fallback for the other 2% might be worthwhile). It would look something like this (live copy):

    // jQuery
    jQuery(function($) {
      var theWindow        = $(window),
          aspectRatio,
          // Create img element in jQuery wrapper
          $bg              = $("<img>"),
          // Get the raw element
          bg               = $bg[0];
    
      // Hide the element, hook its load event, and set its `id` and `src`
      // Important: Hook the load event *before* setting `src`
      $bg.hide().load(bgLoad);
      bg.id = "bg";
      bg.src = "http://www.injurytimeshort.lanewaypictures.com/images/ij_background.jpg";
    
      // Append it to the body
      $bg.appendTo(document.body);
    
      // Resize on window resize
      theWindow.resize(resizeBg);
    
      function bgLoad() {
        // Now that the image is loaded, get its aspect ratio
        aspectRatio = $bg.width() / $bg.height();
    
        // Resize it
        resizeBg();
    
        // And fade it in
        $bg.fadeIn();
      }
    
      function resizeBg() {
    
          if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
              $bg
                  .removeClass()
                  .addClass('bgheight');
          } else {
              $bg
                  .removeClass()
                  .addClass('bgwidth');
          }
    
      }
    });
    

    Note that we’re no longer waiting for the overall window#load event to fire, we only care that the DOM is ready and that this specific image is finished loading.

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

Sidebar

Related Questions

At work, we recently started a project using CouchDB (a document-oriented database). I've been
I recently started work on a personal coding project using C++ and KDevelop. Although
I've recently started a new web development project for an embedded device and wanted
I have recently started on a project developing a window mobile/ce buisiness application. I
I recently started a small project in which I wanted to use zxing. I
I have recently started work on a project that has already been running for
I've recently started work on a new project using PHP5 and want to use
I've recently started work on a new project using EpiServer which I am totally
I currently have a recently started project in development with a 2010 target date
I have recently started a project in Ruby on Rails. I used to do

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.