//调用方式:zoomImg(this,150,150) var flag = false; function zoomImg(imgT,iwidth,iheight){ //需处理的图片,允许的宽度,允许的高度 var img = new Image(); img.src = imgT.src; if(img.width > 0 && img.height > 0){ flag = true; if(img.width/img.height >= iwidth/iheight){ if(img.width > iwidth){ imgT.width = iwidth; imgT.height = (img.height * iwidth)/img.width; }else{ //维持原图大小 //imgT.width = iwidth; //imgT.height = (img.height * img.width)/iwidth; imgT.width = img.width; imgT.height = img.height; } }else{ if(img.height > iheight){ imgT.width = (img.width * iheight)/img.height; imgT.height = iheight; }else{ //imgT.width = (img.width * img.height)/iheight; //imgT.height = iheight; imgT.width = img.width; imgT.height = img.height; } } } } //等比例压缩(可一边压缩) function AutoResizeImage(objImg,maxWidth,maxHeight){ var img = new Image(); img.src = objImg.src; var hRatio; var wRatio; var Ratio = 1; var w = img.width; var h = img.height; wRatio = maxWidth / w; hRatio = maxHeight / h; if (maxWidth ==0 && maxHeight==0){ Ratio = 1; }else if (maxWidth==0){ if (hRatio<1) Ratio = hRatio; }else if (maxHeight==0){ if (wRatio<1) Ratio = wRatio; }else if (wRatio<1 || hRatio<1){ Ratio = (wRatio<=hRatio?wRatio:hRatio); } if (Ratio<1){ w = w * Ratio; h = h * Ratio; } objImg.height = h; objImg.width = w; } //图片缩放(可填充空白) function rectDrawImage(ImgD,FitWidth,FitHeight){ var image=new Image(); image.src=ImgD.src; if(image.width>0 && image.height>0){ if(image.width/image.height>= FitWidth/FitHeight){ if(image.width>FitWidth){ ImgD.width=FitWidth; ImgD.height=(image.height*FitWidth)/image.width; }else{ ImgD.width=image.width; ImgD.height=image.height; } }else{ if(image.height>FitHeight){ ImgD.height=FitHeight; ImgD.width=(image.width*FitHeight)/image.height; }else{ ImgD.width=image.width; ImgD.height=image.height; } } } //图片高小于设定的边框高时,让图片上下居中 padding 为内边距 if(ImgD.height < FitHeight ){ var paddH = parseInt((FitHeight - ImgD.height)/2); ImgD.style.paddingTop = paddH + "px"; ImgD.style.paddingBottom = paddH + "px"; } //图片宽小于设定的边框宽时,让图片左右居中 padding 为内边距 if(ImgD.width < FitWidth ){ var paddW = parseInt((FitWidth - ImgD.width)/2); ImgD.style.paddingLeft = paddW + "px"; ImgD.style.paddingRight = paddW + "px"; } } //获取元素 function getElemClass(oParent, sClass){ var _parent = document.getElementById(oParent).getElementsByTagName("*"); var array = []; if(_parent){ for(var i=0;i<_parent.length;i++){ if(_parent[i].className == sClass){ array.push(_parent[i]); } } } return array; } //获取样式 function getStyle(obj,attr){ return obj.currentStyle ? obj.currentStyle[attr] : document.defaultView.getComputedStyle(obj, null)[attr]; } //缓冲效果 function doMoveBuffer(obj, json, fx, fn){ obj.timer && clearInterval(obj.timer); obj.timer = setInterval(function(){ var bStop = true, mType = 1, //缓动类型 attr = null, cur = 0; for(attr in json){ cur = attr == 'opacity' ? parseInt(parseFloat(getStyle(obj, attr)).toFixed(2)*100) : parseInt(getStyle(obj, attr)); if(!obj.speed) obj.speed = {}; if(!obj.speed[attr]) obj.speed[attr] = 0; //平滑缓动效果 if(mType == 1){ if(cur != json[attr]){ bStop = false; obj.speed[attr] = (json[attr] - cur)/fx; obj.speed[attr] = obj.speed[attr] > 0 ? Math.ceil(obj.speed[attr]) : Math.floor(obj.speed[attr]); } } //反弹缓动效果 else{ if(Math.abs(json[attr]-cur)>=1 || Math.abs(obj.speed[attr])>=1){ bStop=false; obj.speed[attr] += (json[attr] - cur)/fx; obj.speed[attr] *= 0.7; } } if(attr == 'opacity'){ obj.style.filter = 'alpha(opacity:'+(cur+obj.speed[attr])+')'; obj.style.opacity = (cur+obj.speed[attr])/100; }else{ obj.style[attr] = (cur+obj.speed[attr]) + 'px'; } if(bStop){ clearInterval(obj.timer); obj.timer = null; fn && fn.call(obj); } } }, 30); }