首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调整主图片大小后,jcrop demo不再起作用(更新图片)

问调整主图片大小后,jcrop demo不再起作用(更新图片)
EN

Stack Overflow用户
提问于 2010-11-02 00:12:32
回答 2查看 2.7K关注 0票数 2

我正在尝试replicate this jcrop demo。一切都很好,除了我的原始图片非常大,所以,在我的页面上的as per the manual, i am resizing them如下:

代码语言:javascript
复制
 jQuery('#cropbox').Jcrop({
            onChange: showPreview,
            onSelect: showPreview,
            bgColor: 'white',
            aspectRatio: 1,
            boxWidth: 300,
            boxHeight: 500
        });

现在的问题是第二个预览窗口(id=“预览”)不再显示裁剪框上裁剪部分的内容。下面是一个示例:

​

​

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

当你预先调整主图像的大小时,你知道如何让预览图像正确显示吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-09 17:54:34

试试this JSFiddle

html:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
#previewcrop{
    width: 100px;
    height: 100px;
    overflow: hidden;
    margin-left: 5px;
}

js:

代码语言:javascript
复制
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'
    });
};
票数 4
EN

Stack Overflow用户

发布于 2010-11-04 13:17:34

代码语言:javascript
复制
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并修改它)。此外,不要使用缩放图像的宽度和高度,它将不起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4070731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档