功能:背景图片始终与窗口大小一致。
主要代码下:
(function($) {
$.fn.fullBg = function(){
var bgImg = $(this); function resizeImg() {
var imgwidth = bgImg.width();
var imgheight = bgImg.height(); var winwidth = $(window).width();
var winheight = $(window).height(); var widthratio = winwidth / imgwidth;
var heightratio = winheight / imgheight; var widthdiff = heightratio * imgwidth;
var heightdiff = widthratio * imgheight; if(heightdiff>winheight) {
bgImg.css({
width: winwidth+'px',
height: heightdiff+'px'
});
} else {
bgImg.css({
width: widthdiff+'px',
height: winheight+'px'
});
}
}
resizeImg();
$(window).resize(function() {
resizeImg();//当窗口大小变化时改变图片的大小
});
};
})(jQuery)
主要结构:
<img src="your-background-image.jpg" alt="" id="background" />
<div id="maincontent">
<!-- Your site content goes here -->
</div>
相关样式:
.fullBg {
position: absolute;
top: 0;
left: 0;
overflow: hidden;
width: 100%;
} #maincontent {
position: absolute;
top: 0;
left: 0;
z-index: 50;
}
DEMO:http://www.wfuyu.com/a/Demo/2010/jQuery-fullback.html
英文演示:http://bavotasan.com/demos/fullbg/