产品需求,需要用到对div(dom)进行拖拽缩放操作,看到有好多插件,要么依赖jQuery,要么文件太大。
封装了一个插件,不压缩状态下5KB。
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>demo</title>
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
100%;
height: 100%;
}
.box {
200px;
height: 200px;
border: 1px dashed #ccc;
position: absolute;
left: 50px;
top: 50px;
}
.content {
100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
font-size: 28px;
}
.dragHandle {
15px;
height: 15px;
border-radius: 50%;
background: #000;
position: absolute;
left: 0;
top: -15px
}
.resizeHandle {
display: block;
position: absolute;
10px;
height: 10px;
border-right: 2px solid #b2b2b2;
border-bottom: 2px solid #b2b2b2;
right: 0px;
bottom: 0px;
overflow: hidden;
cursor: nw-resize;
}
</style>
</head>
<body>
<div class="box">
<div class="content">
Candy
</div>
<div class="dragHandle">
</div>
<div class="resizeHandle">
</div>
</div>
</body>
</html>
<script src="./drag.js">
</script>
<script>
//父元素相对定位 relative
//当前拖动元素绝对定位 absolute
let elem = document.querySelector(".box")
let dragHandle = document.querySelector(".dragHandle")
let resizeHandle = document.querySelector(".resizeHandle")
let drag = new Draggable(elem, dragHandle, resizeHandle, true, (dragStyle) => {
console.log("当前元素现在的位置",dragStyle)
}, (resizeStyle) => {
console.log("当前元素现在的宽高",resizeStyle)
});
</script>
js在github上,顺便点个星吧,谢谢啦:
github地址:https://github.com/1096121120/Draggable
骚年,我看你骨骼惊奇,有撸代码的潜质,这里有324.57GB的前端学习资料传授于你!什么,你不信???
先随便看几个图:
肯定没看够。再来个GIF图热个身???
那么问题来了,如果你也想入坑前端或者学习更多技术,广交天下朋友(基友),认识更多有趣的灵魂,和沙雕网友在线斗图,撩更多小姐姐的话,欢迎加入前端交流群鸭~
扫二维码加为好友即可拉你入群获取学习资料哦。