Javascript 를 사용하여 비율에 맞게 이미지 크기조정할 방법 아는 사람?
나도 날잡아 dom 을 수정할 수 있지만, '속성' 높이 '와' 너버 추가하기만 즉석에서 IE6 작동하지 못한 것 같다.
단순히 이미지를 수정 비례, css 속성을, 다른 하나는 미디어만을 변경하십시오 너비입니다 / 높이 남겨두십시오 자동으로 설정되었습니다.
image.style.width = '50%'
image.style.height = 'auto'
이를 통해 해당 종횡비 그대로 남아 있습니다.
이미지의 속성을 수정하는 대신 높이와 폭과 높이가 시도하시겠습니까 CSS 수정 너비입니다.
myimg = document.getElementById('myimg');
myimg.style.height = "50px";
myimg.style.width = "50px";
하나의 공통 " gotcha"; 에로남이네 폭과 높이가 스타일은 문장열 유닛 포함하는 등 px" "; 위의 예에서.
편집 - 나도 그렇게 생각해 폭과 높이가 설정 및 스테일스위드스 스티레자에이트 사용하지 않고 직접 노력해야 합니다. 원래 크기 또한 이미 갖고 있는 장점을 가지고 있습니다. 조금 코드에 게시물로의 수 있습니까? # 39, re in 표준 모드 대신 계속하시겠습니까 you& 쿼크 모드?
이 노력해야 합니다.
myimg = document.getElementById('myimg');
myimg.height = myimg.height * 2;
myimg.width = myimg.width * 2;
나는 답변됨 이 여기에 질문: [비율에 맞게 이미지 / 화면 비율을 유지하는 방법을 크기조정할?] [2]. 나는 여기에 복사 때문에 아주 안정적인 방법을 생각합니다:)
/**
* Conserve aspect ratio of the original region. Useful when shrinking/enlarging
* images to fit into a certain area.
*
* @param {Number} srcWidth width of source image
* @param {Number} srcHeight height of source image
* @param {Number} maxWidth maximum available width
* @param {Number} maxHeight maximum available height
* @return {Object} { width, height }
*/
function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) {
var ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);
return { width: srcWidth*ratio, height: srcHeight*ratio };
}
[2]: # 14731922 https://stackoverflow.com/questions/3971841/how-to-resize-images-proportionally-keeping-the-aspect-ratio/14731922
예: 어떻게 조정할 수 있는 내용이다.
<head>
<script type="text/javascript">
var CreateNewImage = function (url, value) {
var img = new Image;
img.src = url;
img.width = img.width * (1 + (value / 100));
img.height = img.height * (1 + (value / 100));
var container = document.getElementById ("container");
container.appendChild (img);
}
</script>
</head>
<body>
<button onclick="CreateNewImage ('http://www.medellin.gov.co/transito/images_jq/imagen5.jpg', 40);">Zoom +40%</button>
<button onclick="CreateNewImage ('http://www.medellin.gov.co/transito/images_jq/imagen5.jpg', 60);">Zoom +50%</button>
<div id="container"></div>
</body>
이 방법은 모든 경우에 있어서.
function resizeImg(imgId) {
var img = document.getElementById(imgId);
var $img = $(img);
var maxWidth = 110;
var maxHeight = 100;
var width = img.width;
var height = img.height;
var aspectW = width / maxWidth;
var aspectH = height / maxHeight;
if (aspectW > 1 || aspectH > 1) {
if (aspectW > aspectH) {
$img.width(maxWidth);
$img.height(height / aspectW);
}
else {
$img.height(maxHeight);
$img.width(width / aspectH);
}
}
}
이거 드세요.
<html>
<body>
<head>
<script type="text/javascript">
function splitString()
{
var myDimen=document.getElementById("dimen").value;
var splitDimen = myDimen.split("*");
document.getElementById("myImage").width=splitDimen[0];
document.getElementById("myImage").height=splitDimen[1];
}
</script>
</head>
<h2>Norwegian Mountain Trip</h2>
<img border="0" id="myImage" src="..." alt="Pulpit rock" width="304" height="228" /><br>
<input type="text" id="dimen" name="dimension" />
<input type="submit" value="Submit" Onclick ="splitString()"/>
</body>
</html>
텍스트 상자 같은 형식으로 50*60 강조표시할 ur 에서 치수값이 열거하십시오 있다. Submit (제출) 을 클릭합니다. 이 크기조정 이미지를 얻을 수 있습니다. 이미지의 이미지 경로에 제공하세요 점의 배치하십시오 태그이고,
Jquery 사용
var scale=0.5;
minWidth=50;
minHeight=100;
if($("#id img").width()*scale>minWidth && $("#id img").height()*scale >minHeight)
{
$("#id img").width($("#id img").width()*scale);
$("#id img").height($("#id img").height()*scale);
}
이미지 크기를 javascript:
$(window).load(function() {
mitad();doble();
});
function mitad(){
imag0.width=imag0.width/2;
imag0.height=imag0.height/2;
}
function doble(){
imag0.width=imag0.width*2;
imag0.height=imag0.height*2;}
imag0 이름은 이미지:
<img src="xxx.jpg" name="imag0">
function resize_image(image, w, h) {
if (typeof(image) != 'object') image = document.getElementById(image);
if (w == null || w == undefined)
w = (h / image.clientHeight) * image.clientWidth;
if (h == null || h == undefined)
h = (w / image.clientWidth) * image.clientHeight;
image.style['height'] = h + 'px';
image.style['width'] = w + 'px';
return;
}
그냥 패스해 img DOM element, 또는 id 하나에 이미지 요소 및 새로운 폭과 높이를.
그냥 가공할지 너비입니다 인컨텍스트 통과시킬 수 있고 높이 (또는 null 로 너비입니다 경우 단순히 높이 관심용 전달하십시오 정의되지 않은) 크기조정할 화면 비율을 유지할 것
이것은 내 빽이라는 칠 솔루션 (비슷한 배경 크기: 하지만, IE 는 빽이라는 오래된 브라우저)
<div class="imgContainer" style="height:100px; width:500px; overflow:hidden; background-color: black">
<img src="http://dev.isaacsonwebdevelopment.com/sites/development/files/views-slideshow-settings-jquery-cycle-custom-options-message.png" id="imgCat">
</div>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
<script>
$(window).load(function() {
var heightRate =$("#imgCat").height() / $("#imgCat").parent(".imgContainer").height();
var widthRate = $("#imgCat").width() / $("#imgCat").parent(".imgContainer").width();
if (window.console) {
console.log($("#imgCat").height());
console.log(heightRate);
console.log(widthRate);
console.log(heightRate > widthRate);
}
if (heightRate <= widthRate) {
$("#imgCat").height($("#imgCat").parent(".imgContainer").height());
} else {
$("#imgCat").width($("#imgCat").parent(".imgContainer").width());
}
});
</script>