固定网页背景图同时保持图片比例的思路代码


Posted in Javascript onAugust 15, 2013

提供一个背景图片策略:

1,背景图片固定

2,随窗口大小改变而改变大小

3,保持比例不变而缩放

支持浏览器:IE 6,7,8,9+ ,FF,Chrome

演示地址:http://www.einino.net/bg_image.html

<style> 
body{margin:0; padding:0;height:2000px; } 
#background_img{ 
top:expression(documentElement.scrollTop); /*we need this expression to fixed the top*/ 
} 
</style>

/** 
*@desc make a fixed background image. resize the image to fit the window size besides do not lost the image's proportion 
*@author EI Nino 
*2013/8/15 
*/ 
var imgBackground=function(_img_obj) 
{ 
this.img = _img_obj; 
this.init(); 
} 
imgBackground.prototype={ 
init:function(){ 
this.img.style.top="0"; 
this.img.style.left="0"; 
if(navigator.appVersion.indexOf('MSIE 6.0')>0){ 
this.img.style.position="absolute"; 
this.img.style.bottom="auto"; 
if(!document.body.style.backgroundImage){//esacpe the flash when scroll the window in IE 6 
document.body.style.backgroundImage="url(about:blank)"; 
document.body.style.backgroundAttachment="fixed"; 
} 
} 
else{ 
this.img.style.position="fixed"; 
} 
this.ra = this.img.width/this.img.height; 
this.resize(); 
this.BindEvent(); 
}, 
resize:function() { 
var self=this; 
if((document.documentElement.clientWidth /document.documentElement.clientHeight )>=self.ra) 
{ 
self.img.style.width=document.documentElement.clientWidth+"px"; 
self.img.style.height=""; 
} 
else{ 
self.img.style.width=""; 
self.img.style.height=document.documentElement.clientHeight +"px"; 
} 
}, 
GetStyle:function(_obj,_style){ 
var obj = _obj; 
return obj.currentStyle? obj.currentStyle[_style] :window.getComputedStyle(obj, null)[_style]; 
}, 
BindEvent:function(){ 
var self = this; 
$(window).resize(function(){//use jquery lib 
self.resize(); 
}); 
} 
}; 
new imgBackground(document.getElementById("background_img"));
Javascript 相关文章推荐
JQUERY 对象与DOM对象之两者相互间的转换
Apr 27 Javascript
JavaScript如何从listbox里同时删除多个项目
Oct 12 Javascript
js 弹出框只弹一次(二次修改之后的)
Nov 26 Javascript
chrome下img加载对height()的影响示例探讨
May 26 Javascript
复制网页内容,粘贴之后自动加上网址的实现方法(脚本之家特别整理)
Oct 16 Javascript
详解JavaScript对象和数组
Dec 03 Javascript
AngularJS中如何使用echart插件示例详解
Oct 26 Javascript
浅谈javascript中的 “ &amp;&amp; ” 和 “ || ”
Feb 02 Javascript
vue单页应用加百度统计代码(亲测有效)
Jan 31 Javascript
详解webpack打包第三方类库的正确姿势
Oct 20 Javascript
JS forEach跳出循环2种实现方法
Jun 24 Javascript
javascript实现搜索筛选功能实例代码
Nov 12 Javascript
jQuery UI 实现email输入提示实例
Aug 15 #Javascript
javascript中如何处理引号编码&amp;#034;
Aug 15 #Javascript
Jquery中val()表单取值赋值的实例代码
Aug 15 #Javascript
jquery ajax方式直接提交整个表单核心代码
Aug 15 #Javascript
js冒泡法和数组转换成字符串示例代码
Aug 14 #Javascript
js实现无需数据库的县级以上联动行政区域下拉控件
Aug 14 #Javascript
Extjs3.0 checkboxGroup 动态添加item实现思路
Aug 14 #Javascript
You might like
PHP 遍历XP文件夹下所有文件
2008/11/27 PHP
PHP逐行输出(ob_flush与flush的组合)
2012/02/04 PHP
javascript页面上使用动态时间具体实现
2014/03/18 Javascript
javascript定义变量时有var和没有var的区别探讨
2014/07/21 Javascript
JsRender for index循环索引用法详解
2014/10/31 Javascript
node.js中的fs.read方法使用说明
2014/12/17 Javascript
使用angular写一个hello world
2015/01/23 Javascript
JavaScript中Function()函数的使用教程
2015/06/04 Javascript
jQuery:unbind方法的使用详解
2017/08/14 jQuery
Vue实现数字输入框中分割手机号码的示例
2017/10/10 Javascript
浅谈React组件之性能优化
2018/03/02 Javascript
代码详解javascript模块加载器
2018/03/04 Javascript
vue插件开发之使用pdf.js实现手机端在线预览pdf文档的方法
2018/07/12 Javascript
详解Vue中watch的详细用法
2018/11/28 Javascript
微信小程序与公众号实现数据互通的方法
2019/07/25 Javascript
针对Vue路由history模式下Nginx后台配置操作
2020/10/22 Javascript
python通过正则查找微博@(at)用户的方法
2015/03/13 Python
10种检测Python程序运行时间、CPU和内存占用的方法
2015/04/01 Python
pymongo给mongodb创建索引的简单实现方法
2015/05/06 Python
python 3.5实现检测路由器流量并写入txt的方法实例
2017/12/17 Python
python3+PyQt5使用数据库表视图
2018/04/24 Python
PyCharm的设置方法和第一个Python程序的建立
2019/01/16 Python
神经网络训练采用gpu设置的方式
2021/03/03 Python
总结30个CSS3选择器
2017/04/13 HTML / CSS
详解CSS3中常用的样式【基本文本和字体样式】
2020/10/20 HTML / CSS
法国票务网站:Ticketmaster法国
2018/07/09 全球购物
雅诗兰黛加拿大官网:Estee Lauder加拿大
2019/07/31 全球购物
Envie de Fraise意大利:法国网上推出的孕妇装品牌
2020/10/18 全球购物
网络通讯中,端口有什么含义,端口的取值范围
2012/11/23 面试题
团队精神演讲稿
2013/12/31 职场文书
幼儿园教师工作感言
2014/02/15 职场文书
2015年公司新年寄语
2014/12/08 职场文书
工程项目合作意向书
2015/05/08 职场文书
色戒观后感
2015/06/12 职场文书
python 自动化偷懒的四个实用操作
2021/04/11 Python
仅用几行Python代码就能复制她的U盘文件?
2021/06/26 Python