2012-07-17 2 views
2

내가 이미지에 onLoad 및하여 onResize 크기를 조정하려면이 기능을 사용하는 스크립트가하여 onResize 설정 :는 최대 높이

/** 
* Calculates the display width of an image depending on its display height when it is resized. 
* @param displayHeight the resized height of the image 
* @param originalHeight the original height of the image 
* @param originalWidth the original width of the image 
* @return the display width 
*/ 
function getDisplayWidth(displayHeight, originalHeight, originalWidth){ 
    var ratio = originalHeight/displayHeight, 
     res = Math.round(originalWidth/ratio) || 1000; 
    return res; 
} 

을 ..하지만 난 800 픽셀을 초과하는 이미지 높이 싶지 않아, 가리키고 심지어 될 수 있습니다 800 × 530px 크기로 고정. res에 고정 값을 반환하려고했지만 작동하지 않는 것 같습니다.

감사합니다.

답변

2

당신은 그냥 ... 함수에 if 문을 추가 할 너비를 설정하면

/** 
* Calculates the display width of an image depending on its display height when it is resized. 
* @param displayHeight the resized height of the image 
* @param originalHeight the original height of the image 
* @param originalWidth the original width of the image 
* @return the display width 
*/ 
function getDisplayWidth(displayHeight, originalHeight, originalWidth){ 
    if (displayHeight > 800) displayHeight = 800; 
    var ratio = originalHeight/displayHeight, 
     res = Math.round(originalWidth/ratio) || 1000; 
    return res; 
} 

자동 너비/높이 비율의 정확한 값으로 높이를 설정합니다 필요합니다. getDisplayWidth에 변수를 전달하여 높이를 가져올 수도 있습니다. 그러면 함수가 해당 변수를 반환 할 때 최대 높이 조건에 의해 정의 된 높이가됩니다.

+0

효과가 없으므로 스크립트 전체 : http://pastebin.com/GVgzRNrF – 3zzy