javascript 未捕获的类型错误:hammerit.get不是一个函数- jquery cropbox

t3irkdon  于 2023-05-27  发布在  Java
关注(0)|答案(1)|浏览(137)

使用jquery cropbox时出现此错误
未捕获的类型错误:获取不是一个功能,我还需要作物或下载按钮

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/1.0.5/hammer.js"></script>
<script type="text/javascript"
    src="https://cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.6/jquery.mousewheel.js"></script>
<script type="text/javascript" src="jquery.cropbox.js"></script>
<script type="text/javascript" defer>
    $(function () {
        var r = $('#results'),
            x = $('.cropX', r),
            y = $('.cropY', r),
            w = $('.cropW', r),
            h = $('.cropH', r);
        $('#cropimage').cropbox({
            width: 200,
            height: 200
        }).on('cropbox', function (event, results, img) {
            x.text(results.cropX);
            y.text(results.cropY);
            w.text(results.cropW);
            h.text(results.cropH);
        });
    });
</script>

<img id="cropimage" alt="" src="http://acornejo.github.io/jquery-cropbox/img.jpg" />
    <div id="results"> <b>X</b>: <span class="cropX"></span>
     <b>Y</b>: <span class="cropY"></span>
     <b>W</b>: <span class="cropW"></span>
     <b>H</b>: <span class="cropH"></span>
    
    </div>
<a>Download</a>

错误图像为:

icnyk63a

icnyk63a1#

更新jQuery并在任何其他jQuery插件之前加载它。
同时将crop jQuery和CSS添加到代码片段中。
由于某些原因,最新的hammer.js不工作,所以我离开了它
这在JSFiddle中有效,但由于iFrame安全性,可能在SO上无效

function cropAndSave(src, x, y, w, h) {
  var canvas = document.createElement("canvas");
  //document.body.appendChild(canvas); // for testing
  var ctx = canvas.getContext("2d");
  canvas.width = w;
  canvas.height = h;
  ctx.fillRect(0, 0, w, h);
  ctx.drawImage(src, -x, -y);
  return {
    save: function(filename) {
      var a = document.createElement("a");
      canvas.toBlob(function(blob) {
        a.href = URL.createObjectURL(blob);
        a.download = filename;
        console.log("trying to download",filename);
        a.click();
      });
    }
  }
}

$(function() {
  let img = $("#cropimage")[0];
  let r = $('#results'),
    x = $('.cropX', r),
    y = $('.cropY', r),
    w = $('.cropW', r),
    h = $('.cropH', r);
  $('#cropimage').cropbox({
    width: 200,
    height: 200
  }).on('cropbox', function(event, results, img) {
    x.text(results.cropX);
    y.text(results.cropY);
    w.text(results.cropW);
    h.text(results.cropH);
  });
  $("#save").on("click", (e) => {
    e.preventDefault();
    cropAndSave(img,
      x.text(),
      y.text(),
      w.text(),
      h.text()
    ).save("test");
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/1.0.5/hammer.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.6/jquery.mousewheel.js"></script>
<script src="
https://cdn.jsdelivr.net/npm/jquery-cropbox@0.1.3/jquery.cropbox.min.js
"></script>
<link href="
https://cdn.jsdelivr.net/npm/jquery-cropbox@0.1.3/jquery.cropbox.min.css
" rel="stylesheet">

<img id="cropimage" crossorigin="anonymous" alt="" src="http://acornejo.github.io/jquery-cropbox/img.jpg" />
<div id="results"> <b>X</b>: <span class="cropX"></span>
  <b>Y</b>: <span class="cropY"></span>
  <b>W</b>: <span class="cropW"></span>
  <b>H</b>: <span class="cropH"></span>

</div>
<a href="#" id="save">Download</a>

相关问题