因为宽度自动设置为视口的 100%,所以除非你改变它,否则会发生拉伸。
用这个:
$.scrollingParallax('img/clouds.png', {
bgHeight : '250%',
staticSpeed : .25,
staticScrollLimit : false,
bgWidth: 'auto'
});
以上可以在这里看到:jsFiddle
由于未拉伸的背景图像始终向左浮动,因此我使用了一些 jQuery 优点,因此图像始终位于视口中心,即使在Window Resize Event期间也是如此。也就是说,图像现在将放大到最大图像大小或缩小,同时在所有浏览器中保留纵横比。
$(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');
});
});
在这里查看它的实际效果: jsFiddle