均一性のためにcssでサイズ変更された画像にJcropを使用しています。
JS
<script type="text/javascript">
$(window).load(function() {
//invoke Jcrop API and set options
var api = $.Jcrop('#image', { onSelect: storeCoords, trueSize: [w, h] });
api.disable(); //disable until ready to use
//enable the Jcrop on crop button click
$('#crop').click(function() {
api.enable();
});
});
function storeCoords(c) {
$('#X').val(c.x);
$('#Y').val(c.y);
$('#W').val(c.w);
$('#H').val(c.h);
};
</script>
HTML
<body>
<img src="/path/to/image.jpg" id="image" class="img_class" alt="" />
<br />
<span id="crop" class="button">Crop Photo</span>
<span id="#X" class="hidden"></span>
<span id="#Y" class="hidden"></span>
<span id="#W" class="hidden"></span>
<span id="#H" class="hidden"></span>
</body>
CSS
body { font-size: 13px; width: 500px; height: 500px; }
.image { width: 200px; height: 300px; }
.hidden { display: none; }
h
およびw
変数を実際の画像のサイズに設定する必要があります。マニピュレーターを使用し.clone()
て画像のコピーを作成し、クローンからクラスを削除してサイズを取得しようとしましたが、変数がゼロに設定されます。
var pic = $('#image').clone();
pic.removeClass('image');
var h = pic.height();
var w = pic.width();
ページ内の要素に画像を追加すると機能しますが、これらはより大きな画像であり、これを行うためのより良い方法がある場合は、非表示の画像としてロードしたくないと思います。また、クラスを削除し、変数を設定してから、クラスを再度追加すると、散発的な結果が生成されていました。
私は次のようなものを望んでいました:
$('#image').removeClass('image', function() {
h = $(this).height();
w = $(this).width();
}).addClass('image');
しかし、removeClass
関数はそのようには機能しません:P