How to use Layui to develop a drag-and-drop sorting image upload function
Introduction:
In today's Internet era, image uploading has become a part of our daily life One of the common requirements. In the actual development process, the drag-and-drop sorting function can not only improve the user experience for image uploading, but also effectively manage the sorting of images. Layui is a simple and easy-to-use front-end UI framework based on Nodejs. It provides powerful functions and rich styles, and is very suitable for developing drag-and-drop sorting image upload functions.
This article will introduce how to use Layui to develop a drag-and-drop sorting image upload function, and provide specific code examples to facilitate practical application.
<div class="upload-container"> <div class="uploaded-list" id="uploaded-list"></div> <div class="drop-area"> <div class="drop-text">将图片拖拽到此处上传</div> <div class="upload-button">点击上传图片</div> <input type="file" id="file-input" multiple="multiple" style="display: none;"> </div> </div>
.upload-container { width: 500px; margin: 0 auto; padding: 20px; box-sizing: border-box; } .uploaded-list { margin-bottom: 20px; } .drop-area { border: 1px dashed #ccc; height: 200px; text-align: center; line-height: 200px; background: #f7f7f7; cursor: pointer; position: relative; } .drop-text { color: #999; } .upload-button { position: absolute; top: 0; left: 0; width: 100%; height: 100%; text-align: center; line-height: 200px; cursor: pointer; background: rgba(0, 0, 0, 0.5); color: #fff; display: none; } .upload-button:hover { background: rgba(0, 0, 0, 0.7); }
<script> layui.use('upload', function(){ var upload = layui.upload; //执行实例 var uploadInst = upload.render({ elem: '#file-input', //绑定元素 url: '/upload', //上传接口 data: {}, //可选项,额外的参数 multiple: true, //是否允许多文件上传 done: function(res){ //上传完毕回调 console.log(res); }, error: function(){ //请求异常回调 } }); //拖拽上传 var dropArea = document.querySelector('.drop-area'); var uploadButton = document.querySelector('.upload-button'); var fileInput = document.querySelector('#file-input'); dropArea.addEventListener('dragover', function(e) { e.preventDefault(); this.classList.add('highlight'); }); dropArea.addEventListener('dragleave', function(e) { e.preventDefault(); this.classList.remove('highlight'); }); dropArea.addEventListener('drop', function(e) { e.preventDefault(); this.classList.remove('highlight'); fileInput.files = e.dataTransfer.files; uploadButton.click(); }); //显示已上传的图片 uploadInst.config.done = function(res) { var uploadedList = document.querySelector('.uploaded-list'); var img = document.createElement('img'); img.src = res.url; uploadedList.appendChild(img); }; }); </script>
Code description:
Summary:
It is not difficult to develop a drag-and-drop sorting image upload function using Layui. You only need to prepare the corresponding HTML layout, CSS style and JavaScript code. Through Layui's upload module and drag-and-drop function, we can easily implement drag-and-drop upload and display uploaded images. I hope my sharing can be helpful to you, thank you for reading!
The above is the detailed content of How to use Layui to develop a drag-and-drop sorting image upload function. For more information, please follow other related articles on the PHP Chinese website!