pure javascript draggable element

后端 未结 1 1713
名媛妹妹
名媛妹妹 2021-01-05 22:56

I know there\'re example of doing this on the web but every example is different and so is my own implementation. I\'m trying to figure what\'s wrong with my implementation

相关标签:
1条回答
  • 2021-01-05 23:12

    Quite interesting, had so far only done it with jQuery. Rewrote it a bit and made sure that every mousemove has it's event listener removed afterwards - that would be a memory leak that you might start to notice otherwise :

    http://jsfiddle.net/8wtq17L8/

    var contextmenu = document.getElementById('contextMenu');
    var initX, initY, mousePressX, mousePressY;
    
    contextmenu.addEventListener('mousedown', function(event) {
    
        initX = this.offsetLeft;
        initY = this.offsetTop;
        mousePressX = event.clientX;
        mousePressY = event.clientY;
    
        this.addEventListener('mousemove', repositionElement, false);
    
        window.addEventListener('mouseup', function() {
          contextmenu.removeEventListener('mousemove', repositionElement, false);
        }, false);
    
    }, false);
    
    function repositionElement(event) {
        this.style.left = initX + event.clientX - mousePressX + 'px';
        this.style.top = initY + event.clientY - mousePressY + 'px';
    }
    

    Seems to be working well. :-)

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Edit - thought I'd add a version with touch support too (although newer devices seem to emulate mouse events). Not like Jquery where you can select multiple event listeners so it's mostly a repetition, just with touch events :

    http://codepen.io/Shikkediel/pen/VLZKor?editors=011

    var object = document.getElementById('element'),
    initX, initY, firstX, firstY;
    
    object.addEventListener('mousedown', function(e) {
    
        e.preventDefault();
        initX = this.offsetLeft;
        initY = this.offsetTop;
        firstX = e.pageX;
        firstY = e.pageY;
    
        this.addEventListener('mousemove', dragIt, false);
    
        window.addEventListener('mouseup', function() {
            object.removeEventListener('mousemove', dragIt, false);
        }, false);
    
    }, false);
    
    object.addEventListener('touchstart', function(e) {
    
        e.preventDefault();
        initX = this.offsetLeft;
        initY = this.offsetTop;
        var touch = e.touches;
        firstX = touch[0].pageX;
        firstY = touch[0].pageY;
    
        this.addEventListener('touchmove', swipeIt, false);
    
        window.addEventListener('touchend', function(e) {
            e.preventDefault();
            object.removeEventListener('touchmove', swipeIt, false);
        }, false);
    
    }, false);
    
    function dragIt(e) {
        this.style.left = initX+e.pageX-firstX + 'px';
        this.style.top = initY+e.pageY-firstY + 'px';
    }
    
    function swipeIt(e) {
        var contact = e.touches;
        this.style.left = initX+contact[0].pageX-firstX + 'px';
        this.style.top = initY+contact[0].pageY-firstY + 'px';
    }
    
    0 讨论(0)
提交回复
热议问题