2

我正在尝试复制这个 jcrop 演示。一切正常,除了我的原始图片非常大,所以按照手册,我在我的页面上调整它们的大小,如下所示:

 jQuery('#cropbox').Jcrop({
            onChange: showPreview,
            onSelect: showPreview,
            bgColor: 'white',
            aspectRatio: 1,
            boxWidth: 300,
            boxHeight: 500
        });

现在的问题是第二个预览窗口 ( id="preview" ) 不再显示裁剪框上裁剪部分中的内容。这是一个例子:

替代文字

显然,预览窗口与我在第一张图片中裁剪的区域不匹配。

知道如何在预先调整主图像大小时正确显示预览图像吗?

4

2 回答 2

4

试试这个 JSFiddle

html:

<img id="cropbox" src="http://homepage.mac.com/davydewit/popacademie/rw_common/themes/blendit/images/header/image1.jpg" />

<br />

<br />
<div id="previewcrop">
<img id="preview" src="http://homepage.mac.com/davydewit/popacademie/rw_common/themes/blendit/images/header/image1.jpg" />
</div>

CSS:

#previewcrop{
    width: 100px;
    height: 100px;
    overflow: hidden;
    margin-left: 5px;
}

js:

var imgwidth = 849; // width of the image
var imgheight = 423; // height of the image

jQuery('#cropbox').Jcrop({
            onChange: showPreview,
            onSelect: showPreview,
            bgColor: 'white',
            aspectRatio: 1,
            boxWidth: imgwidth/3,
            boxHeight: imgheight/3
        });

function showPreview(coords)
{
    var rx = 100 / coords.w;
    var ry = 100 / coords.h;

    $('#preview').css({
        width: Math.round(rx * imgwidth) + 'px',
        height: Math.round(ry * imgheight) + 'px',
        marginLeft: '-' + Math.round(rx * coords.x) + 'px',
        marginTop: '-' + Math.round(ry * coords.y) + 'px'
    });
};
于 2010-11-09T09:54:34.040 回答
0
function showPreview(coords)
{
    if (parseInt(coords.w) > 0)
    {
        var rx = 100 / coords.w;
        var ry = 100 / coords.h;

        jQuery('#preview').css({
            width: Math.round(rx * 800) + 'px',
            height: Math.round(ry * 600) + 'px',
            marginLeft: '-' + Math.round(rx * coords.x) + 'px',
            marginTop: '-' + Math.round(ry * coords.y) + 'px'
        });
    }
}

对于 (rx * 800),800 应该是真实图像的宽度。对于 (ry * 600),600 应该是真实图像的宽度。我已经针对 800x600 图像对此进行了测试,并且可以正常工作(使用 Tutorial3.html 并对其进行修改)。另外,不要使用缩放图像的宽度和高度,它不起作用。

于 2010-11-04T05:17:33.933 回答