2013-06-24 42 views
1

我有一個圖片網站,並已實現無限滾動的圖片。 問題是,我不知道如何在加載後使用jQuery調整圖像大小。如何設置使用ajax加載的圖像的寬度?

我使用jQuery 1.5版本的drupal。

這是到目前爲止我的代碼不工作:

jQuery(body).delegate("#region-content","onchange",function() { 
jQuery('#region-content img').each(function() { 
    var desiredWidth = 520; // Max width for the image 
    var ratio = 0; // Used for aspect ratio 
    var width = jQuery(this).width(); // Current image width 
    var height = jQuery(this).height(); // Current image height 



    if(width != desiredWidth){ 
     ratio = desiredWidth/width; // get ratio for scaling image 
     jQuery(this).css("width", desiredWidth); // Set new width 
     jQuery(this).css("height", height * ratio); // Scale height based on ratio 
     height = height * ratio; // Reset height to match scaled image 
     width = width * ratio; // Reset width to match scaled image 
    } 

}); 
}); 
+1

確保圖像實際上是加載這樣的jQuery可以通過看到的寬度和高度'。就緒()'。 – Broxzier

回答

2

爲了維護方面,僅設置了寬度。你可以在CSS做:

CSS

#region-content{ 
    height: 520px; 
    overflow: auto; 
} 

#region-content img{ 
    max-width: 520px; /* only width */ 
} 
+1

您可以放心地使用最大寬度和最大高度,而不會丟失寬高比。 – Broxzier

+0

@Broxzier你是對的:http://jsfiddle.net/yundb/ – Falci

+0

非常感謝。我失去了理智,忘記了它可以通過簡單的CSS來完成。 – hakala

相關問題