javascript - 初始化 cropper.js 时出错

标签 javascript jquery image crop

我正在使用 Cropper.js在我的网站上裁剪照片。我已按照 readme 中的所有步骤进行操作页面,但我遇到了一些错误。我得到的第一个错误是Uncaught ReferenceError: Cropper is not defined。所以我添加了 var Cropper = window.Cropper 语句。当我重新加载页面时,我得到了另一个错误Uncaught TypeError: Cropper is not a constructor。但只有通过这种方式他们才能传递选项到 Cropper 构造函数,无法弄清楚出了什么问题

<!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>

最佳答案

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);
    }
  });

$('#image') 与 document.getElementById('image') 几乎相同,但它将图像元素作为 jQuery 对象返回,该对象具有许多有用的方法。许多插件(如 Cropper.js)将它们自己的方法添加到 jQuery 对象中以使其易于使用。

关于javascript - 初始化 cropper.js 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37474562/

相关文章:

javascript - 尝试从两组字符之间提取数据

javascript - 如何使用 jQuery 来打开和关闭 div

image - 在 codeigniter 中上传图像

javascript - 无法定位之后创建的 li 内的复选框

JavaScript 持续检查一个变量

c# - 单击同级元素时会触发 Click 事件

image - 在 Swift 3 中使用 CGPoint 从图像中获取像素颜色

ios - 为什么当我运行我的应用程序时我的图像不加载?

javascript - 如何拆分从网站上抓取的文本?

javascript - 从 jquery 中的 php jsonstring 解析值