你这样加已经晚了!
页面已经onload了,图片肯定也load完毕了。
页面已经onload了,图片肯定也load完毕了。
解决方案 »
- 请问如何向客户端推送通知消息?
- 100分奉上。div拖动在火狐下始终有点问题。求助
- JavaScript取url 乱码---急等
- 使用JavaScript控制网页上的VML元素
- 怎样修改、替换 缓存中的 javascript
- jquery.nivo.slider.pack透明度的设置问题。请教大家
- 怎么让IE不自动打开想要的下载Excel文件?
- 含frameset的页面中,如何在子页面中设置本身所在的frame的高度?
- 怎样可以任意移动一个层,并始终定位于窗口的某个地方?
- Jquery 模拟点击,并触已发绑定的事件
- (续)100分!!哪位好心人帮帮小弟啊!!!!!!!!!!!!!
- 使flash文件在某网页中依次、随机显示的方法有吗??
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JK:支持民族工业,尽量少买X货</title>
<script>
function setMaxHeight(imgObj,maxHeight){
if(imgObj.offsetHeight>maxHeight) {
imgObj.height=maxHeight;
imgObj.removeAttribute('width');
}
}
</script>
</head>
<body>
设定最大宽度和最大高度,同时保证图片比例。
<hr>
<IMG id="big_img2"
src="20060925132517e9cb7.gif" width="500" onload="setMaxHeight(this,300);">
</body>
</html>
src="20060925132517e9cb7.gif" width="500" onload="setMaxHeight(this,300);">这一断对于我是不可见的,没法直接加。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JK:支持民族工业,尽量少买X货</title>
<script>
function setMaxHeight(imgObj,maxHeight){
if(imgObj.offsetHeight>maxHeight) {
imgObj.height=maxHeight;
imgObj.removeAttribute('width');
}
}
</script>
</head>
<body>
设定最大宽度和最大高度,同时保证图片比例。
<hr>
<IMG id="big_img2"
src="http://zi.csdn.net/huabiao120-60a.gif" >
</body>
</html><script>
big_img2.removeAttribute('width');
big_img2.removeAttribute('height');
big_img2.width=500;
big_img2.onload= new Function("setMaxHeight(this,200);");
</script>
big_img2.removeAttribute('width');
big_img2.removeAttribute('height');
big_img2.width=500;
big_img2.onload= new Function("setMaxHeight(this,200);");
</script>
这一段就直接能执行吗?不需要调用?
不是,这个是一个半开源的东东,在上面做修改,给人家提意见可提不着啊。
<script language="javascript">
//图片按比例缩放
var flag=false;
function DrawImage(ImgD)
{
var image=new Image();
var iwidth = 524; //定义允许图片宽度
var iheight = 100000; //定义允许图片高度
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;
}
}
}
//调用:<img src="图片" onload="javascript:DrawImage(this)">
var imgCcount = document.images.length;
for (i=0;i<imgCcount;i++)
{
var thisimg = document.images[i];
if(thisimg.src != "http://www.dafangtour.com/Library/img-l/butten01.gif" )
{
var imagea=new Image();
imagea.src=thisimg.src;
if (imagea.width > 524)
{
thisimg.removeAttribute('width');
thisimg.removeAttribute('height');
thisimg.removeAttribute('style');
DrawImage(thisimg);
}
}
}
</script>这段代码因为我们有些需求特殊,所以看起来有些罗唆。总结关键是:
1.页面执行循序
执行代码要放在页末尾,需要等所有images对象都转载完再执行。
2.我加了一个
thisimg.removeAttribute('style');
如果是在style里定义了图片的长宽,则我们定义image.width和height都没用,要先将style属性去掉。