国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > web前端 > jquery > jQuery插件:全背景图插件(Full Size Background Image)

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

来源:程序员人生   发布时间:2013-11-12 17:17:30 阅读次数:2868次

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

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
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生