使用JQuery的全屏背景图片,自动适应各种屏幕和浏览器

在你的HTML文档加载jQuery库直接从谷歌的CDN

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>

上面的代码可以插入您的文档头内部的标签,或在最底层,收盘前体标签(通常是建议,以获得更好的性能)。

添加背景图片的标记

 

<div id="bg">
    <img src="image-name.jpg" width="1280" height="800" id="bgimg" />
</div>
更改源属性图像name.jpg映像路径和设置它的实际宽度和高度属性。
 
图像容器的CSS:
body{margin:0px; padding:0px; background:#000;}
#bg{position:fixed; z-index:1; overflow:hidden;}

接下来,我们需要插入一切工作的中心STRECHING proportionaly我们的形象,它的??实际功能。所以在jQuery的夹杂物插入:

<script type="text/javascript">
function FullScreenBackground(theItem){
var winWidth=$(window).width();
var winHeight=$(window).height();
var imageWidth=$(theItem).width();
var imageHeight=$(theItem).height();
var picHeight = imageHeight / imageWidth;
var picWidth = imageWidth / imageHeight;
if ((winHeight / winWidth) < picHeight) {
$(theItem).css("width",winWidth);
$(theItem).css("height",picHeight*winWidth);
} else {
$(theItem).css("height",winHeight);
$(theItem).css("width",picWidth*winHeight);
};
$(theItem).css("margin-left",winWidth / 2 - $(theItem).width() / 2);
$(theItem).css("margin-top",winHeight / 2 - $(theItem).height() / 2);
}
</script>
此功能使图像的宽度和高度伸展,这取决于浏览器和图像尺寸比例。
 
最后,插入的功能,来检查时,我们的页面加载和浏览器的大小:
<script type="text/javascript">
window.onload = function () {
FullScreenBackground('#bgimg');
}
$(window).resize(function() {
FullScreenBackground('#bgimg');
});
</script>

 

 

转载于:https://www.cnblogs.com/shcolo/p/3267875.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值