我一直在試圖使imgareaselect插件工作縮放圖像,圖像是動態縮放使用CSS最大寬度:100%和最大高度:100%,使其適合它的容器。imgareaselect對縮放圖像與預覽不能正常工作
正如你可以在上面小提琴,預覽沒有顯示在選擇同樣的事情看,嘗試選擇樹,你可以清楚地看到它後面的視野。
HTML:
<div id="container">
<img src="http://upload.wikimedia.org/wikipedia/commons/e/eb/Ash_Tree_-_geograph.org.uk_-_590710.jpg" id="croptarget">
<div id="preview-cont">
<img src="http://upload.wikimedia.org/wikipedia/commons/e/eb/Ash_Tree_-_geograph.org.uk_-_590710.jpg" id="preview">
</div>
</div>
的Jquery與imgareaselect插件
$(document).ready(function(){
function preview(img, selection) {
if (!selection.width || !selection.height)
return;
var scaleX = 150/selection.width;
var scaleY = 150/selection.height;
$('#preview').css({
width: Math.round(scaleX * 300),
height: Math.round(scaleY * 300),
marginLeft: -Math.round(scaleX * selection.x1),
marginTop: -Math.round(scaleY * selection.y1)
});
}
//dynamic aspect ratio
var daspectratio = $('#croptarget').height()/$('#croptarget').width();
var paspectratio = $('#preview-cont').height()/$('#preview-cont').width();
var dyap =daspectratio+":" + paspectratio;
$('#croptarget').imgAreaSelect({
aspectRatio: dyap,
handles: true,
fadeSpeed: 200,
onSelectChange: preview
});
});
CSS
#container{
background-color:#ccc;
width:400px;
height:300px;
}
#croptarget{
max-width:100%;
max-height:100%;
}
#preview-cont{
width:150px;
height:150px;
overflow:hidden;
border:1px solid #000;
float:right;
}
你可以看到我嘗試做一個動態的縱橫比,我認爲它不工作太。