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


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 相关文章推荐
JavaScript URL参数读取改进版
Jan 16 Javascript
js 获取Listbox选择的值的代码
Apr 15 Javascript
JS 毫秒转时间示例代码
Sep 22 Javascript
jquery checkbox实现单选小例
Nov 27 Javascript
js/jquery判断浏览器的方法小结
Sep 02 Javascript
node.js中的emitter.on方法使用说明
Dec 10 Javascript
对js中回调函数的一些看法
Aug 29 Javascript
AngularJS实现Input格式化的方法
Nov 07 Javascript
js操作浏览器的参数方法
Jan 21 Javascript
详解使用angular的HttpClient搭配rxjs
Sep 01 Javascript
深入浅析Vue.js中 computed和methods不同机制
Mar 22 Javascript
vue-cli webpack配置文件分析
May 20 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
is_uploaded_file函数引发的不能上传文件问题
2013/10/29 PHP
php基于curl扩展制作跨平台的restfule 接口
2015/05/11 PHP
thinkPHP模板引擎用法示例
2016/12/08 PHP
jQuery 菜单随滚条改为以定位方式(固定要浏览器顶部)
2012/05/24 Javascript
基于jQuery的倒计时实现代码
2012/05/30 Javascript
jquery 卷帘效果实现代码(不同方向)
2013/02/05 Javascript
文本有关的样式和jQuery求对象的高宽问题分别说明
2013/08/30 Javascript
jQuery validate插件submitHandler提交导致死循环解决方法
2016/01/21 Javascript
浅析Javascript匿名函数与自执行函数
2016/02/06 Javascript
JS平滑无缝滚动效果的实现代码
2016/05/06 Javascript
javascript中闭包概念与用法深入理解
2016/12/15 Javascript
微信小程序页面间通信的5种方式
2017/03/31 Javascript
ES6中的箭头函数实例详解
2017/04/06 Javascript
一个有意思的鼠标点击文字特效jquery代码
2017/09/23 jQuery
利用原生js实现html5小游戏之打砖块(附源码)
2018/01/03 Javascript
微信小程序picker组件关于objectArray数据类型的绑定方法
2019/03/13 Javascript
微信小程序生成海报分享朋友圈的实现方法
2019/05/06 Javascript
详解element-ui中el-select的默认选择项问题
2019/08/02 Javascript
js回调函数原理与用法案例分析
2020/03/04 Javascript
JQuery获得内容和属性方法解析
2020/05/30 jQuery
python 与GO中操作slice,list的方式实例代码
2017/03/20 Python
django使用xlwt导出excel文件实例代码
2018/02/06 Python
python3爬取淘宝信息代码分析
2018/02/10 Python
详解解决Python memory error的问题(四种解决方案)
2019/08/08 Python
Python shelve模块实现解析
2019/08/28 Python
Python openpyxl读取单元格字体颜色过程解析
2019/09/03 Python
django框架创建应用操作示例
2019/09/26 Python
PyTorch中torch.tensor与torch.Tensor的区别详解
2020/05/18 Python
解决pycharm导入本地py文件时,模块下方出现红色波浪线的问题
2020/06/01 Python
canvas绘制表情包的示例代码
2018/07/09 HTML / CSS
使用javascript和HTML5 Canvas画的四渐变色播放按钮效果
2014/04/10 HTML / CSS
天猫国际进口超市直营:官方直采,一站购齐
2017/12/11 全球购物
欧洲、亚洲、非洲和拉丁美洲的度假套餐:Great Value Vacations
2019/03/30 全球购物
什么是.net
2015/08/03 面试题
大四自我鉴定
2014/02/08 职场文书
python读取mnist数据集方法案例详解
2021/09/04 Python