[转载]完美的背景图全屏代码
2015-08-23 19:27
330 查看
具有以下特点:
填满整个页面,没有空白空间
能缩放图像
保留图像尺寸比例
图像居中
不因图像原因产生滚动条
更多地支持全部浏览器
不使用类似FLash的东西
Safari 3+
Chrome any+
IE 9 +
Opera 10+ (Opera 9.5 supported background-size but not the keywords)
Firefox 3.6+ (Firefox 4 supports non-vendor prefixed version)
查看Demo
支持
Any version of good browsers: Safari / Chrome / Opera / Firefox
IE 6: Borked - but probably fixable if you use some kind of fixed positioning shim
IE 7/8: Mostly works, doesn't center at small sizes but fills screen fine
IE 9: Works
注:使用DIV应该是为了能够使图片居中,单独img达不到
支持
Safari / Chrome / Firefox (didn't test very far back, but recent versions are fine)
IE 8+
Opera (any version) and IE both fail in the same way (wrongly positioned, not sure why)
如果通过Js添加的图片,需要添加 {width: auto; and height: auto;}以便支持 in IE 8, 9, or 10.
转载自:https://css-tricks.com/perfect-full-page-background-image/
填满整个页面,没有空白空间
能缩放图像
保留图像尺寸比例
图像居中
不因图像原因产生滚动条
更多地支持全部浏览器
不使用类似FLash的东西
帅气,简单,先进的css3方法
CSS
html { background: url(images/bg.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; }
支持
Safari 3+
Chrome any+
IE 9 +
Opera 10+ (Opera 9.5 supported background-size but not the keywords)
Firefox 3.6+ (Firefox 4 supports non-vendor prefixed version)
查看Demo
CSS-Only Technique #1
CSS
img.bg { /* Set rules to fill background */ min-height: 100%; min-width: 1024px; /* Set up proportionate scaling */ width: 100%; height: auto; /* Set up positioning */ position: fixed; top: 0; left: 0; } @media screen and (max-width: 1024px) { /* Specific to this particular image */ img.bg { left: 50%; margin-left: -512px; /* 50% */ } }
支持
Any version of good browsers: Safari / Chrome / Opera / Firefox
IE 6: Borked - but probably fixable if you use some kind of fixed positioning shim
IE 7/8: Mostly works, doesn't center at small sizes but fills screen fine
IE 9: Works
CSS-Only Technique #2
HTML
<div id="bg"> <img src="images/bg.jpg" alt=""> </div>
CSS
#bg { position: fixed; top: -50%; left: -50%; width: 200%; height: 200%; } #bg img { position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; min-width: 50%; min-height: 50%; }
注:使用DIV应该是为了能够使图片居中,单独img达不到
JQuery
HTML
<img src="images/bg.jpg" id="bg" alt="">
CSS
#bg { position: fixed; top: 0; left: 0; } .bgwidth { width: 100%; } .bgheight { height: 100%; }
Javascript代码
$(window).load(function() { var theWindow = $(window), $bg = $("#bg"), aspectRatio = $bg.width() / $bg.height(); function resizeBg() { if ( (theWindow.width() / theWindow.height()) < aspectRatio ) { $bg .removeClass() .addClass('bgheight'); } else { $bg .removeClass() .addClass('bgwidth'); } } theWindow.resize(resizeBg).trigger("resize"); });
支持
Safari / Chrome / Firefox (didn't test very far back, but recent versions are fine)
IE 8+
Opera (any version) and IE both fail in the same way (wrongly positioned, not sure why)
如果通过Js添加的图片,需要添加 {width: auto; and height: auto;}以便支持 in IE 8, 9, or 10.
转载自:https://css-tricks.com/perfect-full-page-background-image/
相关文章推荐
- 编写高质量代码改善C#程序的157个建议——建议137:委托和事件类型应添加上级后缀
- java基础-面向对象-异常
- Python——configParser模块学习
- php大力力 [009节]php在百度文库的几个基础教程
- python 的日志logging模块学习
- Meclipse 10 设置 new 菜单
- 【英语】Bingo口语笔记(57) - 常见的口语弱读
- 《iPhone高级编程—使用Mono Touch和.NET/C#》
- UNIX环境高级编程(6):文件I/O(2)
- 第0010道练习题_Python生成验证码图片
- 【英语】Bingo口语笔记(56) - “令人失望”的表达
- C# 计算时间差 用timespan函数
- UNIX环境高级编程(5):文件I/O(1)
- 编写高质量代码改善C#程序的157个建议——建议136:优先使用后缀表示已有类型的新版本
- java类对象与byte数组的互相转换
- C语言基础知识之(五):break 和 continue 运用详解
- codecombat之边远地区的森林1-11关及地牢38关代码分享
- PHP 中this,self,parent的区别
- 【整理】MATLAB之常微分方程
- Java改变程序观感