使用JQuery的全屏背景图片,自动适应各种屏幕和浏览器
2013-08-19 15:07
681 查看
在你的HTML文档加载jQuery库直接从谷歌的CDN
上面的代码可以插入您的文档头内部的标签,或在最底层,收盘前体标签(通常是建议,以获得更好的性能)。
添加背景图片的标记
更改源属性图像name.jpg映像路径和设置它的实际宽度和高度属性。
图像容器的CSS:
接下来,我们需要插入一切工作的中心STRECHING proportionaly我们的形象,它的??实际功能。所以在jQuery的夹杂物插入:
此功能使图像的宽度和高度伸展,这取决于浏览器和图像尺寸比例。
最后,插入的功能,来检查时,我们的页面加载和浏览器的大小:
<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>
相关文章推荐
- 背景图片自适应浏览器分辨率大小并自动拉伸全屏代码
- 设置背景图片自动适应屏幕
- asp.net背景图片自动适应屏幕大小
- 用css、如何让图片自动适应屏幕大小,不出现滚动条,不变形,兼容各个浏览器
- 背景图片自适应分辨率浏览器大小自动拉伸全屏代码
- 背景图片自适应分辨率浏览器大小自动拉伸全屏代码
- 背景图片自适应分辨率浏览器大小自动拉伸全屏代码
- cocos2d-x 3.X中Sprite背景图片自动拉伸适应屏幕
- 使用jQuery自动缩图片
- Raspberry Desktop 用户自动登录设置,屏幕禁止休眠设置,浏览器全屏设置
- jquery后台登录页面背景图片自动轮换登录界面代码
- 背景图片适应屏幕百分百
- JQuery自动轮播效果,带字幕说明,带底部滚动圈圈,兼容IE7以上所有主流浏览器。(魔兽背景哟亲)
- Javascript、Jquery获取浏览器和屏幕各种高度宽度
- jquery实现根据浏览器窗口大小自动缩放图片的方法
- 背景图片自适应浏览器大小
- 网页背景图片自动适应窗口大小的代码
- body背景图片自适应浏览器高度
- Android中使用XML自定义组件各种状态下的背景图片
- 通过jquery使图片自动适应缩放到指定尺寸