JS实现鼠标拖拽效果以及放大缩小

要求:拖拽的子元素不能走出父元素,大小不能超过父元素,放大/缩小时阻止冒泡事件

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{margin: 0;padding: 0;}
.box1{border: 10px solid #000;width: 400px;height: 400px;position: relative;margin: 0 auto;}
.box2{position: absolute;width: 80px;height: 80px;background: gold;top: 0;left: 0;}
.box3{position: absolute;width: 10px;height: 10px;right: -5px;bottom: -5px;background: #0000FF;border-radius: 50%;}
</style>
</head>
<body>
<div class="box1">
<div class="box2">
<div class="box3"></div>
</div>
</div>
<script type="text/javascript">
var box1 = document.querySelector(".box1");
var box2 = document.querySelector(".box2");
var box3 = document.querySelector(".box3");
box1.onmousedown = function(e) {
e = e || window.event;
var left = box2.offsetLeft;
var top = box2.offsetTop;
var nowX = e.clientX;
var nowY = e.clientY;
document.onmousemove = function(e) {
var resultX = left + e.clientX - nowX;
var resultY = top + e.clientY - nowY;
if(resultX < 0) {
resultX = 0;

} else if (resultX > box1.clientWidth - box2.offsetWidth) {

resultX = box1.clientWidth - box2.offsetWidth
}
if(resultY < 0) {
resultY = 0;
} else if (resultY > box1.clientHeight - box2.offsetHeight) {
resultY = box1.clientHeight - box2.offsetHeight
}
box2.style.left = resultX + "px";
box2.style.top = resultY + "px";
}
}
box3.onmousedown = function(e) {
e = e || window.event;
e.stopPropagation();
e.cancelBubble = true;
var width = box2.offsetWidth;
var height = box2.offsetHeight;
var nowX = e.clientX;
var nowY = e.clientY;
document.onmousemove = function(e) {
e = e || window.event;
var a = width + e.clientX - nowX;
var b = height + e.clientY - nowY;
if(a > box1.clientWidth){
a = box1.clientWidth;
}
if(b > box1.clientHeight){
b = box1.clientHeight;
}
box2.style.width = a + "px"
box2.style.height = b + "px";
}
}
document.onmouseup = function() {
document.onmousemove = null;
}
</script>
</body>
</html>

原文地址:https://www.cnblogs.com/mizuno0237/p/11443456.html

时间: 2024-07-31 06:01:04

JS实现鼠标拖拽效果以及放大缩小的相关文章

js实现鼠标拖拽div-------Day44

如果去问这样一个问题"你觉得鼠标操作简单,还是键盘操作简单",相信会有多数人都会回答鼠标吧,毕竟键盘按钮那么多,如果手小了或者手法不规范了,太容易出问题了,也对操作的速度和有效性是大大的降低了,当然不可否认,会有那么一批人,操作起键盘来完全可以忽略鼠标的,但这些都应该是骨灰级别的了吧,起码我现在的层次还接触不到,只能向往. 然而,当面对这么一个问题时,我突然有点纠结,因为毕竟以前我也是那么想的,但是这次开发让我是大跌眼镜,键盘操作我在开发中,我只能判定其按键是否按键,然后根据不同按键进

jquery鼠标拖拽效果分享

//html代码部分 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="../js/jquery-1.11.1.js"></script> </head> <style> body

JS Event 鼠标拖拽事件

<!DOCTYPE html><html> <head>        <meta charset="UTF-8">        <title>JS Event鼠标拖拽事件</title>                <style>            #box{width:200px;height:200px;background:#000;position:absolute;}       

js实现鼠标拖拽多选功能

最近做了一个用js实现鼠标拖拽多选的功能,于是整理了一下思路,写了一个小demo:遮罩出现:被遮罩盖住的,即为选中的块(背景色为粉色)下面是具体代码,注释已在文中,与大家交流. <!DOCTYPE html> <html> <head> <title>鼠标拖拽多选功能</title> <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">

简单的鼠标拖拽效果(原生js实现)

之前在聊天群里看到有人说面试的时候被问到了怎样实现一个拖拽效果,当时看到后在心里默默思考了下,结果发现好像我也写不出来啊.本着遇到一个解决一个的思想,就亲自敲了一个,看到张鑫旭大神写的代码,真的很厉害,多多学习了,(感觉随便搜一个关于前端方面的问题都能看到他的网站,真是太佩服了,写了那么多文章,十分感谢.)好了,接下来就进入正题了.想实现一个效果首先得明白其中的逻辑,知道了实现逻辑后,就可以码代码了.首先我实现的效果是: 鼠标按下后,才可以执行后续效果 鼠标已经按下,然后鼠标移动,需要拖拽的元素

js 鼠标拖拽效果实现

效果: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>速表拖拽效果实现</title> <style> *{ margin: 0; padding: 0; } .toolbar{ height: 30px; text-align: left; padding-left: 20px;

用JS实现版面拖拽效果

类似于这样的一个版面,点击标题栏,实现拖拽效果. 添加onmousedown事件 通过获取鼠标的坐标(clientX,clientY)来改变面板的位置 注意:面板使用绝对定位方式,是以左上角为参考点,所以我们还需要获取鼠标在面板的位置,也就是以鼠标所在位置为参考点. // 光标按下时光标和面板之间的距离 disX=event.clientX-oDrag.offsetLeft, disY=event.clientY-oDrag.offsetTop; 添加onmousemove事件 在获取了鼠标相对

JS原生---鼠标拖拽

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">  <head runat="server">      &l

鼠标拖拽效果

1 <div id="div3"> 2 </div> 3 <script type="text/javascript"> 4 5 6 div3.onmousedown= function(e){ 7 var arr =[]; 8 arr[0] =[div3.offsetLeft,div3.offsetTop];//记录第一次点击的值; 9 var x = e.offsetX;//获取元素鼠标点击坐标 10 var y = e.of