[英]How Can I Set Dropzone Thumbnail on Element That is Clicked for Browse File?
I have one element as a dropzone and another one is an instance of it;我有一个元素作为拖放区,另一个元素是它的一个实例; both are clickable for browsing the file.
两者都可以单击以浏览文件。
I want to set thumbnail to them based on which one I clicked on it for browsing file.我想根据我点击哪个来浏览文件来为它们设置缩略图。 For example, if I click on element A to browse the file, then I would like to set a thumbnail to an element A. Likewise, if I click on element B to browse the file, then I would like to set a thumbnail to an element B.
例如,如果我点击元素 A 来浏览文件,那么我想为元素 A 设置一个缩略图。同样,如果我点击元素 B 来浏览文件,那么我想为一个元素设置一个缩略图元素 B。
Here is what I have tried, but it did not work:这是我尝试过的方法,但没有用:
$(document).ready(function() { var previewTemplate = document.querySelector('#tpl').innerHTML; var default_option = { url: '/upload', method: "post", autoProcessQueue: false, uploadMultiple: true, maxFiles: 2, parallelUploads: 100, thumbnailWidth: 80, thumbnailHeight: 80, timeout: 0, previewsContainer: '.row-thumbnail', previewTemplate: previewTemplate, clickable: '.a, .b' } var myUploadObj = new Dropzone('#myDz', default_option); });
.upload-container { display: flex; }.a, .b { width: 325px; height: 325px; border: 1px solid; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.7.0/min/dropzone.min.js"></script> <div class="upload-container"> <div class="a" id="myDz"> <div class="dz-message"><span>Element A</span></div> </div> <div class="row-thumbnail dropzone-previews b"> <div class="dz-message"><span>Element B</span></div> </div> </div> <:-- preview template --> <div id="tpl" style="display; none;"> <div class="dz-preview dz-file-preview thumbnail-item"> <div class="dz-image"><img data-dz-thumbnail /></div> </div> </div>
Thanks.谢谢。
I made some changes to your code and tested it out in this fiddle here I think the concept you want to achieve is changing the css property of an element when it is clicked.我对您的代码进行了一些更改并在此处的小提琴中对其进行了测试我认为您想要实现的概念是在单击元素时更改元素的 css 属性。 If you don't want to play with the css background-image property you can always use the :before or :after property to append data before or after your tag (element).
如果您不想使用css背景图像属性,您始终可以在标签(元素)之前或之后使用:before或:after属性来处理 append 数据。
This is the main function responsible for this behavior in the fiddle这是负责小提琴中这种行为的主要 function
$('.class To Be Clicked').click(function() {
$('.class to be changed').css({
"background-image": "url('url to image here')"
});
});
The preview container is determined by the previewsContainer property in the dropzone object. You can take a look at the related source code here - https://github.com/dropzone/dropzone/blob/main/src/dropzone.js line 147 - 156预览容器由 dropzone object 中的 previewsContainer 属性决定。您可以在这里查看相关源代码 - https://github.com/dropzone/dropzone/blob/main/src/dropzone.js line 147 - 156
So you just need to set previewsContainer dynamically whenever a or b is clicked.因此,只要单击 a 或 b,您只需要动态设置 previewsContainer 即可。
$(document).ready(function() {
var previewTemplate = document.querySelector('#tpl').innerHTML;
var default_option = {
url: '/upload',
method: "post",
autoProcessQueue: false,
uploadMultiple: true,
maxFiles: 2,
parallelUploads: 100,
thumbnailWidth: 80,
thumbnailHeight: 80,
timeout: 0,
previewsContainer: '.row-thumbnail',
previewTemplate: previewTemplate,
clickable: '.a, .b'
}
var myUploadObj = new Dropzone('#myDz', default_option);
$(".a").on("click", function(){
myUploadObj.previewsContainer = document.getElementById("myDz");
});
$(".b").on("click", function(){
myUploadObj.previewsContainer = document.getElementById("preview_b");
});
});
To keep it simple, I added id to b:为了简单起见,我将 id 添加到 b:
<div class="row-thumbnail dropzone-previews b" id="preview_b">
That is it.这就对了。 Tested in chrome, works fine.
在 chrome 中测试,工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.