之後的作品我用這個JavaScript來調整,至800像素,在頁面上一個大的(5MB)圖像的最大:的Javascript調整原圖只有F5
<script type="text/javascript">
$(document).ready(function() {
$('.detailImg').each(function() {
var maxWidth = 800; // Max width for the image
var maxHeight = 800; // Max height for the image
var ratio = 0; // Used for aspect ratio
var width = $(this).width(); // Current image width
var height = $(this).height(); // Current image height
// Check if the current width is larger than the max
if (width > maxWidth) {
ratio = maxWidth/width; // get ratio for scaling image
$(this).css("width", maxWidth); // Set new width
$(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
}
// Check if current height is larger than max
if (height > maxHeight) {
ratio = maxHeight/height; // get ratio for scaling image
$(this).css("height", maxHeight); // Set new height
$(this).css("width", width * ratio); // Scale width based on ratio
width = width * ratio; // Reset width to match scaled image
}
});
});
當加載頁面圖像沒有調整大小。在FireBug控制檯上沒有錯誤。 如果我嘗試使用F5刷新頁面,圖像會被重新調整大小! 如果我嘗試Ctrl + F5,圖像會以原始尺寸返回!
問題在哪裏?爲什麼這個JavaScript只適用於頁面刷新?
如果你使用'$(「 detailImg」)。負載(函數(){...'會發生什麼事?我懷疑你的腳本運行時,你的圖像不加載,因此寬度()和高度()不能被正確的確定(如果這是問題,你可能需要做更多的工作,因爲'load()'可能不會觸發緩存的圖像。請參閱http://stackoverflow.com/questions/ 910727/jQuery的事件對圖像加載) –
我記得,'$(文檔)時DOM是準備就緒,圖像不能在這一點上加載.ready'觸發。嘗試'$(窗口).load' –