1

I am using the Scrolling Parallax jQuery plugin to scroll the "background" image (not actually a css background-image) on this site.

Is it possible to maintain the image aspect ratio, i.e. not squash the images, and still have it work in all modern browsers?

I tried setting bgHeight : 'auto' which sets the image css to height:auto, but this stops the scrolling effect working in Chrome and Safari.

I also tried setting bgWidth : 'auto' but then the image is narrower than the browser window.

Also open to other plugins or ways to implement this effect, i.e. having the background image scroll at a different rate to the page content, and also to show the entire image, but not scroll past it...

Thanks in advance for any help!

3
  • What if you put the image in body background & use background-size:cover; css-tricks.com/perfect-full-page-background-image Commented Jun 21, 2012 at 19:08
  • can you set % for width or height? Commented Jun 21, 2012 at 20:33
  • +1 for well written Question. Commented Jun 21, 2012 at 22:41

2 Answers 2

1

Because the width is automatically set to be 100% of the viewport, stretching will occur unless you change that.

Use this:

$.scrollingParallax('img/clouds.png', {
    bgHeight : '250%',
    staticSpeed : .25,
    staticScrollLimit : false,
    bgWidth: 'auto'
});

The above can be seen in action here: jsFiddle


Since a non-stretched background image is always floating left, I whipped up some jQuery goodness so the image is centered in the viewport at all times, even during Window Resize Event. That said, the image will now scale up to the maximum image size or shrink while preserving Aspect Ratio in all browsers.

$(function() {

    // Specify your background image here.
    var bgMain = 'http://indigobrazilianportuguese.com/2012/wp-content/uploads/Home_bg1600.jpg';

    $.scrollingParallax(bgMain, {
        bgHeight : '200%',
        staticSpeed : 0.25,
        staticScrollLimit : false,
        // Important to set to 'auto' so Aspect Ratio for width is preserved because height defined above is fixed.
        bgWidth: 'auto'
    });

    // These two lines is for page load.
    // The variable will calculate CSS 'left' for the background image to center it in the viewport.
    // First, horizontal viewport size is checked via $(window).width()
    // Then, image width is determined by searching for image's unique filepath/filename.
    // Once the different is known, this value is then divided by 2 so that equal space is seen on left and right side of image which becomes the variable value.
    var bgMainHcenter = ( $(window).width() - $('body img[src="' + bgMain + '"]').width() ) /2 ;
    $('body img[src="' + bgMain + '"]').css('left', bgMainHcenter + 'px');

    // Just like above, it's repeated during Window Resize Event.
    $(window).resize(function() {
        bgMainHcenter = ( $(window).width() - $('body img[src="' + bgMain + '"]').width() ) /2 ;
        $('body img[src="' + bgMain + '"]').css('left', bgMainHcenter + 'px');
    });

});

See it in action here: jsFiddle

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks arttronics, but as I mentioned in my question when I set the bgWidth to auto the image displays narrower than the browser window (even with bgHeight set to 200%)
Opps... I rushed to provide just a snippet answer without reading your full question which warranted a full answer. That said, I now provide complete example markup and jsFiddle that is cross-browser friendly. If you still have issue on your website, include bgWidth as shown above and I will look to see what's the matter.
Reminder: Always press the Run Button in jsFiddle after the page loads to insure the code is launched correctly. Thanks.
Awesome artronics! I just wrapped the first image centering function in $(window).load(function(){ to get it to work on my site indigobrazilianportuguese.com - is this the best practice?
If the image would not load otherwise then the method you choose is sound and is equivalent to non-jQuery window.onload=function(){};. That said, try using window.onload method instead to see if images load faster? Two side tips. 1) 300% zoom makes image blurry and very little of original image is shown: Consider less percent and have unique background-color for when bars are exposed on the sides. 2) Click jsLint to see that many semicolons are missing in your markup.
0

ok so according to their homepage you can set % for width and height so I would try that first and see how it goes.

1 Comment

Thanks Haungism. I tried % first, but then that loses the image aspect ratio when the browser window is resized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.