我正在尝试以引导程序模式上传和裁剪图像,并且遵循了codepen example。除了jquery,bootstrap等外,我没有导入任何裁剪库。我收到错误消息,
“未捕获的ReferenceError:未定义imageCrop
在cropImage(upload-image.js?v = 20180410173156:55)
在Image.img.onload(upload-image.js?v = 20180410173156:41)”
我应该使用任何库还是要编写任何JavaScript?我是javascript新手
我的Java语言:
$('#drop').on('click', function() {
$('#fileUpload').trigger('click');
});
$('#fileUpload').on('change', function(e) {
addImage(e.target);
});
$("#drop").on("dragover", function(e) {
e.preventDefault();
e.stopPropagation();
$(this).addClass('dragging');
});
$("#drop").on("dragleave", function(e) {
$(this).removeClass('dragging');
});
$("#drop").on("drop", function(e) {
e.preventDefault();
e.stopPropagation();
var data = e.dataTransfer || e.originalEvent.dataTransfer;
addImage(data);
});
function addImage(data) {
var file = data.files[0];
if (file.type.indexOf('image') === -1) {
alert('Sorry, the file you uploaded doesn\'t appear to be an image.');
return false;
}
var reader = new FileReader();
reader.onload = function(event) {
var img = new Image();
img.onload = function() {
if (img.width < 200 || img.height < 200) {
alert('Sorry, the image you uploaded doesn\'t appear to be large enough.');
return false;
}
cropImage(img);
}
img.src = event.target.result;
}
reader.readAsDataURL(file);
}
function cropImage(originalImage) {
$(originalImage).attr('id', 'fullImage');
$('#imageResize').html(originalImage);
// $('#sectionDragAndDrop').addClass('hidden');
$('#sectionResize').removeClass('hidden');
var newImage = new imageCrop('#fullImage', 200, 200);
$('#crop').on('click', function() {
var results = newImage.crop();
$('#thumbnail').html(results.img);
// $('#sectionResize').addClass('hidden');
$('#sectionThumbnail').removeClass('hidden');
});
}
最佳答案
您需要在codepen链接之后包括imageCrop
定义,您将在JS设置中的外部脚本ImageCrop中找到该脚本。