jquery實(shí)現(xiàn)拖拽添加元素功能
本文實(shí)例為大家分享了jquery實(shí)現(xiàn)拖拽添加元素的具體代碼,供大家參考,具體內(nèi)容如下
需求
1.頁(yè)面上有兩個(gè)不同的容器,拖拽a容器的元素添加到b容器中;
2.a保持原位不dogn動(dòng),b增加新的元素,要實(shí)現(xiàn)的效果如下:
3.點(diǎn)擊b容器中的元素移除元素
首先準(zhǔn)備兩個(gè)容器
頁(yè)面效果如下
<div class="bigBox"> <div id="aBox"> <p class="drag" draggable="true" data-id="我是a元素的第一個(gè)">我是a元素</p> <p class="drag" draggable="true" data-id="我是a元素的第二個(gè)">我是a元素</p> <p class="drag" draggable="true" data-id="我是a元素的第三個(gè)">我是a元素</p> <p class="drag" draggable="true" data-id="我是a元素的第四個(gè)">我是a元素</p> </div> <div id="bBox"> </div> </div>
在css中定義好樣式,區(qū)分兩個(gè)容器
.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; }
封裝一個(gè)添加元素的方法
function add(addId, htmlId) { var listItem = { // 接收綁定的屬性值,并賦值給數(shù)組的某一項(xiàng) name: addId } var obj = {}; var html = '' coloList.push(listItem) coloList = coloList.reduce(function(item, next) { // 對(duì)數(shù)組進(jìn)行去重處理 obj[next.name] ? '' : obj[next.name] = true && item.push(next); return item; }, []); for (var i = 0; i < coloList.length; i++) { // 對(duì)去重后的數(shù)組渲染到頁(yè)面 html += '<span draggable="true" class="span" data-id=' + coloList[i].name + ' >' + coloList[i].name + '</span>' } htmlId.html(html) // b容器要展示的數(shù)據(jù) }
以下是拖拽的方法函數(shù)
var coloList = [] $(document).on('dragstart', '.drag', function(e) { // 拖拽事件綁定到元素上 var dudataId = $(this).attr("data-id") // 獲取到元素綁定的屬性值 $(document).on('dragenter', '#bBox', function() { }) $(document).on('dragover', '#bBox', function() { // 這行代碼一定要有,阻止事件的默認(rèn)行為,才能觸發(fā)鼠標(biāo)放下的事件 event.preventDefault() }) $('#bBox').on('drop', function(e) { // // 鼠標(biāo)放下事件被觸發(fā)把元素添加到bbox中 add(dudataId, $('#bBox')) }) $(document).on('drop', '#bBox', function() { // 定時(shí)器解綁事件,不然會(huì)一直綁定事件,重復(fù)添加數(shù)據(jù) 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++) { // 對(duì)去重后的數(shù)組渲染到頁(yè)面 html += '<span class="span" data-id=' + coloList[i].name + '>' + coloList[i].name + '</span>' } htmlId.html(html) } else { alert('暫無(wú)可移除的維度') } }
綁定點(diǎn)擊事件
$('#bBox').on('click', '.span', function(e) { remove($(this).attr("data-id"), $('#bBox')) // 參數(shù):動(dòng)態(tài)添加的屬性值當(dāng)前點(diǎn)擊的元素,度量列表,維度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元素的第一個(gè)">我是a元素</p> <p class="drag" draggable="true" data-id="我是a元素的第二個(gè)">我是a元素</p> <p class="drag" draggable="true" data-id="我是a元素的第三個(gè)">我是a元素</p> <p class="drag" draggable="true" data-id="我是a元素的第四個(gè)">我是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')) // 參數(shù):動(dòng)態(tài)添加的屬性值當(dāng)前點(diǎn)擊的元素,度量列表,維度html }) function add(addId, htmlId) { var listItem = { // 接收綁定的屬性值,并賦值給數(shù)組的某一項(xiàng) name: addId } // list.push(weiduListItem) var obj = {}; var html = '' // className = 'remove' coloList.push(listItem) coloList = coloList.reduce(function(item, next) { // 對(duì)數(shù)組進(jìn)行去重處理 obj[next.name] ? '' : obj[next.name] = true && item.push(next); return item; }, []); for (var i = 0; i < coloList.length; i++) { // 對(duì)去重后的數(shù)組渲染到頁(yè)面 html += '<span draggable="true" class="span" data-id=' + coloList[i].name + ' >' + coloList[i].name + '</span>' } // weiduList = lis htmlId.html(html) // 維度的數(shù)組 } // // 移除頁(yè)面中維度和度量的元素 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++) { // 對(duì)去重后的數(shù)組渲染到頁(yè)面 html += '<span class="span" data-id=' + coloList[i].name + '>' + coloList[i].name + '</span>' } htmlId.html(html) } else { alert('暫無(wú)可移除的維度') } } </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于jquery實(shí)現(xiàn)的鼠標(biāo)拖拽元素復(fù)制并寫(xiě)入效果
- Jquery UI實(shí)現(xiàn)一次拖拽多個(gè)選中的元素操作
- jquery插件jquery.dragscale.js實(shí)現(xiàn)拖拽改變?cè)卮笮〉姆椒?附demo源碼下載)
- jquery網(wǎng)頁(yè)元素拖拽插件效果及實(shí)現(xiàn)
- jQuery 版元素拖拽原型代碼
- JQuery拖拽元素改變大小尺寸實(shí)現(xiàn)代碼
- jQuery如何獲取動(dòng)態(tài)添加的元素
- jQuery動(dòng)態(tài)添加、刪除元素的方法
- Jquery顯示、隱藏元素以及添加刪除樣式
- Jquery動(dòng)態(tài)添加及刪除頁(yè)面節(jié)點(diǎn)元素示例代碼
相關(guān)文章
jquery.gridrotator實(shí)現(xiàn)響應(yīng)式圖片展示畫(huà)廊效果
本教程將教大家制作一個(gè)jQuery響應(yīng)式圖片展示畫(huà)廊效果,所有圖片以網(wǎng)格的形式排列,然后定時(shí)隨機(jī)翻轉(zhuǎn)其中某些格子用來(lái)切換圖片。這種效果可以用來(lái)當(dāng)做背景或裝飾放在我們的網(wǎng)站上。2015-06-06jquery控制背景音樂(lè)開(kāi)關(guān)與自動(dòng)播放提示音的方法
這篇文章主要介紹了jquery控制背景音樂(lè)開(kāi)關(guān)與自動(dòng)播放提示音的方法,實(shí)例分析了背景音樂(lè)開(kāi)關(guān)的技巧與自動(dòng)播放提示音的常見(jiàn)用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02formValidator3.3的ajaxValidator一些異常分析
ajaxvalidator是大家問(wèn)的最多的問(wèn)題,修正一個(gè)bug(感謝網(wǎng)友“じ龍峸√”),并把大家最關(guān)心的問(wèn)題,再做一次闡述。2011-07-07jquery+ajax實(shí)現(xiàn)異步上傳文件顯示進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了jquery+ajax實(shí)現(xiàn)異步上傳文件顯示進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08從零開(kāi)始學(xué)習(xí)jQuery (三) 管理jQuery包裝集
在使用jQuery選擇器獲取到j(luò)Query包裝集后, 我們需要對(duì)其進(jìn)行操作. 本章首先講解如何動(dòng)態(tài)的創(chuàng)建元素, 接著學(xué)習(xí)如何管理jQuery包裝集, 比如添加,刪除,切片等.2011-02-02jQuery實(shí)現(xiàn)的頁(yè)面遮罩層功能示例【測(cè)試可用】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的頁(yè)面遮罩層功能,結(jié)合完整實(shí)例形式詳細(xì)分析了jQuery遮罩層實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10jquery UI Datepicker時(shí)間控件的使用方法(終結(jié)版)
這篇文章是jquery UI Datepicker時(shí)間控件的使用方法終結(jié)版,可以說(shuō)是技術(shù)的升華,實(shí)現(xiàn)的功能有限制的開(kāi)始時(shí)間和結(jié)束時(shí)間跨度不超過(guò)三天,并配置有清空時(shí)間,重選時(shí)間等,感興趣的小伙伴們可以參考一下2015-11-11