JS类库——Image
Image.js
Code
/* 常用对象 */
//构造函数
var ImageHelper = function() {}
ImageHelper.prototype = {
//根据让IE6的png图片背景变成透明(注意引用后可能会使图片布局改变)
fixPNG:function(imgObj) {
//<span style="display:inline-block; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='PNG图片的URL',sizingMethod='scale');width:span的宽即图片宽;height:span的高即图片高;"></span>
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
var imgID = (imgObj.id) ? "id='" + imgObj.id + "' " : "";
var imgClass = (imgObj.className) ? "class='" + imgObj.className + "' " : "";
var imgTitle = (imgObj.title) ? "title='" + imgObj.title + "' " : "title='" + imgObj.alt + "' ";
var imgStyle = "display:inline-block;" + imgObj.style.cssText;
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + imgObj.width + "px; height:" + imgObj.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + imgObj.src + "\', sizingMethod='scale');\"></span>";
imgObj.outerHTML = strNewHTML;
}
},
//让所有IE6下的所有PNG图片背景透明(注意引用后可能会使图片布局改变)
correctPNG:function() {
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
for(var j=0; j<document.images.length; j++) {
var img = document.images[j];
var imgName = img.src.toUpperCase();
if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
var imgID = (img.id) ? "id='" + img.id + "' " : "";
var imgClass = (img.className) ? "class='" + img.className + "' " : "";
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
var imgStyle = "display:inline-block;" + img.style.cssText;
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
img.outerHTML = strNewHTML
j = j - 1;
}
}
}
},
/**
* 自动缩放图片尺寸
* ImgSource图片源(如URL)
* ImgWidth要缩放的宽
* ImgHeight要缩放的高
* ImgToFit当图长宽都小于显示值时是否拉伸(true拉伸或false不拉伸)
* 使用方法:原图的宽高最好也要写成缩放的大小<img src="***.jpg" width="180" height="110" onload="javascript:__Image__.drawImage(this,180,110,true);">
*/
drawImage:function(ImgSource, ImgWidth, ImgHeight, ImgToFit) {
var image = new Image();
image.src = ImgSource.src;
if(image.width > 0 && image.height > 0) {
if((image.width / image.height) >= (ImgWidth / ImgHeight)) {
if((image.width > ImgWidth) || (image.width < ImgWidth && ImgToFit)) {
ImgSource.width = ImgWidth;
ImgSource.height = (image.height * ImgWidth) / image.width;
}
}
else {
if((image.height > ImgHeight) || ((image.height < ImgHeight) && ImgToFit)) {
ImgSource.height = ImgHeight;
ImgSource.width = parseInt((image.width * ImgHeight) / image.height);
}
}
}
}
}
var __Image__ = new ImageHelper();//默认生成一个对象
/* 常用对象 */
//构造函数
var ImageHelper = function() {}
ImageHelper.prototype = {
//根据让IE6的png图片背景变成透明(注意引用后可能会使图片布局改变)
fixPNG:function(imgObj) {
//<span style="display:inline-block; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='PNG图片的URL',sizingMethod='scale');width:span的宽即图片宽;height:span的高即图片高;"></span>
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
var imgID = (imgObj.id) ? "id='" + imgObj.id + "' " : "";
var imgClass = (imgObj.className) ? "class='" + imgObj.className + "' " : "";
var imgTitle = (imgObj.title) ? "title='" + imgObj.title + "' " : "title='" + imgObj.alt + "' ";
var imgStyle = "display:inline-block;" + imgObj.style.cssText;
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + imgObj.width + "px; height:" + imgObj.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + imgObj.src + "\', sizingMethod='scale');\"></span>";
imgObj.outerHTML = strNewHTML;
}
},
//让所有IE6下的所有PNG图片背景透明(注意引用后可能会使图片布局改变)
correctPNG:function() {
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
for(var j=0; j<document.images.length; j++) {
var img = document.images[j];
var imgName = img.src.toUpperCase();
if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
var imgID = (img.id) ? "id='" + img.id + "' " : "";
var imgClass = (img.className) ? "class='" + img.className + "' " : "";
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
var imgStyle = "display:inline-block;" + img.style.cssText;
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
img.outerHTML = strNewHTML
j = j - 1;
}
}
}
},
/**
* 自动缩放图片尺寸
* ImgSource图片源(如URL)
* ImgWidth要缩放的宽
* ImgHeight要缩放的高
* ImgToFit当图长宽都小于显示值时是否拉伸(true拉伸或false不拉伸)
* 使用方法:原图的宽高最好也要写成缩放的大小<img src="***.jpg" width="180" height="110" onload="javascript:__Image__.drawImage(this,180,110,true);">
*/
drawImage:function(ImgSource, ImgWidth, ImgHeight, ImgToFit) {
var image = new Image();
image.src = ImgSource.src;
if(image.width > 0 && image.height > 0) {
if((image.width / image.height) >= (ImgWidth / ImgHeight)) {
if((image.width > ImgWidth) || (image.width < ImgWidth && ImgToFit)) {
ImgSource.width = ImgWidth;
ImgSource.height = (image.height * ImgWidth) / image.width;
}
}
else {
if((image.height > ImgHeight) || ((image.height < ImgHeight) && ImgToFit)) {
ImgSource.height = ImgHeight;
ImgSource.width = parseInt((image.width * ImgHeight) / image.height);
}
}
}
}
}
var __Image__ = new ImageHelper();//默认生成一个对象
看了要回复,要不就没心思写了~