JavaScript限定范围拖拽及自定义滚动

学习笔记:
拖拽div要发生三个事件:

  1. 鼠标按下onmousedown;
  2. 鼠标移动onmousemove;
  3. 鼠标松开onmouseup;

注意事项:
(1)要防止div移出可视框,要限制div移动的横纵坐标;
(2)防止火狐的bug, 要在最后写上return false,阻止默认事件;
(3)防止鼠标运动时移出div,所以要用document.onmousemove和document.onmouseup,不能用oDiv.onmousemove;

1. 实例一

两个对象:div1 和 div2,其中div1是div2的父元素,div2只能在div1的范围内拖拽

04203545_vrRX.png
image.png
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>客户区可见范围限制拖拽</title>
 <style type="text/css">
  * {
   padding: 0;
   margin: 0;
  }
  #div1 {
   width: 500px;
   height: 500px;
   background: orange;
   position: relative;
   left: 100px;
   top: 30px;
  }
  #div2 {
   width: 100px;
   height: 100px;
   background: black;
   position: absolute;
   border: 1px solid blue;
  }
 </style>
</head>
<body>
 <div id="div1">
  <div id="div2"></div>
 </div>

 <script type="text/javascript">
  var oDiv1 = document.getElementById('div1');
  var oDiv2 = document.getElementById('div2');

  function getStyle(obj, attr) {
   if (obj.currentStyle) {
    return obj.currentStyle[attr];
   } else {
    return getComputedStyle(obj, null)[attr];
   }
  }
  oDiv2.onmousedown = function(ev) {
   var oEvent = ev || event;
   // var disX = oEvent.clientX - oDiv2.offsetLeft;
   // var disY = oEvent.clientY - oDiv2.offsetTop;
   var disX = oEvent.clientX - parseInt(getStyle(oDiv2, 'left'));
   var disY = oEvent.clientY - parseInt(getStyle(oDiv2, 'top'));

   document.onmousemove = function(ev) {
    var oEvent = ev || event;
    var l = oEvent.clientX - disX;
    var t = oEvent.clientY - disY;


    if (l < 0) {
     l = 0;
    } else if (l > oDiv1.offsetWidth - /*parseInt(getStyle(oDiv2,'width'))*/oDiv2.offsetWidth) {
     l = oDiv1.offsetWidth - oDiv2.offsetWidth;
    }
    if (t < 0) {
     t = 0;
    } else if (t > oDiv1.offsetHeight - oDiv2.offsetHeight) {
     t = oDiv1.offsetHeight - oDiv2.offsetHeight;
    }
    oDiv2.style.left = l + 'px';
    oDiv2.style.top = t + 'px';
   };


   document.onmouseup = function() {
    document.onmousemove = null;//如果不取消,鼠标弹起div依旧会随着鼠标移动
    document.onmouseup = null;
   };
  };
 </script>
</body>
</html>

2. 实例二

基于上述原理,我们来做一个自定义滚动条,通过拖拽滚动条的位置来控制另一个对象的大小,比如一幅图。

04203547_Imy7.png
image.png
04203548_Pi1w.png
image.png
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>自定义滚动条</title>
 <style type="text/css">
 #div1 {
  width: 600px;
  height: 20px;
  background: orange;
  position: relative;
  margin: 50px auto;
 }
 #div2 {
  width: 20px;
  height: 20px;
  background: green;
  position: absolute;
 }
 #div3 {
  width: 0;
  height: 0;
  margin: 20px auto;
 }
 #div3 img {
  width: 100%;
  height: 100%;
 }
 </style>
</head>
<body>
 <div id="div1">
 <div id="div2"></div>
 </div>
 <div id="div3">
 ![](https://timgsa.baidu.com/141128%2F201411281041075742.jpg)
 </div>
 <script type="text/javascript">
 var oDiv1 = document.getElementById('div1');
 var oDiv2 = document.getElementById('div2');
 var oDiv3 = document.getElementById('div3');
 oDiv2.onmousedown = function(ev) {
  var oEvent = ev || event;
  var disX = oEvent.clientX - oDiv2.offsetLeft;

  document.onmousemove = function(ev) {
  var oEvent = ev || event;
  var l = oEvent.clientX - disX;
  if (l < 0) {
   l = 0;
  } else if (l > oDiv1.offsetWidth - oDiv2.offsetWidth) {
   l = oDiv1.offsetWidth - oDiv2.offsetWidth;
  }
  oDiv2.style.left = l + 'px';//l范围:[0,580]
  //document.title = l / 580; //范围:[0,1]
  var ratio = oDiv1.offsetWidth - oDiv2.offsetWidth;
  var scale = l / ratio;
  var w = 600 * scale;
  var h = 370 * scale;
  console.log(w);
  oDiv3.style.cssText = ';width:' + w + 'px;height:' + h +'px;';

  };

  document.onmouseup = function() {
  document.onmousemove = null;
  document.onmouseup = null;
  };
 };
 </script>
</body>
</html>

3. 更多相关文章

  1. js中event对象详解
  2. offsetTop、clientTop、scrollTop、offsetTop各属性介绍
  3. js实时获取鼠标所在坐标

转载于:https://my.oschina.net/cllgeek/blog/1584700

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值