最好的方法:<script language="JavaScript">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 400/400){
if(image.width>400){
ImgD.width=400;
ImgD.height=(image.height*400)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>400){
ImgD.height=400;
ImgD.width=(image.width*400)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>
调用:<img src="" border="0" onload="javascript:DrawImage(this);"
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 400/400){
if(image.width>400){
ImgD.width=400;
ImgD.height=(image.height*400)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>400){
ImgD.height=400;
ImgD.width=(image.width*400)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>
调用:<img src="" border="0" onload="javascript:DrawImage(this);"
解决方案 »
- jquery 动态生成的div 不能绑定事件
- 修改aptana默认工作空间 启动报错
- 有个网页元素的id 我想用js改变这个元素的背景 代码怎么写
- java数据转换为JSON数据
- 关于自动提交表单
- 我不甘心啊!为什么JavaScript更新年月日可以更新年,但是不能更新月呢?帮我看下,我不甘心就这样放弃啊~~~~~~~~~~~~~~~~~~
- 关于富文本编辑器的问题
- 关于checkbox的事件问题
- 怎么控制IE浏览器的页眉、页脚的字体大小?
- 定時刷新問題,100分/////
- <a href="javascript:;" onClick="function();">和<a href="javascript:function();">调用函数有什么区别?
- 十万火急,,,,
<script language="Javascript">
function imgScale(srcobj)
{
var OriginImage=new Image();
OriginImage.src=srcobj.src;
var H=OriginImage.height;
var W=OriginImage.width;
var scale=W>160?W/160:1;//得到缩放比例
var objname=""+srcobj.name
srcobj.width=W/scale;//缩放宽度
srcobj.Height=H/scale;//缩放高度
}
</script>
</head><body>
<img name="newsimg" src="1.jpg" width=160>
<input type=button onclick="imgScale(document.all.newsimg)" value="scale">
</body></html>
这叫做短小精悍:<html><head>
<script language="Javascript">
function imgScale(srcobj){
var OImg=new Image();
OImg.src=srcobj.src;
var scale=OImg.width>160?OImg.width/160:1;
//得到缩放比例,大于160就按160显示,不然缩放比例就是1,即不缩放。
srcobj.width=OImg.width/scale;//缩放宽度
srcobj.Height=OImg.height/scale;//缩放高度
}
</script></head><body>
<img name="newsimg" src="c1.jpg" width=160>
<input type=button onclick="imgScale(document.all.newsimg)" value="scale">
</body></html>
上面那个是实现楼主的要求,下面这个实现 1 楼的高、宽均不能超过160<html><head>
<script language="Javascript">
function imgScale(srcobj){
var OImg=new Image();
OImg.src=srcobj.src;
var scale=OImg.width>OImg.height?(OImg.width>160?OImg.width/160:1):(OImg.height>160?OImg.height/160:1);
srcobj.width=OImg.width/scale;
srcobj.Height=OImg.height/scale;
}
</script></head><body>
<img name="newsimg" src="c2.jpg" width=160>
<input type=button onclick="imgScale(document.all.newsimg)" value="scale">
</body></html>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function View(){
document.all.div1.innerHTML="<img id='img1' src='" + document.all.file1.value + "' onload='zoom()' >"
}
function zoom(){
if( document.all.img1.width>500 ){
document.all.img1.style.zoom=0.5;
}
if(document.all.img1.height > 500){
document.all.img1.style.zoom=0.5;
}
/*if( document.all.img1.width < 250 ){
document.all.img1.style.zoom=2;
}
if(document.all.img1.height < 250){
document.all.img1.style.zoom=2;
}
*/
}
//-->
</SCRIPT>
</HEAD>
</BODY>
<INPUT TYPE="file" id="file1" onchange="View()">
<DIV id="div1"></DIV>
<BODY>
</HTML>你自己可以修改下。