How to scroll through a div by dragging and not by using the scroll bars

后端 未结 2 1248
野性不改
野性不改 2021-02-07 19:08

I am working on a project that uses a touch-screen interface. I have a div inside of a smaller div, so the smaller div has scroll bars to access the rest of the first div. Here

相关标签:
2条回答
  • 2021-02-07 19:52

    I found this nice JQuery plugin (Not sure if you are ok with using JQuery or not)

    http://digitalillusion.altervista.org/wordpress/pages/dragscroller/viewport-test.html

    0 讨论(0)
  • 2021-02-07 19:55

    This works...I'd started making it for mobile safari before you cited FireFox...so it may have a little extra...

    var _startX = 0;
    var _startY = 0;
    var _offsetX = 0;			
    var _offsetY = 0;
    var _dragElement;
    document.onmousedown = OnMouseDown;
    document.onmouseup = OnMouseUp;
    
    function OnMouseDown(event){
      document.onmousemove = OnMouseMove;
        _startX = event.clientX;
      _startY = event.clientY;
      _offsetX = document.getElementById('div1').offsetLeft;
      _offsetY = document.getElementById('div1').offsetTop;
      _dragElement = document.getElementById('div1');
    
    }
    
    function OnMouseMove(event){
        _dragElement.style.left = (_offsetX + event.clientX - _startX) + 'px';
      _dragElement.style.top = (_offsetY + event.clientY - _startY) + 'px';
    }
    
    function OnMouseUp(event){
      document.onmousemove = null;
      _dragElement=null;
    }
    .div1{position:absolute; height:500px; width: 500px; z-index:1; background-color:red;}
    .div2{position:absolute; top:100px; left:100px; height:100px; width:100px; z-index:2; overflow:hidden; display:block;}
    <div class="div2" id="div2">
      <div class="div1" id="div1">
      </div>
    </div>

    0 讨论(0)
提交回复
热议问题