Jquery Dropzone.js 将缩略图宽度更改为 100%

Jquery Dropzone.js change thumbnail width to 100%

我正在使用 Dropzone.js 允许用户将文件上传到服务器,根据规范您可以更改缩略图宽度,如下所示,但是我想将宽度更改为 100% 而不是使用 px , 这可能吗?

因为如果我这样做 thumbnailWidth: 100% 无法识别 % 字符。

    dzImageOptions = Dropzone.options.myDropzone = {
        thumbnailWidth: 314, //I want to change width to 100% instead
        thumbnailHeight: 314,
        init: function (file) {

        }
}
    //Also have to change css or thumbnail won't resize properly
    .dropzone.song-image .dz-preview .dz-image {
    border-radius: 1px;
    width: 314px;
    height: 314px;
}

<div class="dropzone song-image"></div>

您不能在 thumbnailWidththumbnailHeight 上指定百分比。 Dropzone 使用这些值来创建图像源以将其显示为预览。

但是您可以保留缩略图的原始宽度和高度,将这些值设置为 null请注意,这可能会导致高分辨率图像出现一些延迟) 然后使用 <img> 宽度和高度属性以您想要的大小显示图像,并使用 css.

调整 .dz-image 容器

html:

<div class="dropzone" id="myDropzone"></div>

js:

Dropzone.autoDiscover = false;

Dropzone.options.myDropzone = {
    url: "yourUrl",
    thumbnailWidth: null,
    thumbnailHeight: null,
    init: function() {
        this.on("thumbnail", function(file, dataUrl) {
            $('.dz-image').last().find('img').attr({width: '100%', height: '100%'});
        }),
        this.on("success", function(file) {
            $('.dz-image').css({"width":"100%", "height":"auto"});
        })
    }
};

var myDropzone = new Dropzone('div#myDropzone');
var myDropZone = jQuery("#file128_dropzone").get(0).dropzone;
myDropZone.options.thumbnailWidth = 250;
myDropZone.options.thumbnailHeight = 250;

我需要使用 dropzone 完成响应式缩略图,这 post 帮了我大忙。我还需要在没有 jquery 的情况下完成它,所以这就是我想出的。我想如果它对其他人有帮助我会分享。

我的 dropzone 初始化函数如下所示:

init: function () {
    this.on('thumbnail', function(file, dataUrl) {
        var thumbs = document.querySelectorAll('.dz-image');
        [].forEach.call(thumbs, function (thumb) {
            var img = thumb.querySelector('img');
            if (img) {
                img.setAttribute('width', '100%');
                img.setAttribute('height', '100%');
            }
        });
    }),
    this.on('success', function(file) {
        var thumbs = document.querySelectorAll('.dz-image');
        [].forEach.call(thumbs, function (thumb) {
            thumb.style = 'width: 100%; height: auto;';
        });
    })
}

我不是 javascript 向导,因此可能有更有效或更好的方法来执行此操作。请分享!

尝试了很长时间后,以下更改对我有用。

HTML代码:

<form action="/" class="dropzone" method="post" id="myAwesomeDropzone">

                                </form>

JS代码:

            Dropzone.autoDiscover = false;

        $(document).ready(function()
        {

            debugger;

            $("form#myAwesomeDropzone").dropzone ({

                acceptedFiles: ".png,.jpg,.gif,.bmp,.jpeg",

                thumbnailWidth: null,

                thumbnailHeight: null,

                init:function()
                {

                    debugger;

                    var self = this;

                    self.on("thumbnail", function(file, dataUrl)
                    {

                        debugger;

                        $('.dz-preview').css({"width":"100%", "height":"150px", "margin":"0px"});
                        $('.dz-image').css({"width":"100%", "height":"150px"});

                    }),

                    self.on("success", function(file)
                    {

                        debugger;

                        $('.dz-image').css({"width":"240px", "height":"240px"});

                    })

                }

            });

        });

根据@wallek876 的回答,我尝试了一种更简单的方法来做到这一点。我会直接进入相关部分:

init: function() {
    this.on("success", function(file) {
        $('.dz-image img').css({"width":"100%", "height":"auto"});
    })
}