当前位置:网站首页>js拖拽元素
js拖拽元素
2022-07-23 09:41:00 【ohmorning】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.a {
width: 200px;
height: 200px;
background: #ccc;
position: absolute;
}
</style>
</head>
<body>
<div class="a"></div>
<script>
let a = document.querySelector('.a')
let isMove = false
let x = 0
let y = 0
a.onmousedown = function (e) {
x = e.pageX - a.offsetLeft
y = e.pageY - a.offsetTop
isMove = true
}
a.onmouseup = function () {
isMove = false
}
a.onmousemove = function (e) {
if (isMove) {
a.style.left = e.pageX-x + 'px'
a.style.top = e.pageY-y + 'px'
}
}
</script>
</body>
</html>
注解:
e.pageX 鼠标X轴位置
a.offsetLeft 元素边框距离父元素内边距的距离
e.pageX - a.offsetLeft记录鼠标点击时,在元素内的位置
边栏推荐
猜你喜欢

深度学习单图三维人脸重建

R language practical application case: drawing part (III) - drawing of multiple combination patterns

什么是Per-Title编码?

【 langage c】 devinez jeux numériques + applet d'arrêt

基本51单片机点阵汉字显示程序设计

如何实现多个传感器与西门子PLC之间485无线通讯?
It is suggested that Siyuan notes can be compatible with third-party sync disks

基于nextcloud构建个人网盘

Live classroom system 03 supplement model class and entity

Building personal network disk based on nextcloud
随机推荐
[untitled]
Linux scheduled database backup script
supervisord安装使用
mysql函数汇总之数学函数
Authing supports Zadig! Unified authentication and rapid docking of cloud native users
直播课堂系统02-搭建项目环境
C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
21 - vertical traversal of binary tree
Program design of dot matrix Chinese character display of basic 51 single chip microcomputer
身份证号正则验证
Prometheus入门使用(三)
微信官方出品!小程序自动化框架 minium 分享预告
Opencv calculation outsourcing rectangle
What methods are called behind the use of objects
AI acceleration gesture recognition experience based on efr32mg24
【C語言】猜數字小遊戲+關機小程序
【 langage c】 devinez jeux numériques + applet d'arrêt
day18
[record of question brushing] 19. Delete the penultimate node of the linked list
什么是Per-Title编码?