作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 signup.html
中有一个模式,它应该显示图像裁剪器,来自 cropper.js上传文件时。但是,如果我尝试更改正在上传的文件,模式会显示我使用的第一个文件。我做错了什么?
P.S.: 我是第一次使用 cropper.js。
这是我的代码:
注册.html:
{% extends 'base.html' %}
{% load static %}
{% block content %}
<header>
<img src="{% static 'img/logo-min.png' %}" alt="Logo-min">
</header>
<div class="card-signup">
<form enctype="multipart/form-data" method="POST" class="post-form">
<h1>+ Cadastro de novo aluno</h1>
{% csrf_token %}
{{ credentials_form }}
{{ personal_info_form }}
<button type="submit" class="save btn btn-default">Confirmar</button>
</form>
</div>
<div id="modal">
<div class="canvas">
<a class="close">×</a>
<img class="modal-content" id="image">
<button class="crop">Cortar</button>
</div>
</div>
{% endblock %}
image_cropper.js:
showModal = () => {
let modal = document.querySelector('#modal');
let image = document.querySelector('#image');
let file = document.querySelector('input[type=file]').files[0];
let fileReader = new FileReader();
fileReader.onload = () => {
modal.style.display = 'flex';
image.src = fileReader.result;
let cropper = new Cropper(image, {
dragMode: 'move',
aspectRatio: 1 / 1,
autoCropArea: 0.65,
restore: false,
guides: false,
center: false,
highlight: false,
cropBoxMovable: false,
cropBoxResizable: false,
toggleDragModeOnDblclick: false,
});
let crop_button = document.querySelector('.crop');
crop_button.onclick = cropper.crop;
}
if (file) {
fileReader.readAsDataURL(file);
} else {
image.src = '';
}
}
closeModal = () => {
let modal = document.querySelector('#modal');
picture_input.value = '';
modal.style.display = 'none';
}
let picture_input = document.querySelector('#id_picture');
picture_input.onchange = showModal;
let close_button = document.querySelector('.close');
close_button.onclick = closeModal;
文件输入由 Django 渲染,但它看起来像这样:
<input name="picture" accept="image/*" required="" id="id_picture" type="file">
最佳答案
要删除图像实例,您需要使用可用的destroy()
方法。在我的代码中,我是这样写的:
close_button.onclick = closeModal = () => {
modal.style.display = 'none';
cropper.destroy();
}
关于javascript - 图片上传不起作用(在模态上处理 cropper.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52998130/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!