固定網頁背景圖同時保持圖片比例的思路代碼
更新時間:2013年08月15日 16:56:05 作者:
代碼功能:背景圖片固定,隨窗口大小改變而改變大小,保持比例不變而縮放,有此需求的朋友可以參考下
提供一個背景圖片策略:
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"));
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"));
相關文章
滾動條的監(jiān)聽與內容隨著滾動條動態(tài)加載的實現(xiàn)
下面小編就為大家?guī)硪黄獫L動條的監(jiān)聽與內容隨著滾動條動態(tài)加載的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
JavaScript實現(xiàn)的微信二維碼圖片生成器的示例
二維碼分享功能大多是由后端實現(xiàn)的,對服務器的負載較重,這里有一個前端實現(xiàn)的版本,本文介紹了JavaScript實現(xiàn)的微信二維碼圖片生成器的示例,有需要的可以了解一下。2016-10-10
主頁面中的兩個iframe實現(xiàn)鼠標拖動改變其大小
iframe實現(xiàn)鼠標拖動改變其大小在一個頁面中的兩個iframe的情況下,此方法相當實用,感興趣的各位不妨參考下,或許對你有所幫助2013-04-04

