gpt4 book ai didi

javascript - Packery 未初始化 - 无法调用方法,即 $().packery ("bindDraggabillyEvents")

转载 作者:行者123 更新时间:2023-11-30 16:06:22 28 4
gpt4 key购买 nike

我正在尝试在图像上传功能上结合 packery 和 dragabilly。我在同一个函数中初始化它们,但得到了 packery 没有初始化的错误。我尝试将我的代码分成两个函数,一个用于处理图像上传和拖动。另一个处理包装,但我仍然遇到同样的问题。现在图像被拉入并且可以拖动,但 packery 没有被初始化。感谢您的帮助!

JS

function handleFileSelect(evt) {
var files = evt.target.files;

// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
if (!f.type.match('image.*')) {
continue; // only accept image files
}

var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(uploadedFile) {
return function(e) {
var $container = $(".packery")
var div = document.createElement('div');

div.className = "col-3 image-item";
div.innerHTML = ['<div class="handle"></div><img class="thumb" src="', e.target.result,
'" title="', escape(uploadedFile.name), '"/>'].join('');
document.getElementById('packery').insertBefore(div, null);
var draggie = new Draggabilly (div, {
handle: '.handle'
});

$container.packery('bindDraggabillyEvents', draggie);
};
})(f);

// Read in the image file as a data URL.
reader.readAsDataURL(f);
} // end of for loop
} // handlefileselect end

$('#files').change(handleFileSelect);

HTML

<input id="files" multiple="true" name="files[]" type="file" />
<div class="packery" id="packery"></div>

最佳答案

Packery 未在您提供的代码中进行初始化。参见 http://packery.metafizzy.co/#getting-started有关如何初始化的信息。

下面这行没有初始化 packery!

 $container.packery('bindDraggabillyEvents', draggie);

我建议在最后一行之前添加这行代码(分配 on change 事件处理程序的地方):

$('.packery').packery();

关于javascript - Packery 未初始化 - 无法调用方法,即 $().packery ("bindDraggabillyEvents"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984391/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com