JS實(shí)現(xiàn)盒子拖拽效果
本文實(shí)例為大家分享了JS實(shí)現(xiàn)盒子拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下
效果:

html代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>拖拽</title>
<body>
<div class="leftBox"></div>
<div class="rightBox">
<!-- 開啟拖拽屬性draggable -->
<div class="circle" draggable="true"></div>
</div>
</body>
</html>
css代碼:
<style>
.leftBox {
display: inline-block;
width: 100px;
height: 100px;
border: 1px solid black;
border-radius: 10px;
position: relative;
}
.rightBox {
display: inline-block;
width: 100px;
height: 100px;
border: 1px solid black;
border-radius: 10px;
position: relative;
}
.circle {
width: 50px;
height: 50px;
border-radius: 50%;
background: radial-gradient(25px at center, white, skyblue);
/* 絕對(duì)居中 */
position: absolute;
left: 50%;
margin-left: -25px;
top: 50%;
margin-top: -25px;
}
</style>
js代碼:
<script>
//獲取dom元素,分別是左盒子 圓圈 右盒子
var leftBox = document.querySelector('.leftBox');
var circle = document.querySelector('.circle');
var rightBox = document.querySelector('.rightBox');
var text = document.querySelector('.text');
//移動(dòng)circle
circle.
//開啟左盒子的移入事件
leftBox.ondragover = function (event) {
event.preventDefault();
}
leftBox.ondrop = function () {
leftBox.appendChild(circle);
}
//開啟右盒子的移入事件
rightBox.ondragover = function (event) {
event.preventDefault();
}
rightBox.ondrop = function () {
rightBox.appendChild(circle);
}
</script>

關(guān)于事件的用法,官方用到了object.addEventListener("dragover", myScript)和event.target.id
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS中數(shù)學(xué)計(jì)算精度問題的解決方案
這篇文章主要給大家介紹了JS中數(shù)學(xué)計(jì)算精度問題的解決方案,文中通過代碼示例和圖文結(jié)合給大家講解非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12
layui關(guān)閉層級(jí)、簡(jiǎn)單監(jiān)聽的實(shí)例
今天小編就為大家分享一篇layui關(guān)閉層級(jí)、簡(jiǎn)單監(jiān)聽的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
在JS中a標(biāo)簽加入單擊事件屏蔽href跳轉(zhuǎn)頁面
這篇文章主要介紹了JS中a標(biāo)簽加入單擊事件屏蔽href跳轉(zhuǎn)頁面的相關(guān)資料,需要的朋友可以參考下2016-12-12
JavaScript中正則表達(dá)式判斷匹配規(guī)則及常用方法
JS作為一門常用于web開發(fā)的語言,必然要具備正則這種強(qiáng)大的特性,本文將對(duì)JS中的正則用法及常用函數(shù)進(jìn)行一番總結(jié)2017-08-08
bootstrap datepicker插件默認(rèn)英文修改為中文
這篇文章主要為大家詳細(xì)介紹了bootstrap datepicker插件默認(rèn)英文修改為中文的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
chrome瀏覽器不支持onmouseleave事件的解決技巧
發(fā)現(xiàn)給div加的 onmouseleave事件在chrome 中不起效果,下面與大家分享下具體的解決方法,不會(huì)的朋友可以了解下哈,希望對(duì)大家有所幫助2013-05-05

