在CS里约束帖子中的图片大小

将下面的代码加到Utility/global.js中,这样就可以按比例约束图片的大小了(注意,是页面中的全部超过规定大小的图片哦):
function ResizeImage(imageid,limitWidth,limitHeight) 
{     
    
var image = new Image(); 
    image.src 
= imageid.src; 
     
    
if(image.width <= 0 && image.height <= 0return
     
    
if(image.width/image.height >= limitWidth/limitHeight) 
    { 
        
if(image.width > limitWidth) 
        { 
            imageid.width 
= limitWidth; 
            imageid.height 
= (image.height*limitWidth)/image.width; 
        } 
    } 
    
else if(image.height > limitHeight) 
    { 
            imageid.height 
= limitHeight; 
            imageid.width 
= (image.width*limitHeight)/image.height;      
    } 
     
    
if (imageid.parentElement.tagName != "A"
    { 
        imageid.onclick 
= function(){window.open(this.src);} 
        imageid.style.cursor 
= "hand"
    } 


window.onload 
= InitImages; 

function InitImages() 

    
var maxWidth = 550
    
var maxHeight = 800
     
    
var imgs = document.getElementsByTagName("img"); 
     
    
for(var i=0; i < imgs.length; i++
    { 
        
var img = imgs[i]; 
         
        
if(img.width>maxWidth||img.height>maxHeight) 
            ResizeImage(img, maxWidth, maxHeight); 
    } 
}
posted @ 2005-08-22 15:00  水村  阅读(533)  评论(0编辑  收藏  举报