Moveable/draggable

前端 未结 7 600
我在风中等你
我在风中等你 2020-11-28 02:31

This is my updated and modified script, it works completely, except I would like to universalize it... observe the **** how can I make it so that I don\'t have to do f

7条回答
  •  一向
    一向 (楼主)
    2020-11-28 03:27

    Is jQuery an option for you? It makes what you are doing really simple since the code already exists.

    http://jqueryui.com/demos/draggable/

    Demo

    JavaScript Code

    window.onload = addListeners;
    
    function addListeners(){
        document.getElementById('dxy').addEventListener('mousedown', mouseDown, false);
        window.addEventListener('mouseup', mouseUp, false);
    
    }
    
    function mouseUp()
    {
        window.removeEventListener('mousemove', divMove, true);
    }
    
    function mouseDown(e){
      window.addEventListener('mousemove', divMove, true);
    }
    
    function divMove(e){
        var div = document.getElementById('dxy');
      div.style.position = 'absolute';
      div.style.top = e.clientY + 'px';
      div.style.left = e.clientX + 'px';
    }​
    

提交回复
热议问题