Feb 17

用JS控制页面图的图片大小的通用代码 晴

本文转载自IT柏拉图的博客,我觉得这个方法非常好,无须在显示图片时添加额外的代码

这个一般用于文章查看的页面,针对新闻性质的网站,此外这个算法是直接分析网页中所有图片的,因此如果你的版面设计中有比指定宽度大的图片是不能用这个函数的(或者用id区分开)。

<script language="javascript">
function ResizeImages()
{
   var myimg,oldwidth;
   var maxwidth=600;
   for(i=0;i<document.images.length;i++){
     myimg = document.images[i];
     if(myimg.width > maxwidth)
     {
        oldwidth = myimg.width;
        myimg.width = maxwidth;
        myimg.height = myimg.height * (maxwidth/oldwidth);
     }
   }
}
ResizeImages();
</script>
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]