2016-05-27 156 views
3

我使用Cropper.js裁剪照片在我的website.i已按照readme页面中的所有步骤,但我得到了一些和error.The第一个错误我得到的是未捕获的ReferenceError:是克罗珀没有定义。所以我已经添加了 VAR克罗珀= window.Cropper statement.when我重装我得到了另一个错误的页面遗漏的类型错误:克罗珀是不是构造。但以这种方式仅他们传递的选项到Cropper的构造函数,找不到出了什么问题错误而初始化cropper.js

<!doctype html> 
<html lang="en"> 
<head> 
    <title>Cropper</title> 
    <link rel="stylesheet" href="../dist/cropper.css"> 
    <style> 
    img { 
     max-width: 100%; 
    } 
    </style> 
</head> 
<body> 

    <div> 
    <img id="image" src="wallpaper.jpg"> 
    </div> 

    <div id="preview" ></div> 

    <!-- Scripts --> 
    <script src="../assets/js/jquery.min.js"></script> 
    <script src="../dist/cropper.js"></script> 
    <script> 
     var Cropper = window.Cropper; 
     var image = document.getElementById('image'); 
     var cropper = new Cropper(image, { 
     aspectRatio: 16/9, 
     crop: function(e) { 
      console.log(e.detail.x); 
      console.log(e.detail.y); 
      console.log(e.detail.width); 
      console.log(e.detail.height); 
      console.log(e.detail.rotate); 
      console.log(e.detail.scaleX); 
      console.log(e.detail.scaleY); 
     } 
     }); 
    </script> 
</body> 
</html> 
+0

听起来像你还没有包括'cropper.js'文件。你是否确定文件确实位于'../ dist/cropper.js'?看起来很可能不是 – DelightedD0D

+0

在您提供给Cropper示例的链接中,我没有看到任何全局的'Cropper'对象,因此您的方法也会在那里失败。我建议你仔细阅读文档。 – Robusto

+0

在他们要求初始化裁剪像Window.cropper – codegeek

回答

4

Cropper(不要与Cropper.js混淆)是指具有jQuery的工作,所以你需要使用它通过一个jQuery对象,像这样:

var image = $('#image'); 
    var cropper = image.cropper({ 
    aspectRatio: 16/9, 
    crop: function(e) { 
     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); 
    } 
    }); 

的$(“#图像”)是非常与document.getElementById('image')是一样的,但它将图像元素作为一个jQuery对象返回,它有很多有用的方法。像Cropper.js这样的插件可以将自己的方法添加到jQuery对象中,以方便使用。

+0

Hi @peter,我运行代码像上面的建议。但我得到的错误无法读取属性'x'的undefined – codegeek

+0

哎呀。对于那个很抱歉。你只需要把.detail部分拿出来。我编辑了我的答案。 –

+0

感谢peter工作 – codegeek