欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript限定范围拖拽及自定义滚动条应用(3)

程序员文章站 2022-06-20 10:34:37
两个对象:div1 和 div2,其中div1是div2的父元素,div2只能在div1的范围内拖拽 图中,红点是鼠标的位置,两个绿色箭头相减的结果就是disx...

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

JavaScript限定范围拖拽及自定义滚动条应用(3)

图中,红点是鼠标的位置,两个绿色箭头相减的结果就是disx,最后oevent.clientx - disx 就是绿色箭头的部分,这个长度就是判断是否“出格”的依据,也就是这个短的绿色箭头范围应该在0 ~ div2.offsetwidth - div1.offsetwidth之间!

<!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>

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

JavaScript限定范围拖拽及自定义滚动条应用(3)

JavaScript限定范围拖拽及自定义滚动条应用(3)

<!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">
 <img src="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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。