HTML5拖拽和JS拖拽分别怎么实现,代码是什么
Admin 2022-06-29 群英技术资讯 1365 次浏览
今天小编跟大家讲解下有关“HTML5拖拽和JS拖拽分别怎么实现,代码是什么”的内容 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了相关资料,希望小伙伴们看了有所帮助。拖拽元素:可以为元素添加 draggable="true"来让元素能够被拖拽。
拖拽元素的事件监听:(应用于拖拽元素)
ondragstart当拖拽开始时调用ondragleave 当鼠标离开拖拽元素时调用ondragend 当拖拽结束时调用ondrag 整个拖拽过程都会调用目标元素:把元素A拖拽到元素B里,那么元素B就是目标元素。页面中任何一个元素都可以成为目标元素。
目标元素的事件监听:(应用于目标元素)
ondragenter 当拖拽元素进入时调用ondragover 当拖拽元素停留在目标元素上时,就会连续一直触发(不管拖拽元素此时是移动还是不动的状态)ondrop 当在目标元素上松开鼠标时调用ondragleave 当鼠标离开目标元素时调用如果想让拖拽元素在目标元素里做点事情,就必须要在 ondragover() 里加event.preventDefault()这一行代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
width: 200px;
height: 200px;
background: green;
}
.box2 {
position: relative;
left: 300px;
top: 50px;
width: 300px;
height: 300px;
background: red;
}
</style>
</head>
<body>
<div class="box" draggable="true"></div>
<div class="box2"></div>
<script>
// HTML5 拖拽
// 应用于拖拽元素
var box = document.querySelector('.box')
box.ondragstart = function () {
console.log('拖拽开始')
}
box.ondragleave = function () {
console.log('鼠标离开元素')
}
box.ondragend = function () {
console.log('拖拽结束')
}
// box.ondrag = function () {
// console.log('在拖拽');
// }
// 应用于目标元素(想把 box 拖拽进去的地方)
var box2 = document.querySelector('.box2')
box2.ondragenter = function () {
console.log('进来了')
}
box2.ondragleave = function () {
console.log('离开了')
}
// 当拖拽元素在 目标元素上时,连续触发
box2.ondragover = function (e) {
// 如果想让拖拽元素在目标元素里做点事情,就必须要在 ondragover() 里加event.preventDefault()这一行代码。
e.preventDefault()
console.log('over')
}
box2.ondrop = function () {
console.log('松开鼠标了')
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box-b {
width: 250px;
height: 250px;
background: green;
}
.cell-a {
float: left;
width: 50px;
height: 50px;
margin: 5px;
text-align: center;
line-height: 50px;
border-radius: 50%;
background: red;
}
.box-c {
width: 200px;
height: 200px;
margin-top: 10px;
background: skyblue;
}
</style>
</head>
<body>
<p>boxB</p>
<div class="box-b">
<div class="cell-a" draggable="true">1</div>
<div class="cell-a" draggable="true">2</div>
<div class="cell-a" draggable="true">3</div>
<div class="cell-a" draggable="true">4</div>
<div class="cell-a" draggable="true">5</div>
</div>
<p>boxC</p>
<div class="box-c"></div>
<script>
var cellA = document.querySelectorAll('.cell-a')
var boxB = document.querySelector('.box-b')
var boxC = document.querySelector('.box-c')
var temp = null
cellA.forEach((cell, index) => {
// 从 boxB 拖拽到 boxC
cell.ondragstart = function () {
// 保持当前拖拽的元素
temp = this
}
cell.ondragend = function () {
temp = null
}
boxC.ondragover = function (e) {
e.preventDefault()
}
boxC.ondragenter = function () {
this.appendChild(temp)
}
// 从 boxC 拖拽到 boxB
boxB.ondragover = function (e) {
e.preventDefault()
}
boxB.ondragenter = function () {
this.appendChild(temp)
}
})
</script>
</body>
</html>
效果展示

按下鼠标进行简单的拖拽。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
#box {
position: absolute;
width: 200px;
height: 200px;
background: green;
}
</style>
<script>
window.onload = function () {
var box = document.getElementById('box')
var disX = 0
var disY = 0
box.onmousedown = function (e) {
var e = e || window.event
disX = e.clientX - this.offsetLeft
disY = e.clientY - this.offsetTop
box.onmousemove = function (e) {
var e = e || window.event
box.style.left = e.clientX - disX + 'px'
box.style.top = e.clientY - disY + 'px'
}
box.onmouseup = function (e) {
console.log('end')
this.onmousemove = null
}
return false
}
}
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>
效果展示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
position: absolute;
width: 200px;
height: 200px;
background: skyblue;
}
.box1 {
position: absolute;
border: 1px dashed black;
opacity: 0.5;
}
.way-box {
position: absolute;
bottom: 30px;
right: 30px;
/* 无法选中 */
-moz-user-select: none; /* 火狐 */
-webkit-user-select: none; /* 谷歌 */
-ms-user-select: none; /* IE */
user-select: none;
}
</style>
<script>
window.onload = function () {
;(function () {
var box = document.querySelector('.box')
var disX, disY, temp
var body = document.querySelector('body')
var way1 = document.querySelector('#way1')
var way2 = document.querySelector('#way2')
box.onmousedown = function (e) {
var e = e || window.event // 兼容性写法
disX = e.clientX - this.offsetLeft
disY = e.clientY - this.offsetTop
temp = document.createElement('div')
body.appendChild(temp)
temp.classList.add('box')
temp.classList.add('box1')
// 移动后位置会变,temp 的位置应该与 box 位置重合
temp.style.left = e.clientX - disX + 'px' // 记得加单位!
temp.style.top = e.clientY - disY + 'px'
temp.onmousemove = function (e) {
var e = e || window.event
temp.style.left = e.clientX - disX + 'px' // 记得加单位!
temp.style.top = e.clientY - disY + 'px'
}
temp.onmouseup = function (e) {
console.log('end')
this.onmousemove = null
// 1 则默认不发生实际移动
if (way2.checked) {
box.style.left = e.clientX - disX + 'px'
box.style.top = e.clientY - disY + 'px'
}
temp.style.display = 'none'
this.onmouseup = null
}
}
})()
}
</script>
</head>
<body>
<div class="box"></div>
<div class="way-box">
<p>请选择拖拽的方式</p>
<input type="radio" id="way1" name="way" checked />
<label for="way1">1</label>
<input type="radio" id="way2" name="way" />
<label for="way2">2</label>
</div>
</body>
</html>
效果展示

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
也就是因为这个flexbox伸缩盒布局太强大了,以至于我没在意它也是display的一个属性。要想解决这个布局问题,我们还是先了解一些基础的问题。先回顾下display有哪些属性吧:none:隐藏对象。与visibility属性的hidden值不同,其不为被隐藏的对象保留其物理空间inline:指定对象为内联元素。block:指定对象为块元素。list-i ...
这篇文章主要介绍了CSS 阴影动画优化技巧,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
1.opacity:0只是把元素隐藏起来了,但是还是占有布局,所以还是对布局有影响<divclass="div1">snda:opacity:0只是把元素隐藏起来了,但是还是占有布局,所以还是对布局有影响</div><p>ppskdkad</p>.div1{opacity:0;width:200px;height:200px;border:1pxsolidred;}2.visibility:hidden还是只是把元素隐藏了,但是还是占有布 ...
这篇文章主要介绍了详解如何使用CSS3中的结构伪类选择器和伪元素选择器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要给大家分享使用CSS3中filter属性来实现网页灰色或者黑色模式,本文有示例代码,具有一定的借鉴价值,感兴趣的朋友可以参考参考,下面我们就来一起学习下。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008