Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用querySelectorAll移动多个div_Javascript_Jquery_Drag And Drop_Draggable - Fatal编程技术网

Javascript 使用querySelectorAll移动多个div

Javascript 使用querySelectorAll移动多个div,javascript,jquery,drag-and-drop,draggable,Javascript,Jquery,Drag And Drop,Draggable,我试图使用queryselectorall来移动多个div,但我不知道如何适应,因此遇到了麻烦。我想用一下这种风格: [].forEach.call(els,函数(el){ ... }); 但是没有成功,我如何修改脚本使其工作 我的代码: var draggableEl = document.querySelectorAll('[data-drag]'), magnet = document.querySelector('.magnet-zone'); function isOverlappin

我试图使用
queryselectorall
来移动多个div,但我不知道如何适应,因此遇到了麻烦。我想用一下这种风格:

[].forEach.call(els,函数(el){
...
});

但是没有成功,我如何修改脚本使其工作

我的代码:

var draggableEl = document.querySelectorAll('[data-drag]'), magnet = document.querySelector('.magnet-zone');
function isOverlapping(el1, el2) {
    var rect1 = el1.getBoundingClientRect(), rect2 = el2.getBoundingClientRect();
    return !(rect1.top > rect2.bottom || rect1.right < rect2.left || rect1.bottom < rect2.top || rect1.left > rect2.right);
}
function moveToPos(x, y) {
    var el = draggableEl;
    el.style.transform = 'translate(' + Math.round(x, 10) + 'px, ' + Math.round(y, 10) + 'px) translateZ(0)';
    el.style.webkitTransform = 'translate(' + Math.round(x, 10) + 'px, ' + Math.round(y, 10) + 'px) translateZ(0)';
}
function moveMagnet(x, y) {
    var dist = 12, width = $('body').width() / 2, height = $('body').height(), direction = x > width ? 1 : -1, percX = x > width ? (x - width) / width : -(width - x) / width, percY = Math.min(1, (height - y) / (height / 2));
    magnet.style.marginLeft = Math.round(dist / 1.5 * percX) + 'px';
    magnet.style.marginBottom = Math.round(dist * percY) + 'px';
}
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);


}

$(draggableEl).on('touchstart mousedown', onTouchStart).on('touchmove drag', move).on('touchend mouseup', onTouchEnd);
function onTouchStart(event) {
    var rect = this.getBoundingClientRect();
    $('body').addClass('touching');
    $(this).removeClass('edge transition');
    this._touchOrigin = {
        x: event.pageX,
        y: event.pageY
    };
    this._posOrigin = {
        x: rect.left,
        y: rect.top
    };
}
function onTouchEnd(event) {
    var el = draggableEl, rect = el.getBoundingClientRect(), width = $('body').width(), halfScreen = width / 2;
    if (!$(el).hasClass('overlap')) {
        $('body').removeClass('moving touching');
        magnet.style.marginBottom = magnet.style.marginLeft = '0px';
        var x = rect.left + rect.width / 2 < halfScreen ? +10 : width - 10 - rect.width;
        $(el).addClass('edge');
        moveToPos(x, rect.top);
        setTimeout(function () {
            $(el).removeClass('edge');
        }, 500);
    }
}
var draggableEl=document.queryselectoral(“[data drag]”),magnet=document.querySelector(“.magnet zone”);
功能重叠(el1、el2){
var rect1=el1.getBoundingClientRect(),rect2=el2.getBoundingClientRect();
返回!(rect1.top>rect2.bottom | rect1.rightrect2.right);
}
函数moveToPos(x,y){
var el=draggableEl;
el.style.transform='translate('+Math.round(x,10)+'px',+Math.round(y,10)+'px)translateZ(0)';
el.style.webkitttransform='translate('+Math.round(x,10)+'px',+Math.round(y,10)+'px)translateZ(0)';
}
功能磁铁(x,y){
变量dist=12,width=$('body').width()/2,height=$('body').height(),direction=x>width?1:-1,percX=x>width?(x-width)/width:-(width-x)/width,percY=Math.min(1,(height-y)/(height/2));
magnet.style.marginLeft=Math.round(dist/1.5*percX)+“px”;
magnet.style.marginBottom=数学圆(dist*percY)+“px”;
}
功能移动(事件){
var el=draggableEl,magnetRect=magnet.getBoundingClientRect(),elRect=el.getBoundingClientRect();
x=此。_posOrigin.x+event.pageX-此。_touchOrigin.x;
y=这个。_posOrigin.y+event.pageY-这个。_touchOrigin.y;
移动磁铁(x+elRect.WITH/2,y+elRect.height/2);
$('body').addClass('moving');
var touchPos={
上图:y,
右:x+elRect.width,
底部:y+垂直高度,
左:x
};
重叠=!(touchPos.top>magnetRect.bottom | | touchPos.rightmagnetRect.right);
如果(重叠){
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(“转换”);
setTimeout(函数(){
$(el).removeClass('transition');
}, 150);
setTimeout(函数(){
el.移除();
setTimeout(函数(){
$('body').removeClass('moving touching');
}, 900);
}, 1000);
}
magnet.className=magnet.className.replace('overlap','')+'overlap';
el.className=el.className.replace('overlap','')+'overlap';
}否则{
if($(el).hasClass('transition')){
$(el).removeClass('transition');
}
if($(el).hasClass('overlap')){
$(el).addClass(“转换”);
setTimeout(函数(){
$(el).removeClass('transition');
}, 100);
}
magnet.className=magnet.className.replace('overlap','');
el.className=el.className.replace('overlap','');
}
moveToPos(x,y);
}
$(DragTableel).on('touchstart mousedown',onTouchStart.).on('touchmove drag',move.).on('touchend mouseup',onTouchEnd);
函数onTouchStart(事件){
var rect=this.getBoundingClientRect();
$('body').addClass('touching');
$(this.removeClass('edge transition');
这是.\u touchOrigin={
x:event.pageX,
y:event.pageY
};
这是。_posOrigin={
x:rect.left,
y:rect.top
};
}
函数onTouchEnd(事件){
var el=draggableEl,rect=el.getBoundingClientRect(),width=$('body').width(),半屏幕=width/2;
if(!$(el).hasClass('overlap')){
$('body').removeClass('moving touching');
magnet.style.marginBottom=magnet.style.marginLeft='0px';
var x=rect.left+rect.width/2
资料来源: