jquery实现拖拽添加元素功能

需求
1.页面上有两个不同的容器,拖拽a容器的元素添加到b容器中;
2.a保持原位不dogn动,b增加新的元素,要实现的效果如下:
3.点击b容器中的元素移除元素
首先准备两个容器
页面效果如下

<div class="bigBox">
			<div id="aBox">
				<p class="drag" draggable="true" data-id="我是a元素的第一个">我是a元素</p>
				<p class="drag" draggable="true" data-id="我是a元素的第二个">我是a元素</p>
				<p class="drag" draggable="true" data-id="我是a元素的第三个">我是a元素</p>
				<p class="drag" draggable="true" data-id="我是a元素的第四个">我是a元素</p>
				
			</div>
			<div id="bBox">
				
			</div>
		</div>

在css中定义好样式,区分两个容器

.bigBox {
				display: flex;
				width: 100%;
				height: 400px;
			}
			#aBox {
				width: 40%;
				height: 100%;
				background-color: pink;
			}
			#aBox > p {
				line-height: 30px;
				padding: 4px;
				background-color: yellow;
			}
			#bBox {
				width: 40%;
				height: 100%;
				background-color: #00BCF4;
			}
			.span {
				border: 1px slid #ccc;
				border-radius: 12px;
				display: inline-block;
				padding: 3px;
				background-color: red;
			}

封装一个添加元素的方法

function add(addId, htmlId) {
				var listItem = { // 接收绑定的属性值,并赋值给数组的某一项
					name: addId
				}
				var obj = {};
				var html = ''
					coloList.push(listItem)
					coloList = coloList.reduce(function(item, next) { // 对数组进行去重处理
						obj[next.name] ? '' : obj[next.name] = true && item.push(next);
						return item;
					}, []);
					for (var i = 0; i < coloList.length; i++) { // 对去重后的数组渲染到页面
						html += '<span draggable="true" class="span" data-id=' + coloList[i].name + ' >' + coloList[i].name + '</span>'
					}
				htmlId.html(html) // b容器要展示的数据
			}

以下是拖拽的方法函数

var coloList = []
			$(document).on('dragstart', '.drag', function(e) { // 拖拽事件绑定到元素上
				var dudataId = $(this).attr("data-id") // 获取到元素绑定的属性值
				$(document).on('dragenter', '#bBox', function() {
				})
				$(document).on('dragover', '#bBox', function() { // 这行代码一定要有,阻止事件的默认行为,才能触发鼠标放下的事件
					event.preventDefault()
				})
				$('#bBox').on('drop', function(e) { // // 鼠标放下事件被触发把元素添加到bbox中
					add(dudataId, $('#bBox'))
				})
				$(document).on('drop', '#bBox', function() {  // 定时器解绑事件,不然会一直绑定事件,重复添加数据
					var timer = setInterval(function() { 
						$('#bBox').off('dragover')
						$('#bBox').off('dragenter')
						$('#bBox').off('drop')
						clearInterval(timer);
					}, 30)
				})
			})

移除bbox的事件的方法

function remove(removeId, htmlId) {
				console.log(removeId, htmlId)
				var index = -1
				var html = ''
				// var list = coloList
				for (var k = 0; k < coloList.length; k++) {
					if (removeId === coloList[k].name) {
						index = k
						break
					} else {
						index = -1
					}
				}
				if (index != -1) {
					coloList.splice(index, 1)
					// coloList = list
					for (var i = 0; i < coloList.length; i++) { // 对去重后的数组渲染到页面
						html += '<span class="span" data-id=' + coloList[i].name + '>' + coloList[i].name + '</span>'
					}
					htmlId.html(html)
				} else {
					alert('暂无可移除的维度')
				}
			}

绑定点击事件

$('#bBox').on('click', '.span', function(e) {
				remove($(this).attr("data-id"), $('#bBox')) // 参数:动态添加的属性值当前点击的元素,度量列表,维度html
			})

这样就完成了呀。
以下是完整的代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.bigBox {
				display: flex;
				width: 100%;
				height: 400px;
			}
			#aBox {
				width: 40%;
				height: 100%;
				background-color: pink;
			}
			#aBox > p {
				line-height: 30px;
				padding: 4px;
				background-color: yellow;
			}
			#bBox {
				width: 40%;
				height: 100%;
				background-color: #00BCF4;
			}
			.span {
				border: 1px slid #ccc;
				border-radius: 12px;
				display: inline-block;
				padding: 3px;
				background-color: red;
			}
		</style>
	</head>
	<body>
		<div class="bigBox">
			<div id="aBox">
				<p class="drag" draggable="true" data-id="我是a元素的第一个">我是a元素</p>
				<p class="drag" draggable="true" data-id="我是a元素的第二个">我是a元素</p>
				<p class="drag" draggable="true" data-id="我是a元素的第三个">我是a元素</p>
				<p class="drag" draggable="true" data-id="我是a元素的第四个">我是a元素</p>
				
			</div>
			<div id="bBox">
				
			</div>
		</div>
		<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			var coloList = []
			$(document).on('dragstart', '.drag', function(e) {
				var dudataId = $(this).attr("data-id")
				$(document).on('dragenter', '#bBox', function() {
				})
				$(document).on('dragover', '#bBox', function() {
					event.preventDefault()
				})
				$('#bBox').on('drop', function(e) {
					add(dudataId, $('#bBox'))
				})
				$(document).on('drop', '#bBox', function() {
					var timer = setInterval(function() {
						$('#bBox').off('dragover')
						$('#bBox').off('dragenter')
						$('#bBox').off('drop')
						clearInterval(timer);
					}, 30)
				})
			})
			$('#bBox').on('click', '.span', function(e) {
				remove($(this).attr("data-id"), $('#bBox')) // 参数:动态添加的属性值当前点击的元素,度量列表,维度html
			})
			function add(addId, htmlId) {
				var listItem = { // 接收绑定的属性值,并赋值给数组的某一项
					name: addId
				}
				// list.push(weiduListItem)
				var obj = {};
				var html = ''
					// className = 'remove'
					coloList.push(listItem)
					coloList = coloList.reduce(function(item, next) { // 对数组进行去重处理
						obj[next.name] ? '' : obj[next.name] = true && item.push(next);
						return item;
					}, []);
					for (var i = 0; i < coloList.length; i++) { // 对去重后的数组渲染到页面
						html += '<span draggable="true" class="span" data-id=' + coloList[i].name + ' >' + coloList[i].name + '</span>'
					}

			
				// weiduList = lis
				htmlId.html(html) // 维度的数组
			}
			// // 移除页面中维度和度量的元素
			function remove(removeId, htmlId) {
				console.log(removeId, htmlId)
				var index = -1
				var html = ''
				// var list = coloList
				for (var k = 0; k < coloList.length; k++) {
					if (removeId === coloList[k].name) {
						index = k
						break
					} else {
						index = -1
					}
				}
				if (index != -1) {
					coloList.splice(index, 1)
					// coloList = list
					for (var i = 0; i < coloList.length; i++) { // 对去重后的数组渲染到页面
						html += '<span class="span" data-id=' + coloList[i].name + '>' + coloList[i].name + '</span>'
					}
					htmlId.html(html)
				} else {
					alert('暂无可移除的维度')
				}
			}
			
		</script>
	</body>
</html>

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值