需求:图片上传后,拖拽排序
网上找了一些案例,大多仅能对现有图片拖拽,对追加的元素无效,找到了了相似案例,改进后代码如下
其中使用背景展示图片,是为了方便控制图片被拖拽到li外部导致bug,不使用背景图展示的话,可以参考‘关闭’按钮禁止拖拽的写法
判断鼠标按下时接触的元素节点
如果是关闭按钮的节点i,拖拽时直接阻断
拖拽结束后释放拖拽功能,否则点击到禁止拖拽区域后,其它可拖拽元素也无法拖拽了
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>拖拽排序</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<style>
* {
box-sizing: border-box;
}
ul {
display: flex;
flex-wrap: wrap;
}
li {
margin:10px;
font-size: 16px;
list-style: none;
height: 100px;
width: 100px;
border: 1px solid #ddd;
cursor: move;
}
.dashed {
border: 3px dashed #ddd;
}
#container{
display: flex;
}
.sort_img{
cursor: move;
background-size: 100% 100%;
background-repeat: no-repeat;
}
.del-img ,.dels-img{
cursor: pointer;
}
.del-img{
position: relative;
top: -20px;
border: 1px solid red;
}
</style>
</head>
<body>
<button id=