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


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实现的动态文字变换
Jul 28 Javascript
仿迅雷焦点广告效果(JQuery版)
Nov 19 Javascript
js 实现css风格选择器(压缩后2KB)
Jan 12 Javascript
js监听键盘事件示例代码
Jul 26 Javascript
js图片预加载示例
Apr 30 Javascript
JS实时弹出新消息提示框并有提示音响起的实现代码
Apr 20 Javascript
无法获取隐藏元素宽度和高度的解决方案
Mar 07 Javascript
在JS中如何把毫秒转换成规定的日期时间格式实例
May 11 Javascript
使用express+multer实现node中的图片上传功能
Feb 02 Javascript
vue实现一拉到底的滑动验证
Jul 25 Javascript
Vue+ElementUI使用vue-pdf实现预览功能
Nov 26 Javascript
vue动态绑定style样式
Apr 20 Vue.js
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/01/23 PHP
php+mysql删除指定编号员工信息的方法
2015/01/14 PHP
PHP魔术方法之__call与__callStatic使用方法
2017/07/23 PHP
远离JS灾难css灾难之 js私有函数和css选择器作为容器
2011/12/11 Javascript
利用javascript判断文件是否存在
2013/12/31 Javascript
取得元素的左和上偏移量的方法
2014/09/17 Javascript
javascript制作sql转换为stringBuffer的小工具
2015/04/03 Javascript
jQuery progressbar通过Ajax请求实现后台进度实时功能
2016/10/11 Javascript
Bootstrap的基本应用要点浅析
2016/12/19 Javascript
详解基于 Nuxt 的 Vue.js 服务端渲染实践
2017/10/24 Javascript
详解Angular2学习笔记之Html属性绑定
2018/01/03 Javascript
用node.js写一个jenkins发版脚本
2019/05/21 Javascript
Vue对象赋值视图不更新问题及解决方法
2019/06/03 Javascript
vue父子组件通信的高级用法示例
2019/08/29 Javascript
JavaScript this使用方法图解
2020/02/04 Javascript
[01:02:30]Mineski vs Secret 2019国际邀请赛淘汰赛 败者组 BO3 第三场 8.22
2019/09/05 DOTA
Python实现学生成绩管理系统
2020/04/05 Python
python opencv3实现人脸识别(windows)
2018/05/25 Python
Django使用 Bootstrap 样式修改书籍列表过程解析
2019/08/09 Python
利用python3 的pygame模块实现塔防游戏
2019/12/30 Python
python 录制系统声音的示例
2020/12/21 Python
HTML5 video 事件应用示例
2014/09/11 HTML / CSS
HTML5自定义mp3播放器源码
2020/01/06 HTML / CSS
canvas 绘图时位置偏离的问题解决
2020/09/16 HTML / CSS
俄罗斯旅游网站:Tripadvisor俄罗斯
2017/03/21 全球购物
一站式跨境收款解决方案:Payoneer(派安盈)
2018/09/06 全球购物
美国最好的钓鱼、狩猎和划船装备商店:Bass Pro Shops
2018/12/02 全球购物
求最大连续递增数字串(如"ads3sl456789DF3456ld345AA"中的"456789")
2015/09/11 面试题
大学生职业生涯规划书模版
2013/12/30 职场文书
国际金融专业自荐信
2014/07/05 职场文书
开天辟地观后感
2015/06/09 职场文书
嘉年华活动新闻稿
2015/07/17 职场文书
获奖感言怎么写
2015/07/31 职场文书
孕妇病假条怎么写
2015/08/17 职场文书
导游词之云南省玉龙雪山
2019/12/19 职场文书
mysql数据库隔离级别详解
2022/06/16 MySQL