【问题标题】:Adding cropper function to dynamically created image为动态创建的图像添加裁剪功能
【发布时间】:2016-01-14 14:23:03
【问题描述】:

我输入了网址和“预览”按钮。 当我单击“预览”时,它会在“提交”按钮之前从该 URL 插入图像。

  $('#preview').click(function() {
    var image_url = $('#event_remote_flyer_url').val();
    var image_tag = $('<img class="preview">');
    image_tag.attr('src',image_url)
    $('input[name="commit"]').before(image_tag);
  });

现在我想为这张图片添加裁剪器插件。 (https://github.com/fengyuanchen/cropper)

文档说我应该这样使用它:

$('img.preview').cropper({
  aspectRatio: 16 / 9,
  crop: function(e) {
    // Output the result data for cropping image.
    console.log(e.x);
    console.log(e.y);
    console.log(e.width);
    console.log(e.height);
    console.log(e.rotate);
    console.log(e.scaleX);
    console.log(e.scaleY);
  }
});

它不起作用。据我了解,javascript 不会等待此裁剪器功能,而是在尚未插入 img.preview 时运行它。我该如何解决这个问题?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您需要在创建img.preview 元素时附上它,并且您的理解是正确的。

    $('#preview').click(function() {
        var image_url = $('#event_remote_flyer_url').val();
        var image_tag = $('<img class="preview">');
        image_tag.attr('src', image_url)
            //Atach Cropper
        image_tag.cropper({
            aspectRatio: 16 / 9,
            crop: function(e) {}
        });
        //Append
        $('input[name="commit"]').before(image_tag);
    });
    

    【讨论】:

    • 现在我看到了裁剪网格,但图片没有上传。
    • 并得到这个错误:Image from origin 'https://fbcdn-sphotos-b-a.akamaihd.net' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2017-05-21
    • 2012-11-18
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多