<script type="text/javascript">
function resizeimg(ImgD,iwidth,iheight) {
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
ImgD.style.cursor= "pointer"; //改变鼠标指针
ImgD.onclick = function() { window.open(this.src);} //点击打开大图片
if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判断浏览器,如果是IE
ImgD.title = "请使用鼠标滚轮缩放图片,点击图片可在新窗口打开";
ImgD.onmousewheel = function img_zoom() //滚轮缩放
{
var zoom = parseInt(this.style.zoom, 10) || 100;
zoom += event.wheelDelta / 12;
if (zoom> 0) this.style.zoom = zoom + "%";
return false;
}
} else { //如果不是IE
ImgD.title = "点击图片可在新窗口打开";
}
}
}
</script><img name="" src="upload/<%=rs("picfilename")%>" alt="<%=rs("title")%>" style="border:1px #999 solid;" οnlοad="javascript:resizeimg(this,150,140)">
<script type="text/JavaScript">
<!--
//图片按比例缩放---素材图片页面控制
var flag=false;
function newDrawImage(ImgD){
var image=new Image();
var iwidth = 200; //定义允许图片宽度
var iheight = 150; //定义允许图片高度
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
// ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>
<img src="admin/uploadpic/2009811522257007705.jpg" width="194" height="130" border="0" οnlοad="newDrawImage(this)"/>
JS限制图片大小并自动缩放
今 天在给一个客户做个企业网站时需要用到,客户给的产品图片大小不一致,有的宽度过大撑开了版面,很影响美观。自己又想偷懒,不想在CMS里一张张的修改。 分析了源文件,发现图片的宽度主要是由HTML标签WIDTH,HEIGHT和css style里对应的WIDTH,HEIGHT指定,所以就花了几分 钟写了下面的JS脚本,一劳永逸,直接修改模板,全部生成即可!
由于本来做这个就是为了赶时间,所以JS写法很多没有规范化和封装,贴上来,有需要的朋友应该蛮多。
实现效果:所有指定ID(fontzoom)内的IMG图片无论是用HTML还是内联指定过width,HEIGHT的,全部自动判断,超过JS指定宽度的,自动缩放,不超过的不变。
经测试,IE6.0 FIREFOX3.0下通过!
//限制产品内容图片大小,自动缩放
function autoResizepic()
{
var picTarget=document.getElementById("fontzoom").getElementsByTagName("img");
if(picTarget){
if(picTarget[0].width>600){picTarget[0].height=picTarget[0].height*600/picTarget[0].width;picTarget[0].width=600;}
if(parseInt(picTarget[0].style.width)>600){picTarget[0].style.height=parseInt(picTarget[0].style.height)*600/parseInt(picTarget[0].style.width);picTarget[0].style.width=600+"px";}
}
}
由于最近在学习AJAX所以不免要看些JS,今天写了一个图片等比缩放的function来和大家一起分享。
JS:
function pic_reset(drawImage,thumbs_size) {
var max = thumbs_size.split(',');
var fixwidth = max[0];
var fixheight = max[1];
w=drawImage.width;h=drawImage.height;
if(w>fixwidth) { drawImage.width=fixwidth;drawImage.height=h/(w/fixwidth);}
if(h>fixheight) { drawImage.height=fixheight;drawImage.width=w/(h/fixheight);}
drawImage.style.cursor= "pointer";
drawImage.onclick = function() { window.open(this.src);}
drawImage.title = "点击查看原始图片";
}
HTML:
<html>
<head>
<title>超哥一线之图片等比缩放</title>
<script language="javascript" type="text/javascript" src="js/Pic2.js"></script>
</head>
<body>
<img src="img/contents.jpg" οnlοad="resize(this,'50,50');"></img>
</body>
<html>
js 等比缩放图片大小 一句话
οnlοad="javascript:if(this.width>500)this.width=500;"
经常看到一些网页,其中的图片比例失调,这里提供一组方法,供需要的朋友参考。基本构思如下:
1、在指定宽度和高度范围内等比例最大化缩放图片;
2、按指定宽度等比最大化缩放图片;
3、按指定高度等比最大化缩放图片。
原方法在pvo.js文件中,为了便于交流,在这里做了修改。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>等比缩放图片</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
/**
*定义缩放的数据结构
*/
var scale={
width:null,
height:null
};
/**
*@see cn.hkm.web.Picture.java
*在指定宽度和高度范围内最大化缩放图片
*@param width 图片原始宽度
*@param height 图片原始高度
*@param outWidth 指定宽度范围
*@param outHeight 指定宽度范围
*/
var scaleWH=function(width,height,outWidth,outHeight){
width=parseInt(width);
height=parseInt(height);
outWidth=parseInt(outWidth);
outHeight=parseInt(outHeight);
var h=width;
var w=height;
var r=height/width;
var rs=outHeight/outWidth;
if((width<=outWidth)&&(height<=outHeight)){
w=width;
h=height;
}
if((width<=outWidth)&&(height>outHeight)){
w=parseInt(outHeight/r);
h=outHeight;
}
if((width>outWidth)&&(height<=outHeight)){
w=outWidth;
h=parseInt(outWidth*r);
}
if((width>outWidth)&&(height>outHeight)){
if(r<rs){
w=outWidth;
h=parseInt(outWidth*r);
}
if(r>rs){
h=outHeight;
w=parseInt(outHeight/r);
}
if(r==rs){
w=outWidth;
h=outHeight;
}
}
scale.width=w;
scale.height=h;
return scale;
}
/**
*@see cn.hkm.web.Picture.java
*在指定宽度范围内最大化缩放图片
*@param width 图片原始宽度
*@param height 图片原始高度
*@param outWidth 指定宽度范围
*/
var scaleW=function(width,height,outWidth){
width=parseInt(width);
height=parseInt(height);
outWidth=parseInt(outWidth);
if(width<outWidth){
scale.width=width;
scale.height=height;
}else{
scale.width=outWidth;
scale.height=parseInt(outWidth*height/width);
}
return scale;
}
/**
*@see cn.hkm.web.Picture.java
*在指定高度范围内最大化缩放图片
*@param width 图片原始宽度
*@param height 图片原始高度
*@param outWidth 指定宽度范围
*/
var scaleH=function(width,height,outHeight){
width=parseInt(width);
height=parseInt(height);
outHeight=parseInt(outHeight);
if(height<outHeight){
scale.width=width;
scale.height=height;
}else{
scale.width=parseInt(outHeight*width/height);
scale.height=outHeight;
}
return scale;
}
function see(){
scale=scaleWH(1366,768,600,600);
document.getElementById("byWH").innerHTML="<img src='images/img001.png' width='"+scale.width+"px' height='"+scale.height+"px' />";
scale=scaleW(1366,768,500);
document.getElementById("byW").innerHTML="<img src='images/img001.png' width='"+scale.width+"px' height='"+scale.height+"px' />";
scale=scaleH(1366,768,300);
document.getElementById("byH").innerHTML="<img src='images/img001.png' width='"+scale.width+"px' height='"+scale.height+"px' />";
}
</script>
</head>
<body οnlοad="see()" >
<div id="byWH"></div>
<br/>
<div id="byW"></div>
<br/>
<div id="byH"></div>
</body>
</html>