用jquery怎样写拖拽且添加元素的效果?
发布时间:2022-01-13 09:39:02 所属栏目:语言 来源:互联网
导读:这篇文章给大家分享的是有关用jquery实现拖拽元素的内容,小编觉得挺实用的,因此分享给大家做个参考。本文要实现的要求的是拖拽且添加元素的功能,接下来一起跟随小编看看怎样写吧。 需求 1.页面上有两个不同的容器,拖拽a容器的元素添加到b容器中; 2.a保
这篇文章给大家分享的是有关用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> (编辑:衡水站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |