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 無(wú)限級(jí)下拉菜單的簡(jiǎn)單實(shí)現(xiàn)代碼
本篇文章主要是對(duì)jquery 無(wú)限級(jí)下拉菜單的簡(jiǎn)單實(shí)現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
jQuery中[attribute^=value]選擇器用法實(shí)例
這篇文章主要介紹了jQuery中[attribute^=value]選擇器用法,實(shí)例分析了[attribute^=value]選擇器的功能、定義及匹配以某些值開(kāi)始的元素的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
表頭固定(利用jquery實(shí)現(xiàn)原理介紹)
表頭固定應(yīng)該是一個(gè)用得比較多的功能,參考了網(wǎng)上幾個(gè)例子,在幾個(gè)常用瀏覽器下顯示不是很完美2012-11-11
jQuery實(shí)現(xiàn)手勢(shì)解鎖密碼特效
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)手勢(shì)解鎖密碼特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
jQuery滿(mǎn)屏焦點(diǎn)圖左右滾動(dòng)特效代碼分享
這篇文章主要介紹了jQuery滿(mǎn)屏焦點(diǎn)圖左右滾動(dòng)特效,一段精致的焦點(diǎn)圖輪播代碼,有需要的小伙伴可以參考下。2015-09-09
jQuery實(shí)現(xiàn)的類(lèi)flash菜單效果代碼
這里要說(shuō)的就是一個(gè)菜單的hover的效果,一般我們通過(guò)CSS的偽類(lèi):hover實(shí)現(xiàn)的菜單切換都比較單調(diào),因?yàn)閮H僅是一個(gè)簡(jiǎn)單的圖片的變化,不像很多flash菜單一樣變化非常的平滑,但是這里我們就是要用非flash的技術(shù)實(shí)現(xiàn)一個(gè)平滑的hover效果。2010-05-05
jQuery插件擴(kuò)展實(shí)例【添加回調(diào)函數(shù)】
這篇文章主要介紹了jQuery插件擴(kuò)展的方法,結(jié)合實(shí)例形式分析了jQuery插件添加回調(diào)函數(shù)與事件觸發(fā)機(jī)制的相關(guān)操作技巧,需要的朋友可以參考下2016-11-11
jQuery 動(dòng)畫(huà)與停止動(dòng)畫(huà)效果實(shí)例詳解
這篇文章主要介紹了jQuery 動(dòng)畫(huà)與停止動(dòng)畫(huà)效果,結(jié)合實(shí)例形式詳細(xì)分析了jQuery 動(dòng)畫(huà)與停止動(dòng)畫(huà)效果相關(guān)函數(shù)功能、用法及使用注意事項(xiàng),需要的朋友可以參考下2020-05-05
使用CDN和AJAX加速WordPress中jQuery的加載
這篇文章主要介紹了使用CDN和AJAX加速WordPress中jQuery的加載的方法,注意一下WordPress中以及CDN的Google連接在內(nèi)地的網(wǎng)絡(luò)問(wèn)題,需要的朋友可以參考下2015-12-12

