Adapt function to queryselectorall

Asked

Viewed 80 times

1

I’m trying to adapt a function that used to be used only with Queryselector, but I need to adapt it to get all the Divs (Queryselectorall).

Look at:

var draggableEl = document.querySelectorAll('[data-drag]')

The function

function move(event) {
    var el = draggableEl, magnetRect = magnet.getBoundingClientRect(), elRect = el.getBoundingClientRect();
    x = this._posOrigin.x + event.pageX - this._touchOrigin.x;
    y = this._posOrigin.y + event.pageY - this._touchOrigin.y;
    moveMagnet(x + elRect.width / 2, y + elRect.height / 2);
    $('body').addClass('moving');
    var touchPos = {
        top: y,
        right: x + elRect.width,
        bottom: y + elRect.height,
        left: x
    };
    overlapping = !(touchPos.top > magnetRect.bottom || touchPos.right < magnetRect.left || touchPos.bottom < magnetRect.top || touchPos.left > magnetRect.right);
    if (overlapping) {
        var mx = magnetRect.width / 2 + magnetRect.left;
        var my = magnetRect.height / 2 + magnetRect.top;
        x = mx - elRect.width / 2;
        y = my - elRect.height / 2;
        if (!$(el).hasClass('overlap')) {
            $(el).addClass('transition');
            setTimeout(function () {
                $(el).removeClass('transition');
            }, 150);

            setTimeout(function () {
                el.remove();
                setTimeout(function () {
                $('body').removeClass('moving touching');
            }, 900);
            }, 1000);
        }
        magnet.className = magnet.className.replace(' overlap', '') + ' overlap';
        el.className = el.className.replace(' overlap', '') + ' overlap';
    } else {
        if ($(el).hasClass('transition')) {
            $(el).removeClass('transition');
        }
        if ($(el).hasClass('overlap')) {
            $(el).addClass('transition');
            setTimeout(function () {
                $(el).removeClass('transition');
            }, 100);
        }
        magnet.className = magnet.className.replace(' overlap', '');
        el.className = el.className.replace(' overlap', '');
    }
    moveToPos(x, y);


}

what defines:

[].forEach.call(draggableEl, function(el) {
$(el).on('touchstart mousedown', onTouchStart).on('touchmove drag', move(el)).on('touchend mouseup', onTouchEnd);
});

She returns to me: Uncaught Typeerror: Undefined is not a Function in:

var el = draggableEl, magnetRect = magnet.getBoundingClientRect(), elRect = el.getBoundingClientRect();

Full code: jsfiddle.net/4yt1roa6

  • 1

    Forehead to change var el = draggableEl for var el = this

1 answer

2


Analyzing the first line:

function move(event) {
    var el = draggableEl,

and knowing that you used to var draggableEl = document.querySelector('[data-drag]') gives me the idea that that function was going to get the draggableEl to the outer scope, and it worked because you only had one element. That’s not good practice, it’s better to have everything within the function.

Assuming this function is run by an Event Handler then the this will be the element you want and so you can use:

function move(event){
    var el = this,

In addition to this syntax .on('touchmove drag', move(el)) is incorrect. So you will be running the function directly, it should be only , move).

Forehead like this:

$('[data-drag]')
    .on('touchstart mousedown', onTouchStart)
    .on('touchmove drag', move)
    .on('touchend mouseup', onTouchEnd);

This way jQuery will fetch all the elements that have a property '[data-drag]' and ties them to event headphones. When one of these runs will be passed the element as this and then you can use as I mentioned above var el = this.

  • 1

    @user3163662 can make a jsFiddle with the problem?

  • 1

    Pardon was my analysis error, it was the move() function that did not know what to move, adapted and put this at the time to call, this way moveToPos(x,y,this) that is correct?

  • 1

    @user3163662: where you suggest to put move(x,y,this)? By my analysis, all you need to do is change the lines I put together in the answer.

  • 1

    Look at, moveToPos was used to position the element according to the drag, in the move() function it is executed at the end, but as in the function it did not specify which of the drags to move it ended up not moving anything, then came my media

  • 1

    @user3163662 ok, in this case it is more correct semantically to use move(x, y, el);. For the el is a reference of this.

  • Thank you very much!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.