我正在使用jCrop预览演示来做我自己的事情。然而,我遇到了一个问题。如果我在使用setSelect:加载图像时创建了一个默认选择,那么我会将选择映射到大图像上,但它不会出现在预览中。当jCrop加载时,我找不到调用updatePreview函数的api处理程序。有人知道如何在jCrop加载时调用此函数吗?
jQuery(function($){
// Create variables (in this scope) to hold the API and image size
var jcrop_api, boundx, boundy;
$('#target').Jcrop({
onChange: updatePreview,
onSelect: updatePreview,
setSelect: [ 0, 0, selectWidth, selectHeight ],
aspectRatio: 1
},function(){
// Use the API to get the real image size
var bounds = this.getBounds();
boundx = bounds[0];
boundy = bounds[1];
// Store the API in the jcrop_api variable
jcrop_api = this;
});
function updatePreview(c)
{
if (parseInt(c.w) > 0)
{
var rx = 100 / c.w;
var ry = 100 / c.h;
$('#preview').css({
width: Math.round(rx * boundx) + 'px',
height: Math.round(ry * boundy) + 'px',
marginLeft: '-' + Math.round(rx * c.x) + 'px',
marginTop: '-' + Math.round(ry * c.y) + 'px'
});
}
};
});发布于 2011-08-31 17:19:23
我尝试设置默认值,而默认值正在预览框中预览。
jQuery(window).load(function(){
jQuery('#cropbox').Jcrop({
onChange: showPreview,
onSelect: showPreview,
setSelect: [ 100, 0, 100, 100 ],
aspectRatio: 1
});
});这就是我想要的方式。您的脚本中可能存在其他错误。但是你不需要调用任何特殊的东西来显示预览。如果您没有在加载时执行此操作,请尝试在加载时执行此操作。
发布于 2012-08-15 02:29:32
您可以在回调中设置初始选择,而不是animateTo hack。所以:
$('#target').Jcrop({
onChange: updatePreview,
onSelect: updatePreview,
aspectRatio: 1
},function(){
// Use the API to get the real image size
var bounds = this.getBounds();
boundx = bounds[0];
boundy = bounds[1];
// Store the API in the jcrop_api variable
jcrop_api = this;
jcrop_api.setSelect([ 0, 0, selectWidth, selectHeight ]); // <--
});发布于 2011-11-13 00:52:10
我也不能让它工作,我发现你的问题正在寻找解决方案。
我使用的是Jcrop v0.9.9中的预览演示(tutorial3)。
将jQuery(function($){更改为jQuery(window).load(function(){
并添加了选项setSelect: [ 0, 40, 312, 244 ]
而且,它不会在加载时更新预览。
我发现的解决办法是使用animateTo和setSelect接口(如果你喜欢动画,也可以自己使用,其速度可以通过选项swingSpeed进行更改)
我已经对你的代码做了修改
jQuery(function($){
// Create variables (in this scope) to hold the API and image size
var jcrop_api, boundx, boundy;
$('#target').Jcrop({
onChange: updatePreview,
onSelect: updatePreview,
setSelect: [ 0, 0, selectWidth, selectHeight ],
aspectRatio: 1
},function(){
// Use the API to get the real image size
var bounds = this.getBounds();
boundx = bounds[0];
boundy = bounds[1];
// Store the API in the jcrop_api variable
jcrop_api = this;
jcrop_api.animateTo([ 0, 0, selectWidth, selectHeight ]);
});
function updatePreview(c)
{
if (parseInt(c.w) > 0)
{
var rx = 100 / c.w;
var ry = 100 / c.h;
$('#preview').css({
width: Math.round(rx * boundx) + 'px',
height: Math.round(ry * boundy) + 'px',
marginLeft: '-' + Math.round(rx * c.x) + 'px',
marginTop: '-' + Math.round(ry * c.y) + 'px'
});
}
};
});https://stackoverflow.com/questions/7254767
复制相似问题