2013-12-13 5 views
1

jQuery의 새로운 기능입니다. 145x190, 140X180 등과 같은 픽셀에 따라 이미지를 선택하고 싶습니다. 비율로 할 수는 있지만 픽셀이나 치수에 맞지는 않습니다. pls 날 도와. 내 코드는 다음과 같습니다 당신이 try my fiddle 사용자 정의 미리보기 이미지를 만들기 위해 이미지에서 선택합니다 Imgareaselect에서 픽셀 단위로 이미지를 선택할 수있는 방법

function preview(img, selection) { 
    var scaleX = 100/(selection.width || 1); 
    var scaleY = 100/(selection.height || 1); 

    $('#ferret + div > img').css({ 
     width: Math.round(scaleX * 400) + 'px', 
     height: Math.round(scaleY * 300) + 'px', 
     marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', 
     marginTop: '-' + Math.round(scaleY * selection.y1) + 'px', 

    }); 
} 

$(document).ready(function() { 
    $('<div> <img src="http://jotform.org/demo/jquery-image-area-select-plugin/images/sweet-dogs.jpg" style="position: relative;"> </div>') 
     .css({ 
      float: 'right', 
      position: 'relative', 
      overflow: 'hidden', 
      width: '100px', 
      height: '100px' 
     }) 
     .insertAfter($('#ferret')); 

    $('#ferret').imgAreaSelect({ aspectRatio: '1:1', onSelectChange: preview ,minWidth: 100,minHeight: 100}); 
}); 

+0

이전에 imgAreaSelect를 사용 해본 적이 없지만 고정 크기의 상자를 원할 경우 최소 너비와 최대 너비 및 높이를 지정할 수 있습니다 :'$ ('# ladybug_ant'). imgAreaSelect ({maxWidth : 200, maxHeight : 150, minWidth : 200, minHeight : 150, handles : true});'http://odyniec.net/projects/imgareaselect/examples.html 첫 번째 무당 벌레 이미지 – HMR

답변

0

죄송합니다.

HTML

<div class="container"> 
    <div> 
     <input id="thumbWidth" placeholder="width" value="200" /> 
     <input id="thumbHeight" placeholder="height" value="200" /> 
    </div> 
    <p> 
     <img id="ferret" src="http://odyniec.net/projects/imgareaselect/ferret.jpg" alt="It's coming right for us!" title="It's coming right for us!" style="float: left; margin-right: 10px;" /> 
    </p> 
</div> 

JAVASCRIPT

: 아래의 코드를 사용하는 경우 동적으로 width and height through the textboxes의 값을 변경할 수 있습니다 제외

, 그것은, 내 위의 예와 동일하게 수행

$(document).ready(function() { 
var defaultVal = 100; 
var thumbWidth = $('#thumbWidth').val() | defaultVal, 
    thumbHeight = $('#thumbHeight').val() | defaultVal; 
$('#thumbWidth').on("change keyup", function() { 
    thumbWidth = this.value | defaultVal; 
    reLoad('cancel'); 
}); 
$('#thumbHeight').on("change keyup", function() { 
    thumbHeight = this.value | defaultVal; 
    reLoad('cancel'); 
}); 


function preview(img, selection) { 
    var scaleX = thumbWidth/(selection.width || 1); 
    var scaleY = thumbHeight/(selection.height || 1); 

    $('#ferret + div > img').css({ 
     width: Math.round(scaleX * $("#ferret").width()) + 'px', 
     height: Math.round(scaleY * $("#ferret").height()) + 'px', 
     marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', 
     marginTop: '-' + Math.round(scaleY * selection.y1) + 'px' 
    }); 
} 
var reLoad = function (cancel) { 
    $('.premove').remove(); 
    $('<div class="premove"><img src="http://odyniec.net/projects/imgareaselect/ferret.jpg" style="position: relative;" /><div>') 
     .css({ 
     float: 'left', 
     position: 'relative', 
     overflow: 'hidden', 
     width: thumbWidth + 'px', 
     height: thumbHeight + 'px' 
    }) 
     .insertAfter($('#ferret')); 

    var fer = $('#ferret').imgAreaSelect({ 
     aspectRatio: thumbWidth + ':' + thumbHeight, 
     onSelectChange: preview, 
     instance: true 
    }); 
    if (cancel) fer.cancelSelection(); 
} 
reLoad(); 

});

+0

텍스트 상자 값은 이미지의 영역을 자동으로 선택하지 않으므로 수동으로해야한다. 새로운 '비율'에 따라 자동으로 영역을 선택하는 간단한 스크립트를 작성할 수 있습니다. –

+0

지원해 주셔서 감사합니다 .... – sonalgoyal

1

... 당신이 내 질문을 이해 바랍니다. javascript 섹션으로 가서 thumbWidththumbHeight의 값을 변경하십시오. 기본적으로 미리보기 이미지를 원하는 크기로 조정하고 해당 비율을 aspectRatio으로 사용합니다. 좀 피곤 sloppyness에 대한

자바 스크립트 코드

var thumbWidth = 200, thumbHeight = 200; 

function preview(img, selection) { 
    var scaleX = thumbWidth/(selection.width || 1); 
    var scaleY = thumbHeight/(selection.height || 1); 

    $('#ferret + div > img').css({ 
     width: Math.round(scaleX * $("#ferret").width()) + 'px', 
     height: Math.round(scaleY * $("#ferret").height()) + 'px', 
     marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', 
     marginTop: '-' + Math.round(scaleY * selection.y1) + 'px' 
    }); 
} 

$(document).ready(function() { 

    $('<div><img src="http://odyniec.net/projects/imgareaselect/ferret.jpg" style="position: relative;" /><div>') 
     .css({ 
      float: 'left', 
      position: 'relative', 
      overflow: 'hidden', 
      width: thumbWidth + 'px', 
      height: thumbHeight + 'px' 
     }) 
     .insertAfter($('#ferret')); 

    $('#ferret').imgAreaSelect({aspectRatio: thumbWidth+':'+thumbHeight, onSelectChange: preview }); 
}); 
+0

을 클릭하십시오. – sonalgoyal

+0

나는 당신을 위해 더 많은 것을 가지고있다 –

관련 문제