This article uses jQuery to implement a drag-and-drop function to delete small desktop icons. It works just like the recycle bin of the operating system. We only need to drag the application icon to the trash can to delete the icon. Share it with everyone and implement it in detail. The method is as follows
Operation rendering:
Introducing core files
Here you need to introduce jquery, jquery ui, and jquery ui css
<link rel="stylesheet" href="assets/css/jquery-ui.css" /> <script src="js/jquery/1.8.3/jquery.min.js"></script> <script src="js/jqueryui/1.9.2/jquery-ui.min.js"></script>
Build html
<div id="main"> <div class="folder"> <div class="front"></div> <div class="back"></div> </div> <img src="assets/48px/Implement dragging icons to the recycle bin and deleting them based on jQuery_jquery.png" style="max-width:90%" alt="Implement dragging icons to the recycle bin and deleting them based on jQuery_jquery" /> <img src="assets/48px/calculator.png" style="max-width:90%" alt="calculator" /> <img src="assets/48px/clipboard.png" style="max-width:90%" alt="clipboard" /> <img src="assets/48px/console.png" style="max-width:90%" alt="console" /> <img src="assets/48px/basketball.png" style="max-width:90%" alt="basketball" /> <img src="assets/48px/facebook.png" style="max-width:90%" alt="facebook" /> <img src="assets/48px/gift.png" style="max-width:90%" alt="gift" /> <img src="assets/48px/id_card.png" style="max-width:90%" alt="id card" /> <img src="assets/48px/imac.png" style="max-width:90%" alt="imac" /> <img src="assets/48px/system_monitoring.png" style="max-width:90%" alt="system monitoring" /> </div>
Core CSS Styles
Friends who have no CSS3 foundation, please understand CSS3 first, otherwise the following CSS will be difficult
/*---------------------------- CSS3文件夹 -----------------------------*/ .folder { /* This will enable the 3D effect. Decrease this value * to make the perspective more pronounced: */ -webkit-perspective: 800px; -moz-perspective: 800px; perspective: 800px; /*镜头距离800PX*/ position: absolute; top: 50%; left: 50%; z-index: 0; width: 160px; height: 120px; margin: -100px 0 0 -60px; } .folder div{ width:150px; height:115px; background-color:#93bad8; /* 3D变化保留元素的位置 */ -webkit-transform-style: preserve-3d; -moz-transform-style: preserve-3d; transform-style: preserve-3d; /*平滑的动画过渡 */ -webkit-transition:0.5s; -moz-transition:0.5s; transition:0.5s; /*禁止用户选中元素*/ -webkit-user-select: none; -moz-user-select: none; user-select: none; position:absolute; top:0; left:50%; margin-left:-75px; } .folder .front{ /*圆角,X轴3D转换30度 */ border-radius:5px 5px 0 0; -moz-transform:rotateX(-30deg); -webkit-transform:rotateX(-30deg); transform:rotateX(-30deg); /*定义在X轴与Y轴的位置 */ -moz-transform-origin:50% 100%; -webkit-transform-origin:50% 100%; transform-origin:50% 100%; /*定义渐变效果 */ background-image: -moz-linear-gradient(top, #93bad8 0%, #6c9dc0 85%, #628faf 100%); background-image: -webkit-linear-gradient(top, #93bad8 0%, #6c9dc0 85%, #628faf 100%); background-image: linear-gradient(top, #93bad8 0%, #6c9dc0 85%, #628faf 100%); /*定义阴影 */ Implement dragging icons to the recycle bin and deleting them based on jQuery_jquery-shadow:0 -2px 2px rgba(0,0,0,0.1), 0 1px rgba(255,255,255,0.35) inset; z-index:10; font: bold 26px sans-serif; color: #5A88A9; text-align: center; text-shadow: 1px 1px 1px rgba(255, 255, 255, 0.1); line-height: 115px; } .folder .back{ /*定义渐变效果 */ background-image: -webkit-linear-gradient(top, #93bad8 0%, #89afcc 10%, #5985a5 60%); background-image: -moz-linear-gradient(top, #93bad8 0%, #89afcc 10%, #5985a5 60%); background-image: linear-gradient(top, #93bad8 0%, #89afcc 10%, #5985a5 60%); /*定义圆角*/ border-radius:0 5px 0 0; /*定义阴影 */ Implement dragging icons to the recycle bin and deleting them based on jQuery_jquery-shadow:0 -1px 1px rgba(0,0,0,0.15); } /*在.back前加上内容 */ .folder .back:before{ content:''; width:60px; height:10px; border-radius:4px 4px 0 0; background-color:#93bad8; position:absolute; top:-10px; left:0px; Implement dragging icons to the recycle bin and deleting them based on jQuery_jquery-shadow:0 -1px 1px rgba(0,0,0,0.15); } /*在.back后加上内容 */ .folder .back:after{ content:''; width:100%; height:4px; border-radius:5px; position:absolute; bottom:5px; left:0px; Implement dragging icons to the recycle bin and deleting them based on jQuery_jquery-shadow:0 4px 8px #333; } .folder.open .front{ /*3D转换50度 */ -moz-transform:rotateX(-50deg); -webkit-transform:rotateX(-50deg); transform:rotateX(-50deg); } /*---------------------------- Draggable Icons -----------------------------*/ #main img{ position:absolute; cursor:move; }
Write JS
$(function() { var folder = $("#main .folder"), //文件夹 front = folder.find('.front'), //文件夹前面部分 img = $("#main img"), //容器main中的所有图片 droppedCount = 0; //记数器 img.draggable(); //使所有图片可以拖拽 folder.droppable({ //droppable事件,即拖拽到文件夹时触发的事件 drop : function(event, ui) {//释放时触发 // 移动拖拽的图片 ui.draggable.remove(); // 给计数器加1 front.text(++droppedCount); }, activate : function(){ //拖拽时让文件夹打开 folder.addClass('open'); }, deactivate : function(){ //停止拖拽时让文件夹关闭 folder.removeClass('open'); } }); });
Source code download: jQuery implements the recycle bin function of dragging and deleting small icons
The above is a tutorial to implement the function of dragging icons to the recycle bin and deleting them. Thank you for your patience in reading. I hope it will be helpful to everyone's learning.