当前位置:网站首页>原生JS 实现页面元素的拖动 拖拽
原生JS 实现页面元素的拖动 拖拽
2022-06-28 13:37:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
1 . 实现原理
要实现页面元素的拖动,其原理就是根据鼠标的移动实时的更改元素的left 和 top值(当然元素肯定是要做绝对定位的),那么就达到我们要的效果了呀!
鼠标的位置是可以通过 e.clientX 获取的,通过获取的值减去鼠标和目标元素之间的偏移量,就是我们的 left 值了呗, top值是同理的,不过记住要设置界限哟,不然跑出去了。
代码我尽量写了注释,如果还是有什么不懂的,直接评论就好了,我会尽快回复的。
2 . 实例展示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style> * {
padding: 0; margin: 0; } .move {
height: 100px; width: 100px; background: #41ffce; position: absolute; top: 50px; left: 50px; } .move:hover{
cursor: pointer; } </style>
</head>
<body>
<div class="move"></div>
<script> window.onload = function () {
// 目标元素 var move = document.getElementsByClassName("move")[0]; // 鼠标按下 move.onmousedown = function (e) {
// 获取事件对象 var e_down = e || window.event; // 计算鼠标点击的位置 和 目标元素之间 的偏移量 var x = e_down.clientX - e_down.target.offsetLeft; var y = e_down.clientY - e_down.target.offsetTop; // 我们想要拖拽元素,其实就是根据鼠标的移动实时的更改元素的left 和 top值 // 鼠标的位置是可以通过e.clientX 获取的,然后减去x 不就是我们的left值了 //鼠标移动,肯定是在按住的情况下移动的嘛 document.onmousemove = function(e){
var e_move = e || window.event; left_ = e_move.clientX - x; top_ = e_move.clientY - y; // 对left_和top_添加界限 if(left_ < 0){
left_ = 0 }else if(left_ > document.documentElement.clientWidth - e_down.target.offsetWidth){
left_ = document.documentElement.clientWidth - e_down.target.offsetWidth } if(top_ < 0){
top_ = 0 }else if(top_ > document.documentElement.clientHeight - e_down.target.offsetHeight){
top_ = document.documentElement.clientHeight - e_down.target.offsetHeight } // move.setAttribute("style","top:"+top_+'px'+";left:"+left_+"px") move.style.left = left_+'px'; move.style.top = top_+'px'; } // 释放鼠标 move.onmouseup = function(){
document.onmousemove = null } } } </script>
</body>
</html>希望能够帮助到大家,有什么问题可以 直接评论即可,如果不够详细的话也可以说,我会及时回复的。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/150641.html原文链接:https://javaforall.cn
边栏推荐
- Yii2 connects to websocket service to realize that the server actively pushes messages to the client
- Oracle cloud infrastructure extends distributed cloud services to provide organizations with greater flexibility and controllability
- Jeecg 官方组件的使用笔记(更新中...)
- China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share
- How about stock online account opening and account opening process? Is it safe to open a mobile account?
- 你的代码会说话吗?(上)
- yii2连接websocket服务实现服务端主动推送消息给客户端
- Commonly used "redmine" for # test bug
- Pytorch main modules
- 股票网上开户及开户流程怎样?手机开户是安全么?
猜你喜欢

Oracle cloud infrastructure extends distributed cloud services to provide organizations with greater flexibility and controllability

PCB understand Wang, are you? I am not

腾讯云国际云服务器登录之后没有网络,如何排查?

Jupyter notebook中添加虚拟环境

Align content attribute in flex layout

MySQL multi table joint query

5A synchronous rectifier chip 20V to 12v2a/5v4.5a high current 24W high power synchronous rectifier chip high current step-down IC fs2462

First knowledge of exception

China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected

Oracle 云基础设施扩展分布式云服务,为组织提供更高的灵活性和可控性
随机推荐
Centos7:切换mysql用户并登录mysql
新品体验:阿里云新一代本地SSD实例i4开放公测
ThreadLocal的简单理解
[codec] write H264 decoder (1) from scratch
再谈exception——异常抛出时会发生什么?
New product experience: Alibaba cloud's new generation of local SSD instance I4 open beta
股票网上开户及开户流程怎样?手机开户是安全么?
Recognize the startup function and find the user entry
Latest summary! 30 provinces announce 2022 college entrance examination scores
众昂矿业着眼氟化工产业,布局新能源产业链
其他国产手机未能填补华为的空缺,苹果在高端手机市场已无对手
中国广电5G套餐来了,比三大运营商低,却没预期那么低
Unit test ci/cd
Pytorch main modules
我呕血收集融合了来自各路经典shell书籍的脚本教学,作为小白的你快点来吧
Zhongang mining focuses on the fluorine chemical industry and lays out the new energy industry chain
Arcgis 矢量中心点生成矩形并裁剪tif图像进行深度学习样本训练
中国数据库技术大会(DTCC)特邀科蓝SUNDB数据库专家精彩分享
为什么越来越多的用户放弃 Swagger,选择Apifox
设计人工智能产品:技术可能性、用户合意性、商业可行性