程序员人生 网站导航

jQuery插件:全背景图插件(Full Size Background Image)

栏目:jquery时间:2013-11-12 17:17:30

功能:背景图片始终与窗口大小一致。

fullbg Full Size Background Image jQuery Plugin

主要代码下:

(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/

作者:Rady Huang
出处:http://www.radys.cn/article.asp?id=263
------分隔线----------------------------
------分隔线----------------------------

最新技术推荐