当前位置:网站首页>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记录鼠标点击时,在元素内的位置
边栏推荐
- About flex layout justify content: the last solution to the misalignment of space around and why it is solved like this is a discussion
- Advanced operation and maintenance 02
- leetcode-设置交集大小至少为2
- Live classroom system 01 database table design
- 【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显
- C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
- supervisord安装使用
- 生成订单号
- 21 - vertical traversal of binary tree
- Argocd user management, RBAC control, script login, APP synchronization
猜你喜欢
![[paper notes] mobile robot navigation method based on hierarchical depth reinforcement learning](/img/3d/6486f836535e5a1fa1a362d5214d77.png)
[paper notes] mobile robot navigation method based on hierarchical depth reinforcement learning

C language project practice: 24 point game calculator (based on knowledge points such as structure, pointer, function, array, loop, etc.)

Regular expression common syntax parsing

直播课堂系统02-搭建项目环境

@Feignclient detailed tutorial (illustration)

Oracle 报表常用sql

Dynamic programming -- knapsack problem

Live classroom system 03 model class and entity

头部姿态估计原理及可视化_loveliuzz的博客-程序员宅基地_头部姿态估计

Use of KOA framework
随机推荐
Regular verification of ID number
R language practical application case: drawing part (III) - drawing of multiple combination patterns
Palindrome related topics
Towhee weekly model
【数组&&字符串&&宏练习题】
Due to resource constraints, the namenode fails to start with an error unable to create new native thread
supervisord安装使用
FastAPI应用加入Nacos
Regular expression common syntax parsing
[can I do your first project?] Detailed introduction and Simulation Implementation of gzip
104 maximum depth of binary tree and 543 diameter of binary tree and 124 maximum path sum of binary tree
微信官方出品!小程序自动化框架 minium 分享预告
自研的数据产品迭代了一年多,为什么不买第三方商业数据平台产品呢?
ArgoCD 用户管理、RBAC 控制、脚本登录、App 同步
Quick introduction to PKI system
Which is a good fixed asset management system? What are the fixed asset management platforms?
Official wechat product! Applet automation framework minium sharing Preview
对象使用过程中背后调用了哪些方法
Oracle 报表常用sql
Live classroom system 02 build project environment