分别拖动每个元素

时间:2016-08-08 19:28:58

标签: javascript html css

我在像Win7 Sidebar这样的javascript中制作小工具系统,但我只能使用一个元素。如果我使用两个,所有元素可以具有相同的位置或不起作用。我需要可以使用多个元素并分别拖动每个元素(每个元素都有他的特定位置)。

我只有一个元素。如何将其与多个一起使用?



var selected = null, // Object of the element to be moved
    x_pos = 0, y_pos = 0, // Stores x & y coordinates of the mouse pointer
    x_elem = 0, y_elem = 0; // Stores top, left values (edge) of the element

// Will be called when user starts dragging an element
function _drag_init(elem) {
    // Store the object of the element which needs to be moved
    selected = elem;
    x_elem = x_pos - selected.offsetLeft;
    y_elem = y_pos - selected.offsetTop;
}

// Will be called when user dragging an element
function _move_elem(e) {
    x_pos = document.all ? window.event.clientX : e.pageX;
    y_pos = document.all ? window.event.clientY : e.pageY;
    if (selected !== null) {
        selected.style.left = (x_pos - x_elem) + 'px';
        selected.style.top = (y_pos - y_elem) + 'px';
    }
}

// Destroy the object when we are done
function _destroy() {
    selected = null;
}

// Bind the functions...
document.getElementById('draggable-element').onmousedown = function () {
    _drag_init(this);
    return false;
};

document.onmousemove = _move_elem;
document.onmouseup = _destroy;

body {padding:10px}

#draggable-element {
  width:125px;
  height:125px;
  background-color:#666;
  color:white;
  padding:10px 12px;
  cursor:move;
  position:relative; /* important (all position that's not `static`) */
}

<div id="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div>
&#13;
&#13;
&#13;

fiddle

1 个答案:

答案 0 :(得分:1)

有两点需要注意:

  1. 如果您想对多个元素执行某些操作,则需要使用classtag s。
  2. _drag_init()功能中,您将x_elemy_elem设置为x_posy_pos,这些更改在您_move_elem()功能中。因此它将当前定位添加到元素偏移量中。所以只需摆脱selected.Offset
  3. 但是,您还需要解决其他一些计算问题

    var selected = null, // Object of the element to be moved
        x_pos = 0, y_pos = 0, // Stores x & y coordinates of the mouse pointer
        x_elem = 0, y_elem = 0; // Stores top, left values (edge) of the element
    
    // Will be called when user starts dragging an element
    function _drag_init(elem) {
        // Store the object of the element which needs to be moved
        selected = elem;
        x_elem = x_pos ;
        y_elem = y_pos;
    }
    
    // Will be called when user dragging an element
    function _move_elem(e) {
        x_pos = document.all ? window.event.clientX : e.pageX;
        y_pos = document.all ? window.event.clientY : e.pageY;
        if (selected !== null) {
            selected.style.left = (x_pos - x_elem) + 'px';
            selected.style.top = (y_pos - y_elem) + 'px';
        }
    }
    
    // Destroy the object when we are done
    function _destroy() {
        selected = null;
    }
    
    // Bind the functions...
    var draggables = document.getElementsByClassName('draggable-element');
    for(var i = 0; i < draggables.length; i++){
      draggables[i].onmousedown = function () {
          _drag_init(this);
          return false;
      };
    }
    
    document.onmousemove = _move_elem;
    document.onmouseup = _destroy;
    body {padding:10px}
    
    .draggable-element {
      width:125px;
      height:125px;
      background-color:#666;
      color:white;
      padding:10px 12px;
      cursor:move;
      position:relative; /* important (all position that's not `static`) */
    }
    <div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div>
    <div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div>
    <div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div>
    <div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div>
    <div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div>
    <div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div>