首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jCrop - update preview onload?

jCrop - update preview onload?
EN

Stack Overflow用户
提问于 2011-08-31 17:02:59
回答 3查看 8.4K关注 0票数 4

我正在使用jCrop预览演示来做我自己的事情。然而,我遇到了一个问题。如果我在使用setSelect:加载图像时创建了一个默认选择,那么我会将选择映射到大图像上,但它不会出现在预览中。当jCrop加载时,我找不到调用updatePreview函数的api处理程序。有人知道如何在jCrop加载时调用此函数吗?

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

    });
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-31 17:19:23

我尝试设置默认值,而默认值正在预览框中预览。

代码语言:javascript
复制
jQuery(window).load(function(){

            jQuery('#cropbox').Jcrop({
                onChange: showPreview,
                onSelect: showPreview,
                setSelect: [ 100, 0, 100, 100 ],
                aspectRatio: 1
            });

        });

这就是我想要的方式。您的脚本中可能存在其他错误。但是你不需要调用任何特殊的东西来显示预览。如果您没有在加载时执行此操作,请尝试在加载时执行此操作。

票数 3
EN

Stack Overflow用户

发布于 2012-08-15 02:29:32

您可以在回调中设置初始选择,而不是animateTo hack。所以:

代码语言:javascript
复制
$('#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 ]); // <--
  });
票数 8
EN

Stack Overflow用户

发布于 2011-11-13 00:52:10

我也不能让它工作,我发现你的问题正在寻找解决方案。

我使用的是Jcrop v0.9.9中的预览演示(tutorial3)。

jQuery(function($){更改为jQuery(window).load(function(){

并添加了选项setSelect: [ 0, 40, 312, 244 ]

而且,它不会在加载时更新预览。

我发现的解决办法是使用animateTosetSelect接口(如果你喜欢动画,也可以自己使用,其速度可以通过选项swingSpeed进行更改)

我已经对你的代码做了修改

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

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

https://stackoverflow.com/questions/7254767

复制
相关文章

相似问题

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