Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS background-image replacement removes background

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #32322
    joebob
    Member

    Hi! I am using a full-screen photo as a background. My goal was to set a low res version initially then load a high res version after the page had finished loading. The best way I found to accomplish this was using jQuery but when I set the css attribute the initial low res image was removed (white bg) until the high res version loaded. I spent a long time trying to figure out how to resolve this and it is now working so I thought I’d post what I found and see if anyone has any better method.


    html {
    height: auto;

    /*
    color/transparent didn't matter
    */
    background-color: white;
    background-position: center center;
    background-attachment: fixed;
    background-repeat: no-repeat;

    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;

    background-image: url(../images/bg-low.jpg);
    }
    .bgHighRes {
    /*
    this is what I had initially, so the fix is the next line:
    background-image: url(../images/bg.jpg);
    */
    background-image: url(../images/bg.jpg),url(../images/bg-low.jpg);
    }

    $(window).bind("load",function(){
    $("html").addClass("bgHighRes")
    })
    #51507
    Chris Coyier
    Keymaster

    You might have to do something fancy like inject the high res onto the page as an img, just hidden with CSS. Then watch for that img’s load event, and then replace the background image, that way it’ll snag the image from cache and go faster.

    #51517
    joebob
    Member

    Thanks for the follow-up, Chris. Do you think that’s easier than this solution? Seems that putting both the initial low res and high res backgrounds on the addClass solves this pretty simply. With your response and none from anyone else I must be on to something.

    #51518
    joebob
    Member

    Or do you have a better method of progressively loading the sites background image after the content has loaded?

    #84275
    jtrinker
    Participant

    Hi joebob,

    I’m trying to replace a low-resolution full screen background image with a high-resolution image after the high res image finishes loading. I found your code snippet but it doesn’t seemto be working for me.




    ****BACKGROUND IMAGE TRANSITION TEST****








    *This is a test*






    And then the jquery:

    $(window).bind("load",function(){
    $("html").addClass("bgHighRes")
    });

    When I test it only the low-res image loads. It seems like the jquery is loading the .bgHighRes css class but not the image. Any ideas?

    Thank you!

    #84276

    @jtrinker you must put the 2 .js scripts one after the other and before the style tag: first include jquery library and after that , include the other script !



    Also, take a close look at Chris’s post!
    Cheers!

    #84277
    jtrinker
    Participant

    Thank you! That fixed it. I appreciate you looking at it.

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘CSS’ is closed to new topics and replies.