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


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 相关文章推荐
新手入门常用代码集锦
Jan 11 Javascript
制作高质量的JQuery Plugin 插件的方法
Apr 20 Javascript
js文件Cookie存取值示例代码
Feb 20 Javascript
js中substring和substr两者区别和使用方法
Nov 09 Javascript
Seajs 简易文档 提供简单、极致的模块化开发体验
Apr 13 Javascript
json对象转为字符串,当做参数传递时加密解密的实现方法
Jun 29 Javascript
JS实现倒计时(天数、时、分、秒)
Nov 16 Javascript
Angularjs实现分页和分页算法的示例代码
Dec 23 Javascript
JS将unicode码转中文方法
May 08 Javascript
解决JS外部文件中文注释出现乱码问题
Jul 09 Javascript
详解如何使用PM2将Node.js的集群变得更加容易
Nov 15 Javascript
Js数组扁平化实现方法代码总汇
Nov 11 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获取数组中重复数据的两种方法
2013/06/28 PHP
让PHP显示Facebook的粉丝数量方法
2014/01/08 PHP
Zend Framework教程之模型Model基本规则和使用方法
2016/03/04 PHP
PHP htmlspecialchars_decode()函数用法讲解
2019/03/01 PHP
详解PHP 二维数组排序保持键名不变
2019/03/06 PHP
dropdownlist之间的互相联动实现(显示与隐藏)
2009/11/24 Javascript
jquery.cookie.js 操作cookie实现记住密码功能的实现代码
2011/04/27 Javascript
javascript开发技术大全 第4章 直接量与字符集
2011/07/03 Javascript
jQuery对于显示和隐藏等常用状态的判断方法
2014/12/13 Javascript
JavaScript+CSS实现的可折叠二级菜单实例
2016/02/29 Javascript
jQuery基于$.ajax设置移动端click超时处理方法
2016/05/14 Javascript
ros::spin() 和 ros::spinOnce()函数的区别及详解
2016/10/01 Javascript
基于jQuery实现数字滚动效果
2017/01/16 Javascript
微信小程序功能之全屏滚动效果的实现代码
2018/11/22 Javascript
vue将后台数据时间戳转换成日期格式
2019/07/31 Javascript
javascript合并两个数组最简单的实现方法
2019/09/14 Javascript
[38:44]DOTA2上海特级锦标赛A组小组赛#2 Secret VS CDEC第二局
2016/02/25 DOTA
[01:20:37]FNATIC vs NIP 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/19 DOTA
Python守护线程用法实例
2017/06/23 Python
解决python3 json数据包含中文的读写问题
2018/05/10 Python
CentOS下Python3的安装及创建虚拟环境的方法
2018/11/28 Python
Python删除n行后的其他行方法
2019/01/28 Python
django做form表单的数据验证过程详解
2019/07/26 Python
Python发送邮件的实例代码讲解
2019/10/16 Python
Python执行时间的几种计算方法
2020/07/31 Python
让IE支持CSS3的不完全兼容方案
2014/09/19 HTML / CSS
CSS3 实现倒计时效果
2020/11/25 HTML / CSS
html5标记文字_动力节点Java学院整理
2017/07/11 HTML / CSS
软件测试工程师结构化面试题库
2016/11/23 面试题
short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
2014/09/26 面试题
药物学专业学生的自我评价
2013/10/27 职场文书
入股协议书范本
2014/04/14 职场文书
幼儿评语大全
2014/04/30 职场文书
校园文明倡议书
2014/05/16 职场文书
2019大学生实习报告
2019/06/21 职场文书
python基础之爬虫入门
2021/05/10 Python