玫铃 发表于 2007 年 1 月 2 日 18:39:30

这段限制图片大小的代码该怎么用呢?

#cont img { max-width: 400px;max-height: 400px;width:expression(onload=function(){if (this.offsetWidth>this.offsetHeight){this.style.width=(this.offsetWidth > 400)?"400px":"auto"}{this.style.height=(this.offsetHeight < 400)?"auto":"400px"}});border:0px;}

我的论坛的图片显示BBcode是

'#IMAGE
                        if isFound(str,"") > 0 AND isFound(str,"javascript:") = 0 then
                                imageIndex = imageIndex + 1
                                str= Replace(str,"[img=","<br><img onclick=""userImage" & imageIndex & """ onload=""javascript:if(this.width> screen.width/2) this.width=screen.width/2"" id='userImage" & imageIndex & "' src=")
                                str = Replace(str,"]",">")
                                                        isChange = false
                        End if

该怎么组合呢?

我踏MJ而来 发表于 2007 年 1 月 2 日 18:43:24

这段代码是css里的,看错了,有用,直接插css里就可以
不过,你把这段加在html的head上面也可以

<script type="text/javascript">
window.onload=function(){
var imgsrc=document.images;
for(i=0;i<imgsrc.length;i++) {
if(imgsrc.width>500) {
imgsrc.width=499;
}
}
}
</script>


[ 本帖最后由 我踏MJ而来 于 2007-1-2 18:55 编辑 ]

玫铃 发表于 2007 年 1 月 2 日 19:04:22

直接放到css里就可以了吗

我踏MJ而来 发表于 2007 年 1 月 2 日 19:07:53

对,但是相对图片的大区域要加个cont的id

玫铃 发表于 2007 年 1 月 2 日 19:08:03

我的论坛顶端有一个超宽的图片,所以用了你的代码,那个图片就会变小,但是其他图片不变:)

我踏MJ而来 发表于 2007 年 1 月 2 日 19:13:24


<script type="text/javascript">
window.onload=function(){
var imgsrc=document.images;
for(i=0;i<imgsrc.length;i++) {
if(imgsrc.width>500 && imgsrc.ref!='keep') {
imgsrc.width=499;
}
}
}
</script>

不想变的图片加一个ref="keep"的属性,具体的限制大小改500和499

waitme 发表于 2007 年 1 月 2 日 19:22:16

:lol :lol 。。。。。。。。。。。

玫铃 发表于 2007 年 1 月 2 日 19:40:27

:) :) 真是太谢谢你啦,我苦恼已久的问题终于解决了,呵呵呵。

ashun 发表于 2007 年 1 月 2 日 20:00:24

小白素强人
膜拜ing

esnak 发表于 2007 年 1 月 2 日 20:22:46


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>css</title>
<style type="text/css">
body {}

img {border:0;}

.article {width:200px; height:100px; background:#ccc;}

.article img {
max-width: 200px;
max-height: 200px;
width:
expression(onload=function(){if (this.offsetWidth>this.offsetHeight)
{this.style.width=(this.offsetWidth > 200)?"200px":"auto"}
{this.style.height=(this.offsetHeight < 200)?"auto":"200px"}
});
}
</style>
</head>
<body>
<div class="article">
<img src="http://jgwy.net/bbs/images/server.gif" />
<br />
<img src="http://www.jgwy.net/bbs/images/smilies/smile.gif" />
<br />
容器width=200px
</div>
</body>
</html>


但注意,expression是相当耗cpu的,不能用太多
页: [1] 2
查看完整版本: 这段限制图片大小的代码该怎么用呢?